2020-08-04 19:35:08 +02: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/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.searchdialog
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.appcompat.app.AppCompatDialogFragment
|
2020-08-06 01:22:07 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.navigation.fragment.navArgs
|
2020-08-05 21:40:43 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search.view.*
|
2020-08-06 01:22:07 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.*
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import mozilla.components.browser.state.selector.findTab
|
|
|
|
import mozilla.components.lib.state.ext.consumeFrom
|
|
|
|
import mozilla.components.support.ktx.android.view.hideKeyboard
|
|
|
|
import org.mozilla.fenix.HomeActivity
|
2020-08-04 19:35:08 +02:00
|
|
|
import org.mozilla.fenix.R
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.search.SearchEngineSource
|
|
|
|
import org.mozilla.fenix.search.SearchFragmentState
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.search.SearchFragmentStore
|
|
|
|
import org.mozilla.fenix.search.SearchInteractor
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
|
|
|
import org.mozilla.fenix.search.toolbar.ToolbarView
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
|
|
|
|
typealias SearchDialogFragmentStore = SearchFragmentStore
|
|
|
|
typealias SearchDialogInteractor = SearchInteractor
|
|
|
|
fun Settings.shouldShowSearchSuggestions(isPrivate: Boolean): Boolean {
|
|
|
|
return if (isPrivate) {
|
|
|
|
shouldShowSearchSuggestions && shouldShowSearchSuggestionsInPrivate
|
|
|
|
} else {
|
|
|
|
shouldShowSearchSuggestions
|
2020-08-05 21:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 19:35:08 +02:00
|
|
|
|
|
|
|
class SearchDialogFragment : AppCompatDialogFragment() {
|
2020-08-05 21:40:43 +02:00
|
|
|
|
2020-08-06 01:22:07 +02:00
|
|
|
private lateinit var interactor: SearchDialogInteractor
|
|
|
|
private lateinit var store: SearchDialogFragmentStore
|
2020-08-05 21:40:43 +02:00
|
|
|
private lateinit var toolbarView: ToolbarView
|
|
|
|
private lateinit var awesomeBarView: AwesomeBarView
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setStyle(STYLE_NO_TITLE, R.style.SearchDialogStyle)
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:35:08 +02:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
2020-08-05 21:40:43 +02:00
|
|
|
): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_search_dialog, container, false)
|
2020-08-06 01:22:07 +02:00
|
|
|
store = SearchDialogFragmentStore(setUpState())
|
|
|
|
|
|
|
|
interactor = SearchDialogInteractor(
|
|
|
|
SearchDialogController(
|
|
|
|
activity = requireActivity() as HomeActivity,
|
|
|
|
sessionManager = requireComponents.core.sessionManager,
|
|
|
|
store = store,
|
|
|
|
navController = findNavController(),
|
|
|
|
settings = requireContext().settings(),
|
|
|
|
metrics = requireComponents.analytics.metrics,
|
|
|
|
clearToolbarFocus = {
|
|
|
|
toolbarView.view.hideKeyboard()
|
|
|
|
toolbarView.view.clearFocus()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2020-08-05 21:40:43 +02:00
|
|
|
|
|
|
|
toolbarView = ToolbarView(
|
|
|
|
requireContext(),
|
2020-08-06 01:22:07 +02:00
|
|
|
interactor,
|
2020-08-05 21:40:43 +02:00
|
|
|
null,
|
|
|
|
false,
|
|
|
|
view.toolbar,
|
|
|
|
requireComponents.core.engine
|
|
|
|
)
|
|
|
|
|
|
|
|
awesomeBarView = AwesomeBarView(
|
|
|
|
requireContext(),
|
2020-08-06 01:22:07 +02:00
|
|
|
interactor,
|
2020-08-05 21:40:43 +02:00
|
|
|
view.awesomeBar
|
|
|
|
)
|
|
|
|
|
|
|
|
return view
|
|
|
|
}
|
2020-08-06 01:22:07 +02:00
|
|
|
|
|
|
|
@ExperimentalCoroutinesApi
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
consumeFrom(store) {
|
|
|
|
awesomeBar?.visibility = if (it.query.isEmpty()) View.INVISIBLE else View.VISIBLE
|
|
|
|
toolbarView.update(it)
|
|
|
|
awesomeBarView.update(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpState(): SearchFragmentState {
|
|
|
|
val activity = activity as HomeActivity
|
|
|
|
val settings = activity.settings()
|
|
|
|
val args by navArgs<SearchDialogFragmentArgs>()
|
|
|
|
val tabId = args.sessionId
|
|
|
|
val tab = tabId?.let { requireComponents.core.store.state.findTab(it) }
|
|
|
|
val url = tab?.content?.url.orEmpty()
|
|
|
|
val currentSearchEngine = SearchEngineSource.Default(
|
|
|
|
requireComponents.search.provider.getDefaultEngine(requireContext())
|
|
|
|
)
|
|
|
|
val isPrivate = activity.browsingModeManager.mode.isPrivate
|
|
|
|
val areShortcutsAvailable =
|
|
|
|
requireContext().components.search.provider.installedSearchEngines(requireContext())
|
|
|
|
.list.size >= MINIMUM_SEARCH_ENGINES_NUMBER_TO_SHOW_SHORTCUTS
|
|
|
|
return SearchFragmentState(
|
|
|
|
query = url,
|
|
|
|
url = url,
|
|
|
|
searchTerms = tab?.content?.searchTerms.orEmpty(),
|
|
|
|
searchEngineSource = currentSearchEngine,
|
|
|
|
defaultEngineSource = currentSearchEngine,
|
|
|
|
showSearchSuggestions = settings.shouldShowSearchSuggestions(isPrivate),
|
|
|
|
showSearchSuggestionsHint = false,
|
|
|
|
showSearchShortcuts = settings.shouldShowSearchShortcuts &&
|
|
|
|
url.isEmpty() &&
|
|
|
|
areShortcutsAvailable,
|
|
|
|
areShortcutsAvailable = areShortcutsAvailable,
|
|
|
|
showClipboardSuggestions = settings.shouldShowClipboardSuggestions,
|
|
|
|
showHistorySuggestions = settings.shouldShowHistorySuggestions,
|
|
|
|
showBookmarkSuggestions = settings.shouldShowBookmarkSuggestions,
|
|
|
|
tabId = tabId,
|
|
|
|
pastedText = args.pastedText,
|
|
|
|
searchAccessPoint = args.searchAccessPoint
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val MINIMUM_SEARCH_ENGINES_NUMBER_TO_SHOW_SHORTCUTS = 2
|
|
|
|
}
|
2020-08-04 19:35:08 +02:00
|
|
|
}
|