diff --git a/app/src/main/java/org/mozilla/fenix/ext/Int.kt b/app/src/main/java/org/mozilla/fenix/ext/Int.kt index a8730969a..cf84a7cde 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/Int.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/Int.kt @@ -5,8 +5,19 @@ package org.mozilla.fenix.ext import android.content.Context +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes import androidx.core.content.ContextCompat import org.mozilla.fenix.ThemeManager +/** + * Returns the color resource corresponding to the attribute. + */ +@ColorRes fun Int.getColorIntFromAttr(context: Context): Int = ThemeManager.resolveAttribute(this, context) + +/** + * Returns the color int corresponding to the attribute. + */ +@ColorInt fun Int.getColorFromAttr(context: Context): Int = ContextCompat.getColor(context, this.getColorIntFromAttr(context)) diff --git a/app/src/main/java/org/mozilla/fenix/library/LibraryPageUIView.kt b/app/src/main/java/org/mozilla/fenix/library/LibraryPageUIView.kt new file mode 100644 index 000000000..3448c6ee2 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/library/LibraryPageUIView.kt @@ -0,0 +1,74 @@ +/* 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.library + +import android.graphics.ColorFilter +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.view.ViewGroup +import android.widget.ActionMenuView +import android.widget.ImageButton +import androidx.annotation.ColorRes +import androidx.appcompat.view.menu.ActionMenuItemView +import androidx.appcompat.widget.Toolbar +import androidx.core.content.ContextCompat +import androidx.core.view.forEach +import io.reactivex.Observable +import io.reactivex.Observer +import org.mozilla.fenix.R +import org.mozilla.fenix.ext.asActivity +import org.mozilla.fenix.mvi.Action +import org.mozilla.fenix.mvi.Change +import org.mozilla.fenix.mvi.UIView +import org.mozilla.fenix.mvi.ViewState + +/** + * Shared base class for [org.mozilla.fenix.library.bookmarks.BookmarkUIView] and + * [org.mozilla.fenix.library.history.HistoryUIView]. + */ +abstract class LibraryPageUIView( + container: ViewGroup, + actionEmitter: Observer, + changesObservable: Observable +) : UIView(container, actionEmitter, changesObservable) { + + protected val context = container.context + protected val activity = context?.asActivity() + + /** + * Adjust the colors of the [Toolbar] on the top of the screen. + */ + protected fun setToolbarColors(@ColorRes foregroundRes: Int, @ColorRes backgroundRes: Int) { + val toolbar = activity?.findViewById(R.id.navigationToolbar) + + val foreground = ContextCompat.getColor(context, foregroundRes) + val background = ContextCompat.getColor(context, backgroundRes) + + toolbar?.apply { + setBackgroundColor(background) + setTitleTextColor(foreground) + + val colorFilter = PorterDuffColorFilter(foreground, PorterDuff.Mode.SRC_IN) + + overflowIcon?.colorFilter = colorFilter + forEach { child -> + when (child) { + is ImageButton -> child.drawable.colorFilter = colorFilter + is ActionMenuView -> themeActionMenuView(child, colorFilter) + } + } + } + } + + private fun themeActionMenuView(item: ActionMenuView, colorFilter: ColorFilter) { + item.forEach { innerChild -> + if (innerChild is ActionMenuItemView) { + innerChild.compoundDrawables.forEach { drawable -> + item.post { drawable?.colorFilter = colorFilter } + } + } + } + } +} diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkUIView.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkUIView.kt index c74273c59..20542df81 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkUIView.kt @@ -4,17 +4,9 @@ package org.mozilla.fenix.library.bookmarks -import android.graphics.PorterDuff.Mode.SRC_IN -import android.graphics.PorterDuffColorFilter import android.view.LayoutInflater import android.view.ViewGroup -import android.widget.ImageButton import android.widget.LinearLayout -import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.view.menu.ActionMenuItemView -import androidx.appcompat.widget.ActionMenuView -import androidx.appcompat.widget.Toolbar -import androidx.core.content.ContextCompat import io.reactivex.Observable import io.reactivex.Observer import io.reactivex.functions.Consumer @@ -23,16 +15,15 @@ import mozilla.appservices.places.BookmarkRoot import mozilla.components.concept.storage.BookmarkNode import mozilla.components.support.base.feature.BackHandler import org.mozilla.fenix.R -import org.mozilla.fenix.ext.asActivity import org.mozilla.fenix.ext.getColorIntFromAttr -import org.mozilla.fenix.mvi.UIView +import org.mozilla.fenix.library.LibraryPageUIView class BookmarkUIView( container: ViewGroup, actionEmitter: Observer, changesObservable: Observable ) : - UIView(container, actionEmitter, changesObservable), + LibraryPageUIView(container, actionEmitter, changesObservable), BackHandler { var mode: BookmarkState.Mode = BookmarkState.Mode.Normal @@ -46,8 +37,6 @@ class BookmarkUIView( .inflate(R.layout.component_bookmark, container, true) as LinearLayout private val bookmarkAdapter: BookmarkAdapter - private val context = container.context - private val activity = context?.asActivity() init { view.bookmark_list.apply { @@ -87,21 +76,6 @@ class BookmarkUIView( fun getSelected(): Set = bookmarkAdapter.selected - private fun setToolbarColors(foreground: Int, background: Int) { - val toolbar = activity?.findViewById(R.id.navigationToolbar) - val colorFilter = PorterDuffColorFilter( - ContextCompat.getColor(context, foreground), SRC_IN - ) - toolbar?.run { - setBackgroundColor(ContextCompat.getColor(context, background)) - setTitleTextColor(ContextCompat.getColor(context, foreground)) - themeToolbar( - toolbar, foreground, - background, colorFilter - ) - } - } - private fun setUIForSelectingMode( root: BookmarkNode?, mode: BookmarkState.Mode.Selecting @@ -125,62 +99,9 @@ class BookmarkUIView( } private fun setTitle(root: BookmarkNode?) { - (activity as? AppCompatActivity)?.title = - if (root?.guid in setOf( - BookmarkRoot.Mobile.id, - null - ) - ) { - context.getString(R.string.library_bookmarks) - } else { - root!!.title - } - } - - private fun themeToolbar( - toolbar: Toolbar, - textColor: Int, - backgroundColor: Int, - colorFilter: PorterDuffColorFilter? = null - ) { - toolbar.setTitleTextColor(ContextCompat.getColor(context!!, textColor)) - toolbar.setBackgroundColor(ContextCompat.getColor(context, backgroundColor)) - - if (colorFilter == null) { - return - } - - toolbar.overflowIcon?.colorFilter = colorFilter - (0 until toolbar.childCount).forEach { - when (val item = toolbar.getChildAt(it)) { - is ImageButton -> item.drawable.colorFilter = colorFilter - is ActionMenuView -> themeActionMenuView(item, colorFilter) - } - } - } - - private fun themeActionMenuView( - item: ActionMenuView, - colorFilter: PorterDuffColorFilter - ) { - (0 until item.childCount).forEach { - val innerChild = item.getChildAt(it) - if (innerChild is ActionMenuItemView) { - themeChildren(innerChild, item, colorFilter) - } - } - } - - private fun themeChildren( - innerChild: ActionMenuItemView, - item: ActionMenuView, - colorFilter: PorterDuffColorFilter - ) { - val drawables = innerChild.compoundDrawables - for (k in drawables.indices) { - drawables[k]?.let { - item.post { innerChild.compoundDrawables[k].colorFilter = colorFilter } - } + activity?.title = when (root?.guid) { + BookmarkRoot.Mobile.id, null -> context.getString(R.string.library_bookmarks) + else -> root.title } } } diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt index 13bea980e..b32ecfa84 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt @@ -4,16 +4,11 @@ package org.mozilla.fenix.library.history -import android.graphics.PorterDuff.Mode.SRC_IN -import android.graphics.PorterDuffColorFilter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.ImageButton -import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.widget.Toolbar import androidx.constraintlayout.widget.ConstraintLayout -import androidx.core.content.ContextCompat +import androidx.core.view.isVisible import androidx.recyclerview.widget.LinearLayoutManager import io.reactivex.Observable import io.reactivex.Observer @@ -23,16 +18,15 @@ import kotlinx.android.synthetic.main.component_history.view.* import kotlinx.android.synthetic.main.delete_history_button.* import mozilla.components.support.base.feature.BackHandler import org.mozilla.fenix.R -import org.mozilla.fenix.ext.asActivity import org.mozilla.fenix.ext.getColorIntFromAttr -import org.mozilla.fenix.mvi.UIView +import org.mozilla.fenix.library.LibraryPageUIView class HistoryUIView( container: ViewGroup, actionEmitter: Observer, changesObservable: Observable ) : - UIView(container, actionEmitter, changesObservable), + LibraryPageUIView(container, actionEmitter, changesObservable), BackHandler { var mode: HistoryState.Mode = HistoryState.Mode.Normal @@ -40,8 +34,6 @@ class HistoryUIView( private val historyAdapter: HistoryAdapter private var items: List = listOf() - private val context = container.context - private val activity = context?.asActivity() fun getSelected(): List = historyAdapter.selected @@ -77,7 +69,7 @@ class HistoryUIView( private fun setUIForSelectingMode( mode: HistoryState.Mode.Editing ) { - (activity as? AppCompatActivity)?.title = + activity?.title = context.getString(R.string.history_multi_select_title, mode.selectedItems.size) setToolbarColors( R.color.white_color, @@ -86,48 +78,15 @@ class HistoryUIView( } private fun setUIForNormalMode(isEmpty: Boolean) { - (activity as? AppCompatActivity)?.title = context.getString(R.string.library_history) - delete_history_button?.visibility = if (isEmpty) View.GONE else View.VISIBLE - history_empty_view.visibility = if (isEmpty) View.VISIBLE else View.GONE + activity?.title = context.getString(R.string.library_history) + delete_history_button?.isVisible = !isEmpty + history_empty_view.isVisible = isEmpty setToolbarColors( R.attr.primaryText.getColorIntFromAttr(context!!), R.attr.foundation.getColorIntFromAttr(context) ) } - private fun setToolbarColors(foreground: Int, background: Int) { - val toolbar = (activity as AppCompatActivity).findViewById(R.id.navigationToolbar) - val colorFilter = PorterDuffColorFilter(ContextCompat.getColor(context, foreground), SRC_IN) - toolbar.setBackgroundColor(ContextCompat.getColor(context, background)) - toolbar.setTitleTextColor(ContextCompat.getColor(context, foreground)) - - themeToolbar( - toolbar, foreground, - background, colorFilter - ) - } - - private fun themeToolbar( - toolbar: androidx.appcompat.widget.Toolbar, - textColor: Int, - backgroundColor: Int, - colorFilter: PorterDuffColorFilter? = null - ) { - toolbar.setTitleTextColor(ContextCompat.getColor(context!!, textColor)) - toolbar.setBackgroundColor(ContextCompat.getColor(context, backgroundColor)) - - if (colorFilter == null) { - return - } - - toolbar.overflowIcon?.colorFilter = colorFilter - (0 until toolbar.childCount).forEach { - when (val item = toolbar.getChildAt(it)) { - is ImageButton -> item.drawable.colorFilter = colorFilter - } - } - } - override fun onBackPressed(): Boolean { return when (mode) { is HistoryState.Mode.Editing -> {