Commit 2d873071 authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Remove remaining usages of Session[Manager] in BrowserFragment

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