From f9d3e9ab05ebe005b3ade6a8ba6d1f8a5402496a Mon Sep 17 00:00:00 2001 From: mcarare Date: Wed, 2 Sep 2020 17:00:12 +0300 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/9506: Implement banner custom view based on material design specs. --- .../org/mozilla/fenix/browser/InfoBanner.kt | 68 +++++++++++++++++++ app/src/main/res/layout/info_banner.xml | 53 +++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 app/src/main/java/org/mozilla/fenix/browser/InfoBanner.kt create mode 100644 app/src/main/res/layout/info_banner.xml diff --git a/app/src/main/java/org/mozilla/fenix/browser/InfoBanner.kt b/app/src/main/java/org/mozilla/fenix/browser/InfoBanner.kt new file mode 100644 index 0000000000..be74646d29 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/browser/InfoBanner.kt @@ -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) + } +} diff --git a/app/src/main/res/layout/info_banner.xml b/app/src/main/res/layout/info_banner.xml new file mode 100644 index 0000000000..0c3e3b31e3 --- /dev/null +++ b/app/src/main/res/layout/info_banner.xml @@ -0,0 +1,53 @@ + + + + + + + + +