1
0
Fork 0

Closes #2147: Refactors usage of openToBrowserAndLoad

master
Sawyer Blatz 2019-04-29 12:32:30 -07:00 committed by Emily Kager
parent 0e44921e08
commit 70486039c0
7 changed files with 57 additions and 57 deletions

View File

@ -140,22 +140,22 @@ open class HomeActivity : AppCompatActivity() {
this.intent.putExtra(OPEN_TO_BROWSER, false) this.intent.putExtra(OPEN_TO_BROWSER, false)
var customTabSessionId: String? = null var customTabSessionId: String? = null
intent.let { if (isCustomTab) {
if (isCustomTab) { customTabSessionId = SafeIntent(intent).getStringExtra(IntentProcessor.ACTIVE_SESSION_ID)
customTabSessionId = SafeIntent(intent).getStringExtra(IntentProcessor.ACTIVE_SESSION_ID)
}
openToBrowser(BrowserDirection.FromGlobal, customTabSessionId)
} }
openToBrowser(BrowserDirection.FromGlobal, customTabSessionId)
} }
fun openToBrowserAndLoad( fun openToBrowserAndLoad(
searchTermOrURL: String, searchTermOrURL: String,
newTab: Boolean,
from: BrowserDirection,
customTabSessionId: String? = null, customTabSessionId: String? = null,
engine: SearchEngine? = null, engine: SearchEngine? = null
from: BrowserDirection
) { ) {
openToBrowser(from, customTabSessionId) openToBrowser(from, customTabSessionId)
load(searchTermOrURL, customTabSessionId, engine) load(searchTermOrURL, newTab, engine)
} }
fun openToBrowser(from: BrowserDirection, customTabSessionId: String? = null) { fun openToBrowser(from: BrowserDirection, customTabSessionId: String? = null) {
@ -180,10 +180,10 @@ open class HomeActivity : AppCompatActivity() {
navHost.navController.navigate(directions) navHost.navController.navigate(directions)
} }
private fun load(searchTermOrURL: String, sessionId: String?, engine: SearchEngine?) { private fun load(searchTermOrURL: String, newTab: Boolean, engine: SearchEngine?) {
val isPrivate = this.browsingModeManager.isPrivate val isPrivate = this.browsingModeManager.isPrivate
val loadUrlUseCase = if (sessionId == null) { val loadUrlUseCase = if (newTab) {
if (isPrivate) { if (isPrivate) {
components.useCases.tabsUseCases.addPrivateTab components.useCases.tabsUseCases.addPrivateTab
} else { } else {
@ -192,7 +192,7 @@ open class HomeActivity : AppCompatActivity() {
} else components.useCases.sessionUseCases.loadUrl } else components.useCases.sessionUseCases.loadUrl
val searchUseCase: (String) -> Unit = { searchTerms -> val searchUseCase: (String) -> Unit = { searchTerms ->
if (sessionId == null) { if (newTab) {
components.useCases.searchUseCases.newTabSearch components.useCases.searchUseCases.newTabSearch
.invoke(searchTerms, Session.Source.USER_ENTERED, true, isPrivate, searchEngine = engine) .invoke(searchTerms, Session.Source.USER_ENTERED, true, isPrivate, searchEngine = engine)
} else components.useCases.searchUseCases.defaultSearch.invoke(searchTerms, engine) } else components.useCases.searchUseCases.defaultSearch.invoke(searchTerms, engine)

View File

@ -418,7 +418,7 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope,
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
sessionObserver?.let { sessionObserver?.let {
requireComponents.core.sessionManager.selectedSession?.unregister(it) getSessionById()?.unregister(it)
} }
} }
@ -602,7 +602,7 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope,
setToolbarBehavior(loading) setToolbarBehavior(loading)
} }
} }
requireComponents.core.sessionManager.selectedSession?.register(observer) getSessionById()?.register(observer)
return observer return observer
} }

View File

@ -220,8 +220,11 @@ class HomeFragment : Fragment(), CoroutineScope {
requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(action.private) requireComponents.useCases.tabsUseCases.removeAllTabsOfType.invoke(action.private)
} }
is TabAction.PrivateBrowsingLearnMore -> { is TabAction.PrivateBrowsingLearnMore -> {
(activity as HomeActivity).openToBrowserAndLoad(SupportUtils.getGenericSumoURLForTopic (activity as HomeActivity).openToBrowserAndLoad(
(SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS), from = BrowserDirection.FromHome) searchTermOrURL = SupportUtils.getGenericSumoURLForTopic
(SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS),
newTab = true,
from = BrowserDirection.FromHome)
} }
is TabAction.Add -> { is TabAction.Add -> {
val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null) val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null)
@ -251,10 +254,12 @@ class HomeFragment : Fragment(), CoroutineScope {
) )
HomeMenu.Item.Help -> { HomeMenu.Item.Help -> {
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
SupportUtils.getSumoURLForTopic( searchTermOrURL = SupportUtils.getSumoURLForTopic(
context!!, context!!,
SupportUtils.SumoTopic.HELP SupportUtils.SumoTopic.HELP
), from = BrowserDirection.FromHome ),
newTab = true,
from = BrowserDirection.FromHome
) )
} }
} }

View File

@ -145,7 +145,10 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve
if (it.item.type == BookmarkNodeType.ITEM) { if (it.item.type == BookmarkNodeType.ITEM) {
it.item.url?.let { url -> it.item.url?.let { url ->
(activity as HomeActivity) (activity as HomeActivity)
.openToBrowserAndLoad(url, from = BrowserDirection.FromBookmarks) .openToBrowserAndLoad(
searchTermOrURL = url,
newTab = false,
from = BrowserDirection.FromBookmarks)
} }
} }
requireComponents.analytics.metrics.track(Event.OpenedBookmark) requireComponents.analytics.metrics.track(Event.OpenedBookmark)
@ -188,6 +191,7 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve
BrowsingModeManager.Mode.Normal BrowsingModeManager.Mode.Normal
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = url, searchTermOrURL = url,
newTab = true,
from = BrowserDirection.FromBookmarks from = BrowserDirection.FromBookmarks
) )
requireComponents.analytics.metrics.track(Event.OpenedBookmarkInNewTab) requireComponents.analytics.metrics.track(Event.OpenedBookmarkInNewTab)
@ -199,6 +203,7 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve
BrowsingModeManager.Mode.Private BrowsingModeManager.Mode.Private
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = url, searchTermOrURL = url,
newTab = true,
from = BrowserDirection.FromBookmarks from = BrowserDirection.FromBookmarks
) )
requireComponents.analytics.metrics.track(Event.OpenedBookmarkInPrivateTab) requireComponents.analytics.metrics.track(Event.OpenedBookmarkInPrivateTab)

View File

@ -67,7 +67,10 @@ class HistoryFragment : Fragment(), CoroutineScope, BackHandler {
} }
private fun selectItem(item: HistoryItem) { private fun selectItem(item: HistoryItem) {
(activity as HomeActivity).openToBrowserAndLoad(item.url, from = BrowserDirection.FromHistory) (activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = item.url,
newTab = false,
from = BrowserDirection.FromHistory)
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -21,8 +21,6 @@ import kotlinx.android.synthetic.main.fragment_search.*
import kotlinx.android.synthetic.main.fragment_search.view.* import kotlinx.android.synthetic.main.fragment_search.view.*
import mozilla.components.browser.search.SearchEngine import mozilla.components.browser.search.SearchEngine
import mozilla.components.feature.qr.QrFeature import mozilla.components.feature.qr.QrFeature
import mozilla.components.feature.search.SearchUseCases
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import mozilla.components.support.ktx.android.content.isPermissionGranted import mozilla.components.support.ktx.android.content.isPermissionGranted
@ -36,7 +34,6 @@ import org.mozilla.fenix.components.toolbar.SearchChange
import org.mozilla.fenix.components.toolbar.SearchState import org.mozilla.fenix.components.toolbar.SearchState
import org.mozilla.fenix.components.toolbar.ToolbarComponent import org.mozilla.fenix.components.toolbar.ToolbarComponent
import org.mozilla.fenix.components.toolbar.ToolbarUIView import org.mozilla.fenix.components.toolbar.ToolbarUIView
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getSpannable import org.mozilla.fenix.ext.getSpannable
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.mvi.ActionBusFactory import org.mozilla.fenix.mvi.ActionBusFactory
@ -110,7 +107,11 @@ class SearchFragment : Fragment(), BackHandler {
} }
setPositiveButton("ALLOW") { dialog: DialogInterface, _ -> setPositiveButton("ALLOW") { dialog: DialogInterface, _ ->
(activity as HomeActivity) (activity as HomeActivity)
.openToBrowserAndLoad(result, from = BrowserDirection.FromSearch) .openToBrowserAndLoad(
searchTermOrURL = result,
newTab = sessionId == null,
from = BrowserDirection.FromSearch
)
dialog.dismiss() dialog.dismiss()
// TODO add metrics // TODO add metrics
} }
@ -182,7 +183,10 @@ class SearchFragment : Fragment(), BackHandler {
is SearchAction.UrlCommitted -> { is SearchAction.UrlCommitted -> {
if (it.url.isNotBlank()) { if (it.url.isNotBlank()) {
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
it.url, engine = it.engine, from = BrowserDirection.FromSearch searchTermOrURL = it.url,
newTab = sessionId == null,
from = BrowserDirection.FromSearch,
engine = it.engine
) )
val event = if (it.url.isUrl()) { val event = if (it.url.isUrl()) {
@ -212,18 +216,23 @@ class SearchFragment : Fragment(), BackHandler {
.subscribe { .subscribe {
when (it) { when (it) {
is AwesomeBarAction.URLTapped -> { is AwesomeBarAction.URLTapped -> {
getSessionUseCase(requireContext(), sessionId == null).invoke(it.url) (activity as HomeActivity).openToBrowserAndLoad(
(activity as HomeActivity).openToBrowser(BrowserDirection.FromSearch) searchTermOrURL = it.url,
newTab = sessionId == null,
from = BrowserDirection.FromSearch
)
requireComponents.analytics.metrics.track(Event.EnteredUrl(false)) requireComponents.analytics.metrics.track(Event.EnteredUrl(false))
} }
is AwesomeBarAction.SearchTermsTapped -> { is AwesomeBarAction.SearchTermsTapped -> {
getSearchUseCase(requireContext(), sessionId == null) (activity as HomeActivity).openToBrowserAndLoad(
.invoke(it.searchTerms, it.engine) searchTermOrURL = it.searchTerms,
(activity as HomeActivity).openToBrowser(BrowserDirection.FromSearch) newTab = sessionId == null,
from = BrowserDirection.FromSearch,
engine = it.engine
)
val engine = it.engine ?: requireComponents val engine = it.engine ?: requireComponents
.search.searchEngineManager.getDefaultSearchEngine(requireContext()) .search.searchEngineManager.getDefaultSearchEngine(requireContext())
val event = createSearchEvent(engine, true) val event = createSearchEvent(engine, true)
requireComponents.analytics.metrics.track(event) requireComponents.analytics.metrics.track(event)
@ -254,28 +263,6 @@ class SearchFragment : Fragment(), BackHandler {
return Event.PerformedSearch(source) return Event.PerformedSearch(source)
} }
private fun getSearchUseCase(context: Context, useNewTab: Boolean): SearchUseCases.SearchUseCase {
if (!useNewTab) {
return context.components.useCases.searchUseCases.defaultSearch
}
return when (isPrivate) {
true -> context.components.useCases.searchUseCases.newPrivateTabSearch
false -> context.components.useCases.searchUseCases.newTabSearch
}
}
private fun getSessionUseCase(context: Context, useNewTab: Boolean): SessionUseCases.LoadUrlUseCase {
if (!useNewTab) {
return context.components.useCases.sessionUseCases.loadUrl
}
return when (isPrivate) {
true -> context.components.useCases.tabsUseCases.addPrivateTab
false -> context.components.useCases.tabsUseCases.addTab
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) { when (requestCode) {
REQUEST_CODE_CAMERA_PERMISSIONS -> qrFeature.withFeature { REQUEST_CODE_CAMERA_PERMISSIONS -> qrFeature.withFeature {

View File

@ -142,10 +142,9 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
} }
resources.getString(pref_key_help) -> { resources.getString(pref_key_help) -> {
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
SupportUtils.getSumoURLForTopic( searchTermOrURL = SupportUtils.getSumoURLForTopic(context!!, SupportUtils.SumoTopic.HELP),
context!!, newTab = true,
SupportUtils.SumoTopic.HELP from = BrowserDirection.FromSettings
), from = BrowserDirection.FromSettings
) )
} }
resources.getString(pref_key_rate) -> { resources.getString(pref_key_rate) -> {
@ -153,7 +152,8 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
} }
resources.getString(pref_key_feedback) -> { resources.getString(pref_key_feedback) -> {
(activity as HomeActivity).openToBrowserAndLoad( (activity as HomeActivity).openToBrowserAndLoad(
SupportUtils.FEEDBACK_URL, searchTermOrURL = SupportUtils.FEEDBACK_URL,
newTab = true,
from = BrowserDirection.FromSettings from = BrowserDirection.FromSettings
) )
} }