Skip to content

Commit

Permalink
For mozilla-mobile#8800 - dismiss all highlights when menu is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
BranescuMihai committed Mar 5, 2020
1 parent 6099834 commit 1b85850
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ open class BrowserInteractor(
override fun onBrowserToolbarMenuItemTapped(item: ToolbarMenu.Item) {
browserToolbarController.handleToolbarItemInteraction(item)
}

override fun onBrowserMenuDismissed(lowPrioHighlightItems: List<ToolbarMenu.Item>) {
browserToolbarController.handleBrowserMenuDismissed(lowPrioHighlightItems)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface BrowserToolbarController {
fun handleToolbarItemInteraction(item: ToolbarMenu.Item)
fun handleToolbarClick()
fun handleTabCounterClick()
fun handleBrowserMenuDismissed(lowPrioHighlightItems: List<ToolbarMenu.Item>)
}

typealias onComplete = () -> Unit
Expand Down Expand Up @@ -124,6 +125,17 @@ class DefaultBrowserToolbarController(
animateTabAndNavigateHome()
}

override fun handleBrowserMenuDismissed(lowPrioHighlightItems: List<ToolbarMenu.Item>) {
lowPrioHighlightItems.forEach {
when (it) {
ToolbarMenu.Item.AddToHomeScreen -> activity.settings().installPwaOpened = true
is ToolbarMenu.Item.ReaderMode -> activity.settings().readerModeOpened = true
ToolbarMenu.Item.OpenInApp -> activity.settings().openInAppOpened = true
else -> {}
}
}
}

@ExperimentalCoroutinesApi
@SuppressWarnings("ComplexMethod", "LongMethod")
override fun handleToolbarItemInteraction(item: ToolbarMenu.Item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.*
import kotlinx.android.synthetic.main.component_browser_top_toolbar.view.*
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.copy
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.paste
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.paste_and_go
import kotlinx.android.synthetic.main.component_browser_top_toolbar.view.app_bar
import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider
import mozilla.components.browser.session.Session
import mozilla.components.browser.toolbar.BrowserToolbar
Expand All @@ -39,6 +41,7 @@ interface BrowserToolbarViewInteractor {
fun onBrowserToolbarClicked()
fun onBrowserToolbarMenuItemTapped(item: ToolbarMenu.Item)
fun onTabCounterClicked()
fun onBrowserMenuDismissed(lowPrioHighlightItems: List<ToolbarMenu.Item>)
}

class BrowserToolbarView(
Expand Down Expand Up @@ -173,8 +176,9 @@ class BrowserToolbarView(
display.hint = context.getString(R.string.search_hint)
}

val menuToolbar = if (isCustomTabSession) {
CustomTabToolbarMenu(
val menuToolbar: ToolbarMenu
if (isCustomTabSession) {
menuToolbar = CustomTabToolbarMenu(
this,
sessionManager,
customTabSession?.id,
Expand All @@ -184,7 +188,7 @@ class BrowserToolbarView(
}
)
} else {
DefaultToolbarMenu(
menuToolbar = DefaultToolbarMenu(
context = this,
hasAccountProblem = components.backgroundServices.accountManager.accountNeedsReauth(),
shouldReverseItems = !shouldUseBottomToolbar,
Expand All @@ -193,6 +197,10 @@ class BrowserToolbarView(
sessionManager = sessionManager,
bookmarksStorage = bookmarkStorage
)
view.display.setMenuDismissAction {
interactor.onBrowserMenuDismissed(menuToolbar.getLowPrioHighlightItems())
view.invalidateActions()
}
}

toolbarIntegration = if (customTabSession != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ class DefaultToolbarMenu(
BrowserMenuItemToolbar(listOf(forward, bookmark, share, refresh))
}

internal fun getLowPrioHighlightItems(): List<ToolbarMenu.Item> {
val lowPrioHighlightItems: MutableList<ToolbarMenu.Item> = mutableListOf()
if (shouldShowAddToHomescreen() && addToHomescreen.isHighlighted()) {
lowPrioHighlightItems.add(ToolbarMenu.Item.AddToHomeScreen)
}
if (shouldShowReaderMode() && readerMode.isHighlighted()) {
lowPrioHighlightItems.add(ToolbarMenu.Item.ReaderMode(false))
}
if (shouldShowOpenInApp() && openInApp.isHighlighted()) {
lowPrioHighlightItems.add(ToolbarMenu.Item.OpenInApp)
}
return lowPrioHighlightItems
}

// Predicates that need to be repeatedly called as the session changes
private fun shouldShowAddToHomescreen(): Boolean =
session != null && context.components.useCases.webAppUseCases.isPinningSupported()

private fun shouldShowReaderMode(): Boolean = session?.readerable ?: false

private fun shouldShowOpenInApp(): Boolean = session?.let { session ->
val appLink = context.components.useCases.appLinksUseCases.appLinkRedirect
appLink(session.url).hasExternalApp()
} ?: false

private fun shouldShowReaderAppearance(): Boolean = session?.readerMode ?: false
// End of predicates //

private val menuItems by lazy {
// Predicates that are called once, during screen init
val shouldShowSaveToCollection = (context.asActivity() as? HomeActivity)
Expand All @@ -144,16 +172,6 @@ class DefaultToolbarMenu(
ReleaseChannel.FennecProduction
)

// Predicates that need to be repeatedly called as the session changes
fun shouldShowAddToHomescreen(): Boolean =
session != null && context.components.useCases.webAppUseCases.isPinningSupported()
fun shouldShowReaderMode(): Boolean = session?.readerable ?: false
fun shouldShowOpenInApp(): Boolean = session?.let { session ->
val appLink = context.components.useCases.appLinksUseCases.appLinkRedirect
appLink(session.url).hasExternalApp()
} ?: false
fun shouldShowReaderAppearance(): Boolean = session?.readerMode ?: false

val menuItems = listOfNotNull(
help,
settings,
Expand Down

0 comments on commit 1b85850

Please sign in to comment.