From a5d911a09aa4c3f9fe44782f62b07dcc039e1987 Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Tue, 14 Jul 2020 14:15:40 -0700 Subject: [PATCH] For #2681 - Adds ability to bypass cache when reloading --- .../toolbar/BrowserToolbarController.kt | 13 ++++++++++-- .../components/toolbar/BrowserToolbarView.kt | 16 +++++++++++++- .../components/toolbar/DefaultToolbarMenu.kt | 5 +++-- .../fenix/components/toolbar/ToolbarMenu.kt | 2 +- .../fenix/customtabs/CustomTabToolbarMenu.kt | 5 +++-- .../DefaultBrowserToolbarControllerTest.kt | 21 ++++++++++++++++++- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt index 41795a4462f4..7a0d6e56235c 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarController.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.launch import mozilla.appservices.places.BookmarkRoot import mozilla.components.browser.session.Session import mozilla.components.browser.session.SessionManager +import mozilla.components.concept.engine.EngineSession.LoadUrlFlags import mozilla.components.concept.engine.EngineView import mozilla.components.concept.engine.prompt.ShareData import mozilla.components.feature.session.SessionFeature @@ -171,7 +172,15 @@ class DefaultBrowserToolbarController( Do exhaustive when (item) { ToolbarMenu.Item.Back -> sessionUseCases.goBack.invoke(currentSession) ToolbarMenu.Item.Forward -> sessionUseCases.goForward.invoke(currentSession) - ToolbarMenu.Item.Reload -> sessionUseCases.reload.invoke(currentSession) + is ToolbarMenu.Item.Reload -> { + val flags = if (item.bypassCache) { + LoadUrlFlags.select(LoadUrlFlags.BYPASS_CACHE) + } else { + LoadUrlFlags.none() + } + + sessionUseCases.reload.invoke(currentSession, flags = flags) + } ToolbarMenu.Item.Stop -> sessionUseCases.stopLoading.invoke(currentSession) ToolbarMenu.Item.Settings -> browserAnimator.captureEngineViewAndDrawStatically { val directions = BrowserFragmentDirections.actionBrowserFragmentToSettingsFragment() @@ -334,7 +343,7 @@ class DefaultBrowserToolbarController( val eventItem = when (item) { ToolbarMenu.Item.Back -> Event.BrowserMenuItemTapped.Item.BACK ToolbarMenu.Item.Forward -> Event.BrowserMenuItemTapped.Item.FORWARD - ToolbarMenu.Item.Reload -> Event.BrowserMenuItemTapped.Item.RELOAD + is ToolbarMenu.Item.Reload -> Event.BrowserMenuItemTapped.Item.RELOAD ToolbarMenu.Item.Stop -> Event.BrowserMenuItemTapped.Item.STOP ToolbarMenu.Item.Settings -> Event.BrowserMenuItemTapped.Item.SETTINGS is ToolbarMenu.Item.RequestDesktop -> diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarView.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarView.kt index c5011f1e5862..2ed2d0f5fb20 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarView.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/BrowserToolbarView.kt @@ -7,6 +7,7 @@ package org.mozilla.fenix.components.toolbar import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.view.Gravity +import android.view.HapticFeedbackConstants import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -56,6 +57,7 @@ interface BrowserToolbarViewInteractor { fun onScrolled(offset: Int) fun onReaderModePressed(enabled: Boolean) } + @SuppressWarnings("LargeClass") class BrowserToolbarView( private val container: ViewGroup, @@ -209,6 +211,7 @@ class BrowserToolbarView( customTabSession?.id, shouldReverseItems = !shouldUseBottomToolbar, onItemTapped = { + it.performHapticIfNeeded(view) interactor.onBrowserToolbarMenuItemTapped(it) } ) @@ -217,7 +220,10 @@ class BrowserToolbarView( context = this, hasAccountProblem = components.backgroundServices.accountManager.accountNeedsReauth(), shouldReverseItems = !shouldUseBottomToolbar, - onItemTapped = { interactor.onBrowserToolbarMenuItemTapped(it) }, + onItemTapped = { + it.performHapticIfNeeded(view) + interactor.onBrowserToolbarMenuItemTapped(it) + }, lifecycleOwner = lifecycleOwner, sessionManager = sessionManager, store = components.core.store, @@ -307,4 +313,12 @@ class BrowserToolbarView( } } } + + private fun ToolbarMenu.Item.performHapticIfNeeded(view: View) { + (this as? ToolbarMenu.Item.Reload)?.also { reload -> + if (reload.bypassCache) { + view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + } + } + } } diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/DefaultToolbarMenu.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/DefaultToolbarMenu.kt index 427ec03068f5..437bb1bec29c 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/DefaultToolbarMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/DefaultToolbarMenu.kt @@ -98,12 +98,13 @@ class DefaultToolbarMenu( secondaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_stop, secondaryContentDescription = context.getString(R.string.browser_menu_stop), secondaryImageTintResource = primaryTextColor(), - disableInSecondaryState = false + disableInSecondaryState = false, + longClickListener = { onItemTapped.invoke(ToolbarMenu.Item.Reload(bypassCache = true)) } ) { if (session?.loading == true) { onItemTapped.invoke(ToolbarMenu.Item.Stop) } else { - onItemTapped.invoke(ToolbarMenu.Item.Reload) + onItemTapped.invoke(ToolbarMenu.Item.Reload(bypassCache = false)) } } diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarMenu.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarMenu.kt index db8dbbe149dd..ba9569de3845 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarMenu.kt @@ -15,7 +15,7 @@ interface ToolbarMenu { object Share : Item() object Back : Item() object Forward : Item() - object Reload : Item() + data class Reload(val bypassCache: Boolean) : Item() object Stop : Item() object OpenInFenix : Item() object SaveToCollection : Item() diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabToolbarMenu.kt b/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabToolbarMenu.kt index 32a33b8d4a56..a5ed1ef57a45 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabToolbarMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabToolbarMenu.kt @@ -90,12 +90,13 @@ class CustomTabToolbarMenu( secondaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_stop, secondaryContentDescription = context.getString(R.string.browser_menu_stop), secondaryImageTintResource = primaryTextColor(), - disableInSecondaryState = false + disableInSecondaryState = false, + longClickListener = { onItemTapped.invoke(ToolbarMenu.Item.Reload(bypassCache = true)) } ) { if (session?.loading == true) { onItemTapped.invoke(ToolbarMenu.Item.Stop) } else { - onItemTapped.invoke(ToolbarMenu.Item.Reload) + onItemTapped.invoke(ToolbarMenu.Item.Reload(bypassCache = false)) } } diff --git a/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarControllerTest.kt b/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarControllerTest.kt index 2a1cbd089ba2..1ad47107fa76 100644 --- a/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/toolbar/DefaultBrowserToolbarControllerTest.kt @@ -30,6 +30,7 @@ import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.ReaderState import mozilla.components.browser.state.state.createTab import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.engine.EngineSession import mozilla.components.concept.engine.EngineView import mozilla.components.concept.engine.prompt.ShareData import mozilla.components.feature.search.SearchUseCases @@ -231,7 +232,7 @@ class DefaultBrowserToolbarControllerTest { @Test fun handleToolbarReloadPress() = runBlockingTest { - val item = ToolbarMenu.Item.Reload + val item = ToolbarMenu.Item.Reload(false) every { activity.components.useCases.sessionUseCases } returns sessionUseCases @@ -242,6 +243,24 @@ class DefaultBrowserToolbarControllerTest { verify { sessionUseCases.reload(currentSession) } } + @Test + fun handleToolbarReloadLongPress() = runBlockingTest { + val item = ToolbarMenu.Item.Reload(true) + + every { activity.components.useCases.sessionUseCases } returns sessionUseCases + + val controller = createController(scope = this) + controller.handleToolbarItemInteraction(item) + + verify { metrics.track(Event.BrowserMenuItemTapped(Event.BrowserMenuItemTapped.Item.RELOAD)) } + verify { + sessionUseCases.reload( + currentSession, + EngineSession.LoadUrlFlags.select(EngineSession.LoadUrlFlags.BYPASS_CACHE) + ) + } + } + @Test fun handleToolbarStopPress() = runBlockingTest { val item = ToolbarMenu.Item.Stop