2019-01-15 02:42:58 +01:00
|
|
|
/* 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/. */
|
2019-01-29 20:20:29 +01:00
|
|
|
|
2019-01-15 02:42:58 +01:00
|
|
|
package org.mozilla.fenix.search
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-02-08 00:37:52 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-01-15 02:42:58 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-01-24 21:10:16 +01:00
|
|
|
import androidx.navigation.Navigation
|
2019-01-29 20:20:29 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search.view.*
|
2019-02-20 23:16:59 +01:00
|
|
|
import mozilla.components.browser.session.Session
|
|
|
|
import mozilla.components.support.ktx.kotlin.isUrl
|
|
|
|
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
|
2019-02-15 18:31:03 +01:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-01-15 02:42:58 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-02-17 02:04:32 +01:00
|
|
|
import org.mozilla.fenix.components.toolbar.SearchAction
|
|
|
|
import org.mozilla.fenix.components.toolbar.SearchState
|
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarComponent
|
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarUIView
|
2019-02-20 17:58:42 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-01-29 20:20:29 +01:00
|
|
|
import org.mozilla.fenix.mvi.ActionBusFactory
|
2019-02-17 02:04:32 +01:00
|
|
|
import org.mozilla.fenix.mvi.getAutoDisposeObservable
|
2019-02-08 19:35:48 +01:00
|
|
|
import org.mozilla.fenix.mvi.getManagedEmitter
|
2019-01-31 00:51:49 +01:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarAction
|
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarChange
|
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarComponent
|
2019-02-08 19:35:48 +01:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarState
|
2019-01-15 02:42:58 +01:00
|
|
|
|
|
|
|
class SearchFragment : Fragment() {
|
2019-01-31 00:51:49 +01:00
|
|
|
private lateinit var toolbarComponent: ToolbarComponent
|
|
|
|
private lateinit var awesomeBarComponent: AwesomeBarComponent
|
2019-01-24 21:50:30 +01:00
|
|
|
|
2019-01-15 02:42:58 +01:00
|
|
|
override fun onCreateView(
|
2019-01-30 17:36:14 +01:00
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
2019-01-15 02:42:58 +01:00
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
2019-02-08 19:35:48 +01:00
|
|
|
val sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId
|
2019-02-22 18:49:02 +01:00
|
|
|
val isPrivate = (activity as HomeActivity).browsingModeManager.isPrivate
|
2019-01-29 20:20:29 +01:00
|
|
|
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
2019-02-20 17:58:42 +01:00
|
|
|
val url = sessionId?.let {
|
|
|
|
requireComponents.core.sessionManager.findSessionById(it)?.let {
|
|
|
|
session -> session.url
|
|
|
|
}
|
|
|
|
} ?: ""
|
|
|
|
|
2019-02-05 00:41:26 +01:00
|
|
|
toolbarComponent = ToolbarComponent(
|
|
|
|
view.toolbar_wrapper,
|
|
|
|
ActionBusFactory.get(this),
|
2019-02-08 19:35:48 +01:00
|
|
|
sessionId,
|
2019-02-15 18:31:03 +01:00
|
|
|
isPrivate,
|
2019-02-20 17:58:42 +01:00
|
|
|
SearchState(url, isEditing = true)
|
2019-02-05 00:41:26 +01:00
|
|
|
)
|
2019-02-08 19:35:48 +01:00
|
|
|
awesomeBarComponent = AwesomeBarComponent(
|
|
|
|
view.search_layout, ActionBusFactory.get(this),
|
|
|
|
AwesomeBarState("", sessionId == null)
|
|
|
|
)
|
2019-01-31 07:49:41 +01:00
|
|
|
ActionBusFactory.get(this).logMergedObservables()
|
2019-01-29 20:20:29 +01:00
|
|
|
return view
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-01-23 22:39:53 +01:00
|
|
|
|
2019-02-08 00:37:52 +01:00
|
|
|
(activity as AppCompatActivity).supportActionBar?.hide()
|
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
layoutComponents(view.search_layout)
|
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
lifecycle.addObserver((toolbarComponent.uiView as ToolbarUIView).toolbarIntegration)
|
2019-01-23 22:39:53 +01:00
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
view.toolbar_wrapper.clipToOutline = false
|
2019-02-18 22:18:55 +01:00
|
|
|
}
|
2019-01-24 21:10:16 +01:00
|
|
|
|
2019-02-18 22:18:55 +01:00
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
2019-02-17 02:04:32 +01:00
|
|
|
getAutoDisposeObservable<SearchAction>()
|
2019-01-31 00:51:49 +01:00
|
|
|
.subscribe {
|
|
|
|
when (it) {
|
2019-02-20 23:16:59 +01:00
|
|
|
is SearchAction.UrlCommitted -> {
|
2019-02-24 13:28:10 +01:00
|
|
|
if (it.url.isNotBlank()) {
|
|
|
|
transitionToBrowser()
|
|
|
|
load(it.url)
|
|
|
|
}
|
2019-02-20 23:16:59 +01:00
|
|
|
}
|
2019-01-31 07:49:41 +01:00
|
|
|
is SearchAction.TextChanged -> {
|
2019-02-08 19:35:48 +01:00
|
|
|
getManagedEmitter<AwesomeBarChange>().onNext(AwesomeBarChange.UpdateQuery(it.query))
|
2019-01-31 07:49:41 +01:00
|
|
|
}
|
2019-01-31 00:51:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 02:04:32 +01:00
|
|
|
getAutoDisposeObservable<AwesomeBarAction>()
|
2019-01-31 00:51:49 +01:00
|
|
|
.subscribe {
|
|
|
|
when (it) {
|
|
|
|
is AwesomeBarAction.ItemSelected -> transitionToBrowser()
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 21:10:16 +01:00
|
|
|
}
|
2019-01-30 17:36:14 +01:00
|
|
|
|
2019-02-21 19:58:07 +01:00
|
|
|
// Issue: https://github.com/mozilla-mobile/fenix/issues/626
|
|
|
|
// Currently we were kind of forcing all this logic through the Toolbar Feature.
|
|
|
|
// But since we cannot actually load a page without an available GeckoSession
|
|
|
|
// we have to wait until after we navigate to call the use case.
|
|
|
|
// We should move this logic into a place that makes more sense.
|
2019-02-20 23:16:59 +01:00
|
|
|
private fun load(text: String) {
|
|
|
|
val sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId
|
2019-02-22 18:49:02 +01:00
|
|
|
val isPrivate = (activity as HomeActivity).browsingModeManager.isPrivate
|
2019-02-20 23:16:59 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
private fun transitionToBrowser() {
|
2019-02-08 19:35:48 +01:00
|
|
|
val sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId
|
2019-02-22 18:49:02 +01:00
|
|
|
val directions = SearchFragmentDirections.actionSearchFragmentToBrowserFragment(sessionId)
|
2019-02-15 18:31:03 +01:00
|
|
|
|
2019-02-08 19:35:48 +01:00
|
|
|
Navigation.findNavController(view!!.search_layout).navigate(directions)
|
2019-01-30 17:36:14 +01:00
|
|
|
}
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|