2019-07-13 01:32:00 +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.search
|
|
|
|
|
|
|
|
import mozilla.components.browser.search.SearchEngine
|
|
|
|
import mozilla.components.browser.session.Session
|
|
|
|
import mozilla.components.lib.state.Action
|
|
|
|
import mozilla.components.lib.state.State
|
|
|
|
import mozilla.components.lib.state.Store
|
2019-12-18 16:09:12 +01:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-07-13 01:32:00 +02:00
|
|
|
|
|
|
|
/**
|
2019-08-30 15:21:34 +02:00
|
|
|
* The [Store] for holding the [SearchFragmentState] and applying [SearchFragmentAction]s.
|
2019-07-13 01:32:00 +02:00
|
|
|
*/
|
2019-08-30 15:21:34 +02:00
|
|
|
class SearchFragmentStore(
|
|
|
|
initialState: SearchFragmentState
|
|
|
|
) : Store<SearchFragmentState, SearchFragmentAction>(
|
2019-07-17 22:10:24 +02:00
|
|
|
initialState,
|
|
|
|
::searchStateReducer
|
|
|
|
)
|
2019-07-13 01:32:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps a `SearchEngine` to give consumers the context that it was selected as a shortcut
|
|
|
|
*/
|
|
|
|
sealed class SearchEngineSource {
|
|
|
|
abstract val searchEngine: SearchEngine
|
|
|
|
|
|
|
|
data class Default(override val searchEngine: SearchEngine) : SearchEngineSource()
|
|
|
|
data class Shortcut(override val searchEngine: SearchEngine) : SearchEngineSource()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state for the Search Screen
|
|
|
|
* @property query The current search query string
|
|
|
|
* @property searchEngineSource The current selected search engine with the context of how it was selected
|
2019-08-01 16:48:38 +02:00
|
|
|
* @property defaultEngineSource The current default search engine source
|
2019-09-05 23:45:44 +02:00
|
|
|
* @property showSearchSuggestions Whether or not to show search suggestions from the search engine in the AwesomeBar
|
2019-11-15 17:01:34 +01:00
|
|
|
* @property showSearchSuggestionsHint Whether or not to show search suggestions in private hint panel
|
2019-10-03 17:53:16 +02:00
|
|
|
* @property showSearchShortcuts Whether or not to show search shortcuts in the AwesomeBar
|
2019-09-05 23:45:44 +02:00
|
|
|
* @property showClipboardSuggestions Whether or not to show clipboard suggestion in the AwesomeBar
|
|
|
|
* @property showHistorySuggestions Whether or not to show history suggestions in the AwesomeBar
|
|
|
|
* @property showBookmarkSuggestions Whether or not to show the bookmark suggestion in the AwesomeBar
|
2019-07-13 01:32:00 +02:00
|
|
|
* @property session The current session if available
|
2019-09-04 22:56:39 +02:00
|
|
|
* @property pastedText The text pasted from the long press toolbar menu
|
2019-07-13 01:32:00 +02:00
|
|
|
*/
|
2019-08-30 15:21:34 +02:00
|
|
|
data class SearchFragmentState(
|
2019-07-13 01:32:00 +02:00
|
|
|
val query: String,
|
|
|
|
val searchEngineSource: SearchEngineSource,
|
2019-08-01 16:48:38 +02:00
|
|
|
val defaultEngineSource: SearchEngineSource.Default,
|
2019-09-05 23:45:44 +02:00
|
|
|
val showSearchSuggestions: Boolean,
|
2019-11-15 17:01:34 +01:00
|
|
|
val showSearchSuggestionsHint: Boolean,
|
2019-10-03 17:53:16 +02:00
|
|
|
val showSearchShortcuts: Boolean,
|
2019-09-05 23:45:44 +02:00
|
|
|
val showClipboardSuggestions: Boolean,
|
|
|
|
val showHistorySuggestions: Boolean,
|
|
|
|
val showBookmarkSuggestions: Boolean,
|
2019-09-04 22:56:39 +02:00
|
|
|
val session: Session?,
|
2019-12-18 16:09:12 +01:00
|
|
|
val pastedText: String? = null,
|
2020-01-22 20:15:30 +01:00
|
|
|
val searchAccessPoint: Event.PerformedSearch.SearchAccessPoint?,
|
|
|
|
val isAnimatingOut: Boolean
|
2019-07-13 01:32:00 +02:00
|
|
|
) : State
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions to dispatch through the `SearchStore` to modify `SearchState` through the reducer.
|
|
|
|
*/
|
2019-08-30 15:21:34 +02:00
|
|
|
sealed class SearchFragmentAction : Action {
|
2020-01-22 20:15:30 +01:00
|
|
|
object UpdateEditingCanceled : SearchFragmentAction()
|
|
|
|
object ConsumeEditingCancelled : SearchFragmentAction()
|
2019-08-30 15:21:34 +02:00
|
|
|
data class SearchShortcutEngineSelected(val engine: SearchEngine) : SearchFragmentAction()
|
|
|
|
data class SelectNewDefaultSearchEngine(val engine: SearchEngine) : SearchFragmentAction()
|
|
|
|
data class ShowSearchShortcutEnginePicker(val show: Boolean) : SearchFragmentAction()
|
2020-01-23 04:08:07 +01:00
|
|
|
data class AllowSearchSuggestionsInPrivateModePrompt(val show: Boolean) : SearchFragmentAction()
|
2019-08-30 15:21:34 +02:00
|
|
|
data class UpdateQuery(val query: String) : SearchFragmentAction()
|
2019-07-13 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The SearchState Reducer.
|
|
|
|
*/
|
2019-08-30 15:34:39 +02:00
|
|
|
private fun searchStateReducer(state: SearchFragmentState, action: SearchFragmentAction): SearchFragmentState {
|
2019-07-13 01:32:00 +02:00
|
|
|
return when (action) {
|
2019-08-30 15:21:34 +02:00
|
|
|
is SearchFragmentAction.SearchShortcutEngineSelected ->
|
2019-07-13 01:32:00 +02:00
|
|
|
state.copy(
|
|
|
|
searchEngineSource = SearchEngineSource.Shortcut(action.engine),
|
2019-10-03 17:53:16 +02:00
|
|
|
showSearchShortcuts = false
|
2019-07-13 01:32:00 +02:00
|
|
|
)
|
2019-08-30 15:21:34 +02:00
|
|
|
is SearchFragmentAction.ShowSearchShortcutEnginePicker ->
|
2019-10-03 17:53:16 +02:00
|
|
|
state.copy(showSearchShortcuts = action.show)
|
2019-08-30 15:21:34 +02:00
|
|
|
is SearchFragmentAction.UpdateQuery ->
|
2019-07-13 01:32:00 +02:00
|
|
|
state.copy(query = action.query)
|
2019-08-30 15:21:34 +02:00
|
|
|
is SearchFragmentAction.SelectNewDefaultSearchEngine ->
|
2019-08-01 16:48:38 +02:00
|
|
|
state.copy(
|
2019-10-03 17:53:16 +02:00
|
|
|
searchEngineSource = SearchEngineSource.Default(action.engine)
|
2019-08-01 16:48:38 +02:00
|
|
|
)
|
2020-01-23 04:08:07 +01:00
|
|
|
is SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt ->
|
2019-11-15 17:01:34 +01:00
|
|
|
state.copy(showSearchSuggestionsHint = action.show)
|
2020-01-22 20:15:30 +01:00
|
|
|
is SearchFragmentAction.UpdateEditingCanceled ->
|
|
|
|
state.copy(isAnimatingOut = true)
|
|
|
|
is SearchFragmentAction.ConsumeEditingCancelled ->
|
|
|
|
state.copy(isAnimatingOut = false)
|
2019-07-13 01:32:00 +02:00
|
|
|
}
|
|
|
|
}
|