From 33b8a527ff88a915b2c6bb4e1a31f743aa93273a Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Thu, 18 Feb 2021 10:54:17 -0500 Subject: [PATCH] [fenix] Remove remaining usages of Session[Manager] in BrowserFragment --- .../fenix/browser/BaseBrowserFragment.kt | 42 ++++++------------- .../mozilla/fenix/browser/BrowserFragment.kt | 2 +- .../components/toolbar/BrowserToolbarView.kt | 10 ++--- .../mozilla/fenix/utils/ToolbarPopupWindow.kt | 8 ++-- .../fenix/utils/ToolbarPopupWindowTest.kt | 6 +-- 5 files changed, 25 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index e8ed31ceee..19644bf423 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -38,13 +38,15 @@ import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import mozilla.appservices.places.BookmarkRoot -import mozilla.components.browser.session.Session import mozilla.components.browser.state.action.ContentAction +import mozilla.components.browser.state.selector.findCustomTab import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab import mozilla.components.browser.state.selector.findTab +import mozilla.components.browser.state.selector.findTabOrCustomTab import mozilla.components.browser.state.selector.findTabOrCustomTabOrSelectedTab import mozilla.components.browser.state.selector.getNormalOrPrivateTabs import mozilla.components.browser.state.selector.selectedTab +import mozilla.components.browser.state.state.CustomTabSessionState import mozilla.components.browser.state.state.SessionState import mozilla.components.browser.state.state.TabSessionState import mozilla.components.browser.state.state.content.DownloadState @@ -254,7 +256,6 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit @CallSuper internal open fun initializeUI(view: View, tab: SessionState) { val context = requireContext() - val sessionManager = context.components.core.sessionManager val store = context.components.core.store val activity = requireActivity() as HomeActivity @@ -350,7 +351,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit container = view.browserLayout, toolbarPosition = context.settings().toolbarPosition, interactor = browserInteractor, - customTabSession = customTabSessionId?.let { sessionManager.findSessionById(it) }, + customTabSession = customTabSessionId?.let { store.state.findCustomTab(it) }, lifecycleOwner = viewLifecycleOwner ) @@ -551,7 +552,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit val directions = NavGraphDirections.actionGlobalShareFragment( data = arrayOf(shareData), showPage = true, - sessionId = getSessionById()?.id + sessionId = getCurrentTab()?.id ) findNavController().navigate(directions) } @@ -585,14 +586,14 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit searchFeature.set( feature = SearchFeature(store, customTabSessionId) { request, tabId -> - val parentSession = sessionManager.findSessionById(tabId) + val parentSession = store.state.findTabOrCustomTab(tabId) val useCase = if (request.isPrivate) { requireComponents.useCases.searchUseCases.newPrivateTabSearch } else { requireComponents.useCases.searchUseCases.newTabSearch } - if (parentSession?.isCustomTabSession() == true) { + if (parentSession is CustomTabSessionState) { useCase.invoke(request.query) requireActivity().startActivity(openInFenixIntent) } else { @@ -1016,7 +1017,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit final override fun onViewStateRestored(savedInstanceState: Bundle?) { super.onViewStateRestored(savedInstanceState) savedInstanceState?.getString(KEY_CUSTOM_TAB_SESSION_ID)?.let { - if (requireComponents.core.sessionManager.findSessionById(it)?.customTabConfig != null) { + if (requireComponents.core.store.state.findCustomTab(it) != null) { customTabSessionId = it } } @@ -1054,22 +1055,18 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit * or if it has a parent session and no more history */ protected open fun removeSessionIfNeeded(): Boolean { - getSessionById()?.let { session -> + getCurrentTab()?.let { session -> return if (session.source == SessionState.Source.ACTION_VIEW) { activity?.finish() requireComponents.useCases.tabsUseCases.removeTab(session.id) true } else { - if (session.hasParentSession) { - // The removeTab use case does not currently select a parent session, so - // we are using sessionManager.remove - requireComponents.core.sessionManager.remove( - session, - selectParentIfExists = true - ) + val hasParentSession = session is TabSessionState && session.parentId != null + if (hasParentSession) { + requireComponents.useCases.tabsUseCases.removeTab(session.id, selectParentIfExists = true) } // We want to return to home if this session didn't have a parent session to select. - val goToOverview = !session.hasParentSession + val goToOverview = !hasParentSession !goToOverview } } @@ -1134,19 +1131,6 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Activit (activity as HomeActivity).browsingModeManager.mode = sessionMode } - /** - * Returns the current session. - */ - protected fun getSessionById(): Session? { - val sessionManager = requireComponents.core.sessionManager - val localCustomTabId = customTabSessionId - return if (localCustomTabId != null) { - sessionManager.findSessionById(localCustomTabId) - } else { - sessionManager.selectedSession - } - } - @VisibleForTesting internal fun getCurrentTab(): SessionState? { return requireComponents.core.store.state.findCustomTabOrSelectedTab(customTabSessionId) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index 6d0cdb9261..58515a9083 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -84,7 +84,7 @@ class BrowserFragment : BaseBrowserFragment(), UserInteractionHandler { visible = { readerModeAvailable }, - selected = getSessionById()?.let { + selected = getCurrentTab()?.let { activity?.components?.core?.store?.state?.findTab(it.id)?.readerState?.active } ?: false, listener = browserInteractor::onReaderModePressed 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 af57953a5f..d8e7e306f3 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 @@ -16,8 +16,8 @@ import androidx.lifecycle.LifecycleOwner import kotlinx.android.extensions.LayoutContainer import kotlinx.coroutines.ExperimentalCoroutinesApi import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider -import mozilla.components.browser.session.Session import mozilla.components.browser.state.selector.selectedTab +import mozilla.components.browser.state.state.CustomTabSessionState import mozilla.components.browser.state.state.ExternalAppType import mozilla.components.browser.toolbar.BrowserToolbar import mozilla.components.browser.toolbar.behavior.BrowserToolbarBehavior @@ -52,7 +52,7 @@ class BrowserToolbarView( private val container: ViewGroup, private val toolbarPosition: ToolbarPosition, private val interactor: BrowserToolbarViewInteractor, - private val customTabSession: Session?, + private val customTabSession: CustomTabSessionState?, private val lifecycleOwner: LifecycleOwner ) : LayoutContainer { @@ -78,8 +78,8 @@ class BrowserToolbarView( @VisibleForTesting internal val isPwaTabOrTwaTab: Boolean - get() = customTabSession?.customTabConfig?.externalAppType == ExternalAppType.PROGRESSIVE_WEB_APP || - customTabSession?.customTabConfig?.externalAppType == ExternalAppType.TRUSTED_WEB_ACTIVITY + get() = customTabSession?.config?.externalAppType == ExternalAppType.PROGRESSIVE_WEB_APP || + customTabSession?.config?.externalAppType == ExternalAppType.TRUSTED_WEB_ACTIVITY init { val isCustomTabSession = customTabSession != null @@ -185,7 +185,7 @@ class BrowserToolbarView( view, menuToolbar, customTabSession.id, - isPrivate = customTabSession.private + isPrivate = customTabSession.content.private ) } else { DefaultToolbarIntegration( diff --git a/app/src/main/java/org/mozilla/fenix/utils/ToolbarPopupWindow.kt b/app/src/main/java/org/mozilla/fenix/utils/ToolbarPopupWindow.kt index 21c56576b7..f9b349fceb 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/ToolbarPopupWindow.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/ToolbarPopupWindow.kt @@ -15,8 +15,8 @@ import androidx.annotation.VisibleForTesting import androidx.core.view.isVisible import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.* -import mozilla.components.browser.session.Session import mozilla.components.browser.state.selector.selectedTab +import mozilla.components.browser.state.state.CustomTabSessionState import mozilla.components.browser.state.store.BrowserStore import org.mozilla.fenix.R import org.mozilla.fenix.components.FenixSnackbar @@ -27,7 +27,7 @@ import java.lang.ref.WeakReference object ToolbarPopupWindow { fun show( view: WeakReference, - customTabSession: Session? = null, + customTabSession: CustomTabSessionState? = null, handlePasteAndGo: (String) -> Unit, handlePaste: (String) -> Unit, copyVisible: Boolean = true @@ -101,10 +101,10 @@ object ToolbarPopupWindow { @VisibleForTesting internal fun getUrlForClipboard( store: BrowserStore, - customTabSession: Session? = null + customTabSession: CustomTabSessionState? = null ): String? { return if (customTabSession != null) { - customTabSession.url + customTabSession.content.url } else { val selectedTab = store.state.selectedTab selectedTab?.readerState?.activeUrl ?: selectedTab?.content?.url diff --git a/app/src/test/java/org/mozilla/fenix/utils/ToolbarPopupWindowTest.kt b/app/src/test/java/org/mozilla/fenix/utils/ToolbarPopupWindowTest.kt index 12ec801cd2..69ef82b3aa 100644 --- a/app/src/test/java/org/mozilla/fenix/utils/ToolbarPopupWindowTest.kt +++ b/app/src/test/java/org/mozilla/fenix/utils/ToolbarPopupWindowTest.kt @@ -4,11 +4,10 @@ package org.mozilla.fenix.utils -import io.mockk.every import io.mockk.mockk -import mozilla.components.browser.session.Session import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.ReaderState +import mozilla.components.browser.state.state.createCustomTab import mozilla.components.browser.state.state.createTab import mozilla.components.browser.state.store.BrowserStore import org.junit.Assert.assertEquals @@ -21,8 +20,7 @@ class ToolbarPopupWindowTest { @Test fun getUrlForClipboard() { - val customTabSession: Session = mockk() - every { customTabSession.url } returns "https://mozilla.org" + val customTabSession = createCustomTab("https://mozilla.org") // Custom tab assertEquals(