Skip to content

Commit

Permalink
For mozilla-mobile#9506: Implement banner custom view based on materi…
Browse files Browse the repository at this point in the history
…al design specs.
  • Loading branch information
mcarare committed Sep 4, 2020
1 parent 89e57b8 commit 8c797ec
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
68 changes: 68 additions & 0 deletions app/src/main/java/org/mozilla/fenix/browser/InfoBanner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.browser

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import kotlinx.android.synthetic.main.info_banner.view.*
import org.mozilla.fenix.R

/**
* Displays an Info Banner in the specified container with a message and an optional action.
* The container can be a placeholder layout inserted in the original screen, or an existing layout.
*
* @param context - A [Context] for accessing system resources.
* @param container - The layout where the banner will be shown
* @param message - The message displayed in the banner
* @param dismissText - The text on the dismiss button
* @param actionText - The text on the action to perform button
* @param actionToPerform - The action to be performed on action button press
*/
class InfoBanner(
private val context: Context,
private val container: ViewGroup,
private val message: String,
private val dismissText: String,
private val actionText: String? = null,
private val actionToPerform: (() -> Unit)? = null
) {
@SuppressLint("InflateParams")
private val bannerLayout = LayoutInflater.from(context)
.inflate(R.layout.info_banner, null)

internal fun showBanner() {
bannerLayout.banner_info_message.text = message
bannerLayout.dismiss.text = dismissText

if (actionText.isNullOrEmpty()) {
bannerLayout.action.visibility = GONE
} else {
bannerLayout.action.text = actionText
}

container.addView(bannerLayout)

val params = bannerLayout.layoutParams as ViewGroup.LayoutParams
params.height = WRAP_CONTENT
params.width = MATCH_PARENT

bannerLayout.dismiss.setOnClickListener {
dismiss()
}

bannerLayout.action.setOnClickListener {
actionToPerform?.invoke()
}
}

internal fun dismiss() {
container.removeView(bannerLayout)
}
}
53 changes: 53 additions & 0 deletions app/src/main/res/layout/info_banner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?><!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="21dp"
android:background="?foundation"
android:paddingStart="11dp"
android:paddingTop="21dp"
android:paddingEnd="16dp">

<TextView
android:id="@+id/banner_info_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textAppearance="@style/Body14TextStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Banner info message." />

<com.google.android.material.button.MaterialButton
android:id="@+id/dismiss"
style="@style/CreateShortcutDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/action"
app:layout_constraintTop_toBottomOf="@+id/banner_info_message"
tools:text="Dismiss" />

<com.google.android.material.button.MaterialButton
android:id="@+id/action"
style="@style/CreateShortcutDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="3dp"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/banner_info_message"
tools:text="Action" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 8c797ec

Please sign in to comment.