Commit c9b8f57f authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Refactor BrowserToolbarMenuController to use browser store

parent b6ac5079
...@@ -318,17 +318,17 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit ...@@ -318,17 +318,17 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit
} }
) )
val browserToolbarMenuController = DefaultBrowserToolbarMenuController( val browserToolbarMenuController = DefaultBrowserToolbarMenuController(
store = store,
activity = activity, activity = activity,
navController = findNavController(), navController = findNavController(),
metrics = requireComponents.analytics.metrics, metrics = requireComponents.analytics.metrics,
settings = context.settings(), settings = context.settings(),
readerModeController = readerMenuController, readerModeController = readerMenuController,
sessionManager = requireComponents.core.sessionManager,
sessionFeature = sessionFeature, sessionFeature = sessionFeature,
findInPageLauncher = { findInPageIntegration.withFeature { it.launch() } }, findInPageLauncher = { findInPageIntegration.withFeature { it.launch() } },
swipeRefresh = swipeRefresh, swipeRefresh = swipeRefresh,
browserAnimator = browserAnimator, browserAnimator = browserAnimator,
customTabSession = customTabSessionId?.let { sessionManager.findSessionById(it) }, customTabSessionId = customTabSessionId,
openInFenixIntent = openInFenixIntent, openInFenixIntent = openInFenixIntent,
bookmarkTapped = { url: String, title: String -> bookmarkTapped = { url: String, title: String ->
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
......
...@@ -15,9 +15,10 @@ import kotlinx.coroutines.Dispatchers ...@@ -15,9 +15,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import mozilla.appservices.places.BookmarkRoot import mozilla.appservices.places.BookmarkRoot
import mozilla.components.browser.session.Session import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.state.selector.findTab import mozilla.components.browser.state.selector.findTab
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.store.BrowserStore import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.EngineSession.LoadUrlFlags import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
import mozilla.components.concept.engine.prompt.ShareData import mozilla.components.concept.engine.prompt.ShareData
...@@ -53,17 +54,17 @@ interface BrowserToolbarMenuController { ...@@ -53,17 +54,17 @@ interface BrowserToolbarMenuController {
@Suppress("LargeClass", "ForbiddenComment") @Suppress("LargeClass", "ForbiddenComment")
class DefaultBrowserToolbarMenuController( class DefaultBrowserToolbarMenuController(
private val store: BrowserStore,
private val activity: HomeActivity, private val activity: HomeActivity,
private val navController: NavController, private val navController: NavController,
private val metrics: MetricController, private val metrics: MetricController,
private val settings: Settings, private val settings: Settings,
private val readerModeController: ReaderModeController, private val readerModeController: ReaderModeController,
private val sessionFeature: ViewBoundFeatureWrapper<SessionFeature>, private val sessionFeature: ViewBoundFeatureWrapper<SessionFeature>,
private val sessionManager: SessionManager,
private val findInPageLauncher: () -> Unit, private val findInPageLauncher: () -> Unit,
private val browserAnimator: BrowserAnimator, private val browserAnimator: BrowserAnimator,
private val swipeRefresh: SwipeRefreshLayout, private val swipeRefresh: SwipeRefreshLayout,
private val customTabSession: Session?, private val customTabSessionId: String?,
private val openInFenixIntent: Intent, private val openInFenixIntent: Intent,
private val bookmarkTapped: (String, String) -> Unit, private val bookmarkTapped: (String, String) -> Unit,
private val scope: CoroutineScope, private val scope: CoroutineScope,
...@@ -73,7 +74,7 @@ class DefaultBrowserToolbarMenuController( ...@@ -73,7 +74,7 @@ class DefaultBrowserToolbarMenuController(
) : BrowserToolbarMenuController { ) : BrowserToolbarMenuController {
private val currentSession private val currentSession
get() = customTabSession ?: sessionManager.selectedSession get() = store.state.findCustomTabOrSelectedTab(customTabSessionId)
// We hold onto a reference of the inner scope so that we can override this with the // We hold onto a reference of the inner scope so that we can override this with the
// TestCoroutineScope to ensure sequential execution. If we didn't have this, our tests // TestCoroutineScope to ensure sequential execution. If we didn't have this, our tests
...@@ -84,6 +85,7 @@ class DefaultBrowserToolbarMenuController( ...@@ -84,6 +85,7 @@ class DefaultBrowserToolbarMenuController(
@Suppress("ComplexMethod", "LongMethod") @Suppress("ComplexMethod", "LongMethod")
override fun handleToolbarItemInteraction(item: ToolbarMenu.Item) { override fun handleToolbarItemInteraction(item: ToolbarMenu.Item) {
val sessionUseCases = activity.components.useCases.sessionUseCases val sessionUseCases = activity.components.useCases.sessionUseCases
val customTabUseCases = activity.components.useCases.customTabsUseCases
trackToolbarItemInteraction(item) trackToolbarItemInteraction(item)
Do exhaustive when (item) { Do exhaustive when (item) {
...@@ -104,26 +106,27 @@ class DefaultBrowserToolbarMenuController( ...@@ -104,26 +106,27 @@ class DefaultBrowserToolbarMenuController(
} }
} }
is ToolbarMenu.Item.OpenInFenix -> { is ToolbarMenu.Item.OpenInFenix -> {
// Stop the SessionFeature from updating the EngineView and let it release the session customTabSessionId?.let {
// from the EngineView so that it can immediately be rendered by a different view once // Stop the SessionFeature from updating the EngineView and let it release the session
// we switch to the actual browser. // from the EngineView so that it can immediately be rendered by a different view once
sessionFeature.get()?.release() // we switch to the actual browser.
sessionFeature.get()?.release()
// Strip the CustomTabConfig to turn this Session into a regular tab and then select it // Turn this Session into a regular tab and then select it
customTabSession!!.customTabConfig = null customTabUseCases.migrate(customTabSessionId, select = true)
sessionManager.select(customTabSession)
// Switch to the actual browser which should now display our new selected session // Switch to the actual browser which should now display our new selected session
activity.startActivity(openInFenixIntent.apply { activity.startActivity(openInFenixIntent.apply {
// We never want to launch the browser in the same task as the external app // We never want to launch the browser in the same task as the external app
// activity. So we force a new task here. IntentReceiverActivity will do the // activity. So we force a new task here. IntentReceiverActivity will do the
// right thing and take care of routing to an already existing browser and avoid // right thing and take care of routing to an already existing browser and avoid
// cloning a new one. // cloning a new one.
flags = flags or Intent.FLAG_ACTIVITY_NEW_TASK flags = flags or Intent.FLAG_ACTIVITY_NEW_TASK
}) })
// Close this activity (and the task) since it is no longer displaying any session // Close this activity (and the task) since it is no longer displaying any session
activity.finishAndRemoveTask() activity.finishAndRemoveTask()
}
} }
is ToolbarMenu.Item.Quit -> { is ToolbarMenu.Item.Quit -> {
// We need to show the snackbar while the browsing data is deleting (if "Delete // We need to show the snackbar while the browsing data is deleting (if "Delete
...@@ -150,7 +153,7 @@ class DefaultBrowserToolbarMenuController( ...@@ -150,7 +153,7 @@ class DefaultBrowserToolbarMenuController(
val appLinksUseCases = activity.components.useCases.appLinksUseCases val appLinksUseCases = activity.components.useCases.appLinksUseCases
val getRedirect = appLinksUseCases.appLinkRedirect val getRedirect = appLinksUseCases.appLinkRedirect
currentSession?.let { currentSession?.let {
val redirect = getRedirect.invoke(it.url) val redirect = getRedirect.invoke(it.content.url)
redirect.appIntent?.flags = Intent.FLAG_ACTIVITY_NEW_TASK redirect.appIntent?.flags = Intent.FLAG_ACTIVITY_NEW_TASK
appLinksUseCases.openAppLink.invoke(redirect.appIntent) appLinksUseCases.openAppLink.invoke(redirect.appIntent)
} }
...@@ -161,22 +164,26 @@ class DefaultBrowserToolbarMenuController( ...@@ -161,22 +164,26 @@ class DefaultBrowserToolbarMenuController(
if (item.viewHistory) { if (item.viewHistory) {
navController.navigate( navController.navigate(
BrowserFragmentDirections.actionGlobalTabHistoryDialogFragment( BrowserFragmentDirections.actionGlobalTabHistoryDialogFragment(
activeSessionId = customTabSession?.id activeSessionId = customTabSessionId
) )
) )
} else { } else {
sessionUseCases.goBack.invoke(currentSession) currentSession?.let {
sessionUseCases.goBack.invoke(it.id)
}
} }
} }
is ToolbarMenu.Item.Forward -> { is ToolbarMenu.Item.Forward -> {
if (item.viewHistory) { if (item.viewHistory) {
navController.navigate( navController.navigate(
BrowserFragmentDirections.actionGlobalTabHistoryDialogFragment( BrowserFragmentDirections.actionGlobalTabHistoryDialogFragment(
activeSessionId = customTabSession?.id activeSessionId = customTabSessionId
) )
) )
} else { } else {
sessionUseCases.goForward.invoke(currentSession) currentSession?.let {
sessionUseCases.goForward.invoke(it.id)
}
} }
} }
is ToolbarMenu.Item.Reload -> { is ToolbarMenu.Item.Reload -> {
...@@ -186,15 +193,21 @@ class DefaultBrowserToolbarMenuController( ...@@ -186,15 +193,21 @@ class DefaultBrowserToolbarMenuController(
LoadUrlFlags.none() LoadUrlFlags.none()
} }
sessionUseCases.reload.invoke(currentSession, flags = flags) currentSession?.let {
sessionUseCases.reload.invoke(it.id, flags = flags)
}
}
is ToolbarMenu.Item.Stop -> {
currentSession?.let {
sessionUseCases.stopLoading.invoke(it.id)
}
} }
is ToolbarMenu.Item.Stop -> sessionUseCases.stopLoading.invoke(currentSession)
is ToolbarMenu.Item.Share -> { is ToolbarMenu.Item.Share -> {
val directions = NavGraphDirections.actionGlobalShareFragment( val directions = NavGraphDirections.actionGlobalShareFragment(
data = arrayOf( data = arrayOf(
ShareData( ShareData(
url = getProperUrl(currentSession), url = getProperUrl(currentSession),
title = currentSession?.title title = currentSession?.content?.title
) )
), ),
showPage = true showPage = true
...@@ -211,10 +224,14 @@ class DefaultBrowserToolbarMenuController( ...@@ -211,10 +224,14 @@ class DefaultBrowserToolbarMenuController(
BrowserFragmentDirections.actionBrowserFragmentToSyncedTabsFragment() BrowserFragmentDirections.actionBrowserFragmentToSyncedTabsFragment()
) )
} }
is ToolbarMenu.Item.RequestDesktop -> sessionUseCases.requestDesktopSite.invoke( is ToolbarMenu.Item.RequestDesktop -> {
item.isChecked, currentSession?.let {
currentSession sessionUseCases.requestDesktopSite.invoke(
) item.isChecked,
it.id
)
}
}
is ToolbarMenu.Item.AddToTopSites -> { is ToolbarMenu.Item.AddToTopSites -> {
scope.launch { scope.launch {
val context = swipeRefresh.context val context = swipeRefresh.context
...@@ -234,7 +251,7 @@ class DefaultBrowserToolbarMenuController( ...@@ -234,7 +251,7 @@ class DefaultBrowserToolbarMenuController(
ioScope.launch { ioScope.launch {
currentSession?.let { currentSession?.let {
with(activity.components.useCases.topSitesUseCase) { with(activity.components.useCases.topSitesUseCase) {
addPinnedSites(it.title, it.url) addPinnedSites(it.content.title, it.content.url)
} }
} }
}.join() }.join()
...@@ -294,8 +311,8 @@ class DefaultBrowserToolbarMenuController( ...@@ -294,8 +311,8 @@ class DefaultBrowserToolbarMenuController(
} }
} }
is ToolbarMenu.Item.Bookmark -> { is ToolbarMenu.Item.Bookmark -> {
sessionManager.selectedSession?.let { store.state.selectedTab?.let {
getProperUrl(it)?.let { url -> bookmarkTapped(url, it.title) } getProperUrl(it)?.let { url -> bookmarkTapped(url, it.content.title) }
} }
} }
is ToolbarMenu.Item.Bookmarks -> browserAnimator.captureEngineViewAndDrawStatically { is ToolbarMenu.Item.Bookmarks -> browserAnimator.captureEngineViewAndDrawStatically {
...@@ -325,13 +342,13 @@ class DefaultBrowserToolbarMenuController( ...@@ -325,13 +342,13 @@ class DefaultBrowserToolbarMenuController(
} }
} }
private fun getProperUrl(currentSession: Session?): String? { private fun getProperUrl(currentSession: SessionState?): String? {
return currentSession?.id?.let { return currentSession?.id?.let {
val currentTab = browserStore.state.findTab(it) val currentTab = browserStore.state.findTab(it)
if (currentTab?.readerState?.active == true) { if (currentTab?.readerState?.active == true) {
currentTab.readerState.activeUrl currentTab.readerState.activeUrl
} else { } else {
currentSession.url currentSession.content.url
} }
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment