Merge some library UIView code for toolbar
parent
447123367a
commit
52d4c43916
|
@ -5,8 +5,19 @@
|
||||||
package org.mozilla.fenix.ext
|
package org.mozilla.fenix.ext
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.annotation.ColorInt
|
||||||
|
import androidx.annotation.ColorRes
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import org.mozilla.fenix.ThemeManager
|
import org.mozilla.fenix.ThemeManager
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color resource corresponding to the attribute.
|
||||||
|
*/
|
||||||
|
@ColorRes
|
||||||
fun Int.getColorIntFromAttr(context: Context): Int = ThemeManager.resolveAttribute(this, context)
|
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))
|
fun Int.getColorFromAttr(context: Context): Int = ContextCompat.getColor(context, this.getColorIntFromAttr(context))
|
||||||
|
|
|
@ -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<S : ViewState, A : Action, C : Change>(
|
||||||
|
container: ViewGroup,
|
||||||
|
actionEmitter: Observer<A>,
|
||||||
|
changesObservable: Observable<C>
|
||||||
|
) : UIView<S, A, C>(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<Toolbar>(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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,17 +4,9 @@
|
||||||
|
|
||||||
package org.mozilla.fenix.library.bookmarks
|
package org.mozilla.fenix.library.bookmarks
|
||||||
|
|
||||||
import android.graphics.PorterDuff.Mode.SRC_IN
|
|
||||||
import android.graphics.PorterDuffColorFilter
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.ImageButton
|
|
||||||
import android.widget.LinearLayout
|
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.Observable
|
||||||
import io.reactivex.Observer
|
import io.reactivex.Observer
|
||||||
import io.reactivex.functions.Consumer
|
import io.reactivex.functions.Consumer
|
||||||
|
@ -23,16 +15,15 @@ import mozilla.appservices.places.BookmarkRoot
|
||||||
import mozilla.components.concept.storage.BookmarkNode
|
import mozilla.components.concept.storage.BookmarkNode
|
||||||
import mozilla.components.support.base.feature.BackHandler
|
import mozilla.components.support.base.feature.BackHandler
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import org.mozilla.fenix.ext.asActivity
|
|
||||||
import org.mozilla.fenix.ext.getColorIntFromAttr
|
import org.mozilla.fenix.ext.getColorIntFromAttr
|
||||||
import org.mozilla.fenix.mvi.UIView
|
import org.mozilla.fenix.library.LibraryPageUIView
|
||||||
|
|
||||||
class BookmarkUIView(
|
class BookmarkUIView(
|
||||||
container: ViewGroup,
|
container: ViewGroup,
|
||||||
actionEmitter: Observer<BookmarkAction>,
|
actionEmitter: Observer<BookmarkAction>,
|
||||||
changesObservable: Observable<BookmarkChange>
|
changesObservable: Observable<BookmarkChange>
|
||||||
) :
|
) :
|
||||||
UIView<BookmarkState, BookmarkAction, BookmarkChange>(container, actionEmitter, changesObservable),
|
LibraryPageUIView<BookmarkState, BookmarkAction, BookmarkChange>(container, actionEmitter, changesObservable),
|
||||||
BackHandler {
|
BackHandler {
|
||||||
|
|
||||||
var mode: BookmarkState.Mode = BookmarkState.Mode.Normal
|
var mode: BookmarkState.Mode = BookmarkState.Mode.Normal
|
||||||
|
@ -46,8 +37,6 @@ class BookmarkUIView(
|
||||||
.inflate(R.layout.component_bookmark, container, true) as LinearLayout
|
.inflate(R.layout.component_bookmark, container, true) as LinearLayout
|
||||||
|
|
||||||
private val bookmarkAdapter: BookmarkAdapter
|
private val bookmarkAdapter: BookmarkAdapter
|
||||||
private val context = container.context
|
|
||||||
private val activity = context?.asActivity()
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
view.bookmark_list.apply {
|
view.bookmark_list.apply {
|
||||||
|
@ -87,21 +76,6 @@ class BookmarkUIView(
|
||||||
|
|
||||||
fun getSelected(): Set<BookmarkNode> = bookmarkAdapter.selected
|
fun getSelected(): Set<BookmarkNode> = bookmarkAdapter.selected
|
||||||
|
|
||||||
private fun setToolbarColors(foreground: Int, background: Int) {
|
|
||||||
val toolbar = activity?.findViewById<Toolbar>(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(
|
private fun setUIForSelectingMode(
|
||||||
root: BookmarkNode?,
|
root: BookmarkNode?,
|
||||||
mode: BookmarkState.Mode.Selecting
|
mode: BookmarkState.Mode.Selecting
|
||||||
|
@ -125,62 +99,9 @@ class BookmarkUIView(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setTitle(root: BookmarkNode?) {
|
private fun setTitle(root: BookmarkNode?) {
|
||||||
(activity as? AppCompatActivity)?.title =
|
activity?.title = when (root?.guid) {
|
||||||
if (root?.guid in setOf(
|
BookmarkRoot.Mobile.id, null -> context.getString(R.string.library_bookmarks)
|
||||||
BookmarkRoot.Mobile.id,
|
else -> root.title
|
||||||
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 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,11 @@
|
||||||
|
|
||||||
package org.mozilla.fenix.library.history
|
package org.mozilla.fenix.library.history
|
||||||
|
|
||||||
import android.graphics.PorterDuff.Mode.SRC_IN
|
|
||||||
import android.graphics.PorterDuffColorFilter
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
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.constraintlayout.widget.ConstraintLayout
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.view.isVisible
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.Observer
|
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 kotlinx.android.synthetic.main.delete_history_button.*
|
||||||
import mozilla.components.support.base.feature.BackHandler
|
import mozilla.components.support.base.feature.BackHandler
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import org.mozilla.fenix.ext.asActivity
|
|
||||||
import org.mozilla.fenix.ext.getColorIntFromAttr
|
import org.mozilla.fenix.ext.getColorIntFromAttr
|
||||||
import org.mozilla.fenix.mvi.UIView
|
import org.mozilla.fenix.library.LibraryPageUIView
|
||||||
|
|
||||||
class HistoryUIView(
|
class HistoryUIView(
|
||||||
container: ViewGroup,
|
container: ViewGroup,
|
||||||
actionEmitter: Observer<HistoryAction>,
|
actionEmitter: Observer<HistoryAction>,
|
||||||
changesObservable: Observable<HistoryChange>
|
changesObservable: Observable<HistoryChange>
|
||||||
) :
|
) :
|
||||||
UIView<HistoryState, HistoryAction, HistoryChange>(container, actionEmitter, changesObservable),
|
LibraryPageUIView<HistoryState, HistoryAction, HistoryChange>(container, actionEmitter, changesObservable),
|
||||||
BackHandler {
|
BackHandler {
|
||||||
|
|
||||||
var mode: HistoryState.Mode = HistoryState.Mode.Normal
|
var mode: HistoryState.Mode = HistoryState.Mode.Normal
|
||||||
|
@ -40,8 +34,6 @@ class HistoryUIView(
|
||||||
|
|
||||||
private val historyAdapter: HistoryAdapter
|
private val historyAdapter: HistoryAdapter
|
||||||
private var items: List<HistoryItem> = listOf()
|
private var items: List<HistoryItem> = listOf()
|
||||||
private val context = container.context
|
|
||||||
private val activity = context?.asActivity()
|
|
||||||
|
|
||||||
fun getSelected(): List<HistoryItem> = historyAdapter.selected
|
fun getSelected(): List<HistoryItem> = historyAdapter.selected
|
||||||
|
|
||||||
|
@ -77,7 +69,7 @@ class HistoryUIView(
|
||||||
private fun setUIForSelectingMode(
|
private fun setUIForSelectingMode(
|
||||||
mode: HistoryState.Mode.Editing
|
mode: HistoryState.Mode.Editing
|
||||||
) {
|
) {
|
||||||
(activity as? AppCompatActivity)?.title =
|
activity?.title =
|
||||||
context.getString(R.string.history_multi_select_title, mode.selectedItems.size)
|
context.getString(R.string.history_multi_select_title, mode.selectedItems.size)
|
||||||
setToolbarColors(
|
setToolbarColors(
|
||||||
R.color.white_color,
|
R.color.white_color,
|
||||||
|
@ -86,48 +78,15 @@ class HistoryUIView(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUIForNormalMode(isEmpty: Boolean) {
|
private fun setUIForNormalMode(isEmpty: Boolean) {
|
||||||
(activity as? AppCompatActivity)?.title = context.getString(R.string.library_history)
|
activity?.title = context.getString(R.string.library_history)
|
||||||
delete_history_button?.visibility = if (isEmpty) View.GONE else View.VISIBLE
|
delete_history_button?.isVisible = !isEmpty
|
||||||
history_empty_view.visibility = if (isEmpty) View.VISIBLE else View.GONE
|
history_empty_view.isVisible = isEmpty
|
||||||
setToolbarColors(
|
setToolbarColors(
|
||||||
R.attr.primaryText.getColorIntFromAttr(context!!),
|
R.attr.primaryText.getColorIntFromAttr(context!!),
|
||||||
R.attr.foundation.getColorIntFromAttr(context)
|
R.attr.foundation.getColorIntFromAttr(context)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setToolbarColors(foreground: Int, background: Int) {
|
|
||||||
val toolbar = (activity as AppCompatActivity).findViewById<Toolbar>(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 {
|
override fun onBackPressed(): Boolean {
|
||||||
return when (mode) {
|
return when (mode) {
|
||||||
is HistoryState.Mode.Editing -> {
|
is HistoryState.Mode.Editing -> {
|
||||||
|
|
Loading…
Reference in New Issue