From dfb4f29848f891c9528de3d71c06516ecc0405af Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Wed, 20 Feb 2019 14:16:59 -0800 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/507 - Moves the loading of the URL to the fragment --- .../fenix/components/toolbar/ToolbarUIView.kt | 4 +-- .../mozilla/fenix/search/SearchFragment.kt | 34 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarUIView.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarUIView.kt index 5ec7506068..1cda9ae591 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/ToolbarUIView.kt @@ -39,7 +39,7 @@ class ToolbarUIView( view.apply { setOnUrlCommitListener { actionEmitter.onNext(SearchAction.UrlCommitted(it)) - true + false } onUrlClicked = { actionEmitter.onNext(SearchAction.ToolbarTapped) @@ -60,7 +60,7 @@ class ToolbarUIView( } override fun onStopEditing() { - actionEmitter.onNext(SearchAction.UrlCommitted(url)) +// actionEmitter.onNext(SearchAction.UrlCommitted(url)) } }) } diff --git a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt index afac49aa7a..60e7c7b123 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt @@ -12,6 +12,9 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.navigation.Navigation import kotlinx.android.synthetic.main.fragment_search.view.* +import mozilla.components.browser.session.Session +import mozilla.components.support.ktx.kotlin.isUrl +import mozilla.components.support.ktx.kotlin.toNormalizedUrl import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R import org.mozilla.fenix.components.toolbar.SearchAction @@ -77,7 +80,10 @@ class SearchFragment : Fragment() { getAutoDisposeObservable() .subscribe { when (it) { - is SearchAction.UrlCommitted -> transitionToBrowser() + is SearchAction.UrlCommitted -> { + transitionToBrowser() + load(it.url) + } is SearchAction.TextChanged -> { getManagedEmitter().onNext(AwesomeBarChange.UpdateQuery(it.query)) } @@ -92,6 +98,32 @@ class SearchFragment : Fragment() { } } + private fun load(text: String) { + val sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId + val isPrivate = SearchFragmentArgs.fromBundle(arguments!!).isPrivateTab + + val loadUrlUseCase = if (sessionId == null) { + if (isPrivate) { + requireComponents.useCases.tabsUseCases.addPrivateTab + } else { + requireComponents.useCases.tabsUseCases.addTab + } + } else requireComponents.useCases.sessionUseCases.loadUrl + + val searchUseCase: (String) -> Unit = { searchTerms -> + if (sessionId == null) { + requireComponents.useCases.searchUseCases.newTabSearch + .invoke(searchTerms, Session.Source.USER_ENTERED, true, isPrivate) + } else requireComponents.useCases.searchUseCases.defaultSearch.invoke(searchTerms) + } + + if (text.isUrl()) { + loadUrlUseCase.invoke(text.toNormalizedUrl()) + } else { + searchUseCase.invoke(text) + } + } + private fun transitionToBrowser() { val sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId val directions = SearchFragmentDirections.actionSearchFragmentToBrowserFragment(sessionId,