For #10401 - Hide Shortcuts if just one search engine is installed

Otherwise, the Shortcuts option which allows to choose with what search engines
to search would be redundant.
This commit is contained in:
Mugurell 2020-07-02 12:18:37 +03:00
parent 388c144a62
commit 0fb6bbc175
3 changed files with 40 additions and 2 deletions

View File

@ -101,6 +101,7 @@ class SearchFragment : Fragment(), UserInteractionHandler {
requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea)
val areShortcutsAvailable = areShortcutsAvailable()
searchStore = StoreProvider.get(this) {
SearchFragmentStore(
SearchFragmentState(
@ -111,7 +112,10 @@ class SearchFragment : Fragment(), UserInteractionHandler {
defaultEngineSource = currentSearchEngine,
showSearchSuggestions = shouldShowSearchSuggestions(isPrivate),
showSearchSuggestionsHint = false,
showSearchShortcuts = requireContext().settings().shouldShowSearchShortcuts && url.isEmpty(),
showSearchShortcuts = requireContext().settings().shouldShowSearchShortcuts &&
url.isEmpty() &&
areShortcutsAvailable,
areShortcutsAvailable = areShortcutsAvailable,
showClipboardSuggestions = requireContext().settings().shouldShowClipboardSuggestions,
showHistorySuggestions = requireContext().settings().shouldShowHistorySuggestions,
showBookmarkSuggestions = requireContext().settings().shouldShowBookmarkSuggestions,
@ -334,6 +338,12 @@ class SearchFragment : Fragment(), UserInteractionHandler {
)
}
// Users can from this fragment go to install/uninstall search engines and then return.
val areShortcutsAvailable = areShortcutsAvailable()
if (searchStore.state.areShortcutsAvailable != areShortcutsAvailable) {
searchStore.dispatch(SearchFragmentAction.UpdateShortcutsAvailability(areShortcutsAvailable))
}
if (!permissionDidUpdate) {
toolbarView.view.edit.focus()
}
@ -431,6 +441,8 @@ class SearchFragment : Fragment(), UserInteractionHandler {
private fun updateSearchShortcutsIcon(searchState: SearchFragmentState) {
view?.apply {
search_shortcuts_button.isVisible = searchState.areShortcutsAvailable
val showShortcuts = searchState.showSearchShortcuts
search_shortcuts_button.isChecked = showShortcuts
@ -441,7 +453,16 @@ class SearchFragment : Fragment(), UserInteractionHandler {
}
}
/**
* Return if the user has *at least 2* installed search engines.
* Useful to decide whether to show / enable certain functionalities.
*/
private fun areShortcutsAvailable() =
requireContext().components.search.provider.installedSearchEngines(requireContext())
.list.size >= MINIMUM_SEARCH_ENGINES_NUMBER_TO_SHOW_SHORTCUTS
companion object {
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
private const val MINIMUM_SEARCH_ENGINES_NUMBER_TO_SHOW_SHORTCUTS = 2
}
}

View File

@ -41,6 +41,8 @@ sealed class SearchEngineSource {
* @property showSearchSuggestions Whether or not to show search suggestions from the search engine in the AwesomeBar
* @property showSearchSuggestionsHint Whether or not to show search suggestions in private hint panel
* @property showSearchShortcuts Whether or not to show search shortcuts in the AwesomeBar
* @property areShortcutsAvailable Whether or not there are >=2 search engines installed
* so to know to present users with certain options or not.
* @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
@ -55,6 +57,7 @@ data class SearchFragmentState(
val showSearchSuggestions: Boolean,
val showSearchSuggestionsHint: Boolean,
val showSearchShortcuts: Boolean,
val areShortcutsAvailable: Boolean,
val showClipboardSuggestions: Boolean,
val showHistorySuggestions: Boolean,
val showBookmarkSuggestions: Boolean,
@ -71,6 +74,7 @@ sealed class SearchFragmentAction : Action {
data class SearchShortcutEngineSelected(val engine: SearchEngine) : SearchFragmentAction()
data class SelectNewDefaultSearchEngine(val engine: SearchEngine) : SearchFragmentAction()
data class ShowSearchShortcutEnginePicker(val show: Boolean) : SearchFragmentAction()
data class UpdateShortcutsAvailability(val areShortcutsAvailable: Boolean) : SearchFragmentAction()
data class AllowSearchSuggestionsInPrivateModePrompt(val show: Boolean) : SearchFragmentAction()
data class UpdateQuery(val query: String) : SearchFragmentAction()
}
@ -86,7 +90,9 @@ private fun searchStateReducer(state: SearchFragmentState, action: SearchFragmen
showSearchShortcuts = false
)
is SearchFragmentAction.ShowSearchShortcutEnginePicker ->
state.copy(showSearchShortcuts = action.show)
state.copy(showSearchShortcuts = action.show && state.areShortcutsAvailable)
is SearchFragmentAction.UpdateShortcutsAvailability ->
state.copy(areShortcutsAvailable = action.areShortcutsAvailable)
is SearchFragmentAction.UpdateQuery ->
state.copy(query = action.query)
is SearchFragmentAction.SelectNewDefaultSearchEngine ->

View File

@ -47,6 +47,16 @@ class SearchFragmentStoreTest {
assertEquals(true, store.state.showSearchShortcuts)
}
@Test
fun hideSearchShortcutEnginePicker() = runBlocking {
val initialState = emptyDefaultState()
val store = SearchFragmentStore(initialState)
store.dispatch(SearchFragmentAction.UpdateShortcutsAvailability(false)).join()
assertNotSame(initialState, store.state)
assertEquals(false, store.state.showSearchShortcuts)
}
private fun emptyDefaultState(): SearchFragmentState = SearchFragmentState(
tabId = null,
url = "",
@ -57,6 +67,7 @@ class SearchFragmentStoreTest {
showSearchSuggestionsHint = false,
showSearchSuggestions = false,
showSearchShortcuts = false,
areShortcutsAvailable = true,
showClipboardSuggestions = false,
showHistorySuggestions = false,
showBookmarkSuggestions = false,