Use global navigation action for browser fragment (#4691)

This commit is contained in:
Tiger Oakes 2019-08-14 12:58:29 -04:00 committed by Colin Lee
parent 7729cb63a7
commit 3acabeb6c3
2 changed files with 26 additions and 59 deletions

View File

@ -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)
}

View File

@ -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
}