@ -63,10 +63,11 @@ class DefaultToolbarMenu(
val isPinningSupported : Boolean
) : ToolbarMenu {
private var currentUrlIs Bookmarked = false
private var isCurrentUrl Bookmarked = false
private var isBookmarkedJob : Job ? = null
private val selectedSession : TabSessionState ? get ( ) = store . state . selectedTab
private val isTopToolbarSelected = shouldReverseItems
private val selectedSession : TabSessionState ?
get ( ) = store . state . selectedTab
override val menuBuilder by lazy {
WebExtensionBrowserMenuBuilder (
@ -146,6 +147,9 @@ class DefaultToolbarMenu(
registerForIsBookmarkedUpdates ( )
if ( FeatureFlags . toolbarMenuFeature ) {
BrowserMenuItemToolbar ( listOf ( back , forward , share , refresh ) )
} else {
val bookmark = BrowserMenuItemToolbar . TwoStateButton (
primaryImageResource = R . drawable . ic _bookmark _filled ,
primaryContentDescription = context . getString ( R . string . browser _menu _edit _bookmark ) ,
@ -153,18 +157,19 @@ class DefaultToolbarMenu(
// TwoStateButton.isInPrimaryState must be synchronous, and checking bookmark state is
// relatively slow. The best we can do here is periodically compute and cache a new "is
// bookmarked" state, and use that whenever the menu has been opened.
isInPrimaryState = { currentUrlIs Bookmarked } ,
isInPrimaryState = { isCurrentUrl Bookmarked } ,
secondaryImageResource = R . drawable . ic _bookmark _outline ,
secondaryContentDescription = context . getString ( R . string . browser _menu _bookmark ) ,
secondaryImageTintResource = primaryTextColor ( ) ,
disableInSecondaryState = false
) {
if ( ! currentUrlIsBookmarked ) currentUrlIs Bookmarked = true
if ( !is CurrentUrlBookmarked ) isCurrentUrl Bookmarked = true
onItemTapped . invoke ( ToolbarMenu . Item . Bookmark )
}
BrowserMenuItemToolbar ( listOf ( back , forward , bookmark , share , refresh ) )
}
}
// Predicates that need to be repeatedly called as the session changes
private fun canAddToHomescreen ( ) : Boolean =
@ -469,7 +474,9 @@ class DefaultToolbarMenu(
onItemTapped . invoke ( ToolbarMenu . Item . Settings )
}
val menuItems = listOfNotNull (
val menuItems =
listOfNotNull (
if ( isTopToolbarSelected ) menuToolbar else null ,
newTabItem ,
BrowserMenuDivider ( ) ,
bookmarksItem ,
@ -486,8 +493,8 @@ class DefaultToolbarMenu(
saveToCollectionItem ,
BrowserMenuDivider ( ) ,
settingsItem ,
BrowserMenuDivider ( ) ,
menuToolbar
if ( isTopToolbarSelected ) null else BrowserMenuDivider ( ) ,
if ( isTopToolbarSelected ) null else menuToolbar
)
menuItems
@ -512,7 +519,7 @@ class DefaultToolbarMenu(
)
}
. collect {
currentUrlIs Bookmarked = false
isCurrentUrl Bookmarked = false
updateCurrentUrlIsBookmarked ( it . content . url )
}
}
@ -522,7 +529,7 @@ class DefaultToolbarMenu(
internal fun updateCurrentUrlIsBookmarked ( newUrl : String ) {
isBookmarkedJob ?. cancel ( )
isBookmarkedJob = lifecycleOwner . lifecycleScope . launch {
currentUrlIs Bookmarked = bookmarksStorage
isCurrentUrl Bookmarked = bookmarksStorage
. getBookmarksWithUrl ( newUrl )
. any { it . url == newUrl }
}