Skip to content

Commit

Permalink
For mozilla-mobile#2681 - Adds ability to bypass cache when reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
boek committed Jul 22, 2020
1 parent 66d8591 commit a5d911a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +57,7 @@ interface BrowserToolbarViewInteractor {
fun onScrolled(offset: Int)
fun onReaderModePressed(enabled: Boolean)
}

@SuppressWarnings("LargeClass")
class BrowserToolbarView(
private val container: ViewGroup,
Expand Down Expand Up @@ -209,6 +211,7 @@ class BrowserToolbarView(
customTabSession?.id,
shouldReverseItems = !shouldUseBottomToolbar,
onItemTapped = {
it.performHapticIfNeeded(view)
interactor.onBrowserToolbarMenuItemTapped(it)
}
)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit a5d911a

Please sign in to comment.