diff --git a/app/src/main/java/org/mozilla/fenix/BrowserDirection.kt b/app/src/main/java/org/mozilla/fenix/BrowserDirection.kt new file mode 100644 index 000000000..c4095ca8c --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/BrowserDirection.kt @@ -0,0 +1,24 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix + +import androidx.annotation.IdRes + +/** + * Used with [HomeActivity.openToBrowser] to indicate which fragment + * the browser is being opened from. + * + * @property fragmentId ID of the fragment opening the browser in the navigation graph. + * An ID of `0` indicates a global action with no corresponding opening fragment. + */ +enum class BrowserDirection(@IdRes val fragmentId: Int) { + FromGlobal(0), + FromHome(R.id.homeFragment), + FromSearch(R.id.searchFragment), + FromSettings(R.id.settingsFragment), + FromBookmarks(R.id.bookmarkFragment), + FromHistory(R.id.historyFragment), + FromExceptions(R.id.exceptionsFragment) +} diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index b53037209..f3a1cadfe 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -35,16 +35,9 @@ import mozilla.components.support.utils.SafeIntent import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.isSentryEnabled import org.mozilla.fenix.components.metrics.Event -import org.mozilla.fenix.exceptions.ExceptionsFragmentDirections import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.getRootView import org.mozilla.fenix.ext.nav -import org.mozilla.fenix.home.HomeFragmentDirections -import org.mozilla.fenix.library.bookmarks.BookmarkFragmentDirections -import org.mozilla.fenix.library.bookmarks.selectfolder.SelectBookmarkFolderFragmentDirections -import org.mozilla.fenix.library.history.HistoryFragmentDirections -import org.mozilla.fenix.search.SearchFragmentDirections -import org.mozilla.fenix.settings.SettingsFragmentDirections import org.mozilla.fenix.share.ShareFragment import org.mozilla.fenix.utils.Settings @@ -225,7 +218,6 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback load(searchTermOrURL, newTab, engine, forceSearch) } - @Suppress("ComplexMethod") fun openToBrowser(from: BrowserDirection, customTabSessionId: String? = null) { if (sessionObserver == null) sessionObserver = subscribeToSessions() @@ -238,52 +230,8 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback ) return } - @IdRes var fragmentId: Int? = null - val directions = when (from) { - BrowserDirection.FromGlobal -> - NavGraphDirections.actionGlobalBrowser(customTabSessionId) - BrowserDirection.FromHome -> { - fragmentId = R.id.homeFragment - HomeFragmentDirections.actionHomeFragmentToBrowserFragment(customTabSessionId) - } - BrowserDirection.FromSearch -> { - fragmentId = R.id.searchFragment - SearchFragmentDirections.actionSearchFragmentToBrowserFragment( - customTabSessionId - ) - } - BrowserDirection.FromSettings -> { - fragmentId = R.id.settingsFragment - SettingsFragmentDirections.actionSettingsFragmentToBrowserFragment( - customTabSessionId - ) - } - BrowserDirection.FromBookmarks -> { - fragmentId = R.id.bookmarkFragment - BookmarkFragmentDirections.actionBookmarkFragmentToBrowserFragment( - customTabSessionId - ) - } - BrowserDirection.FromBookmarksFolderSelect -> { - fragmentId = R.id.bookmarkSelectFolderFragment - SelectBookmarkFolderFragmentDirections - .actionBookmarkSelectFolderFragmentToBrowserFragment(customTabSessionId) - } - BrowserDirection.FromHistory -> { - fragmentId = R.id.historyFragment - HistoryFragmentDirections.actionHistoryFragmentToBrowserFragment( - customTabSessionId - ) - } - BrowserDirection.FromExceptions -> { - fragmentId = R.id.exceptionsFragment - ExceptionsFragmentDirections.actionExceptionsFragmentToBrowserFragment( - customTabSessionId - ) - } - } - - navHost.navController.nav(fragmentId, directions) + @IdRes val fragmentId = if (from.fragmentId != 0) from.fragmentId else null + navHost.navController.nav(fragmentId, NavGraphDirections.actionGlobalBrowser(customTabSessionId)) } private fun load( @@ -406,8 +354,3 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback const val OPEN_TO_SEARCH = "open_to_search" } } - -enum class BrowserDirection { - FromGlobal, FromHome, FromSearch, FromSettings, FromBookmarks, - FromBookmarksFolderSelect, FromHistory, FromExceptions -}