From 6e66943b3c020e1a877a5b5620ac8d1f346bd9dc Mon Sep 17 00:00:00 2001 From: Mihai Branescu Date: Wed, 13 May 2020 14:50:45 +0300 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/9425 - SearchFragment now reacts to undo operation for custom engines When you remove a search engine from our list, an undo snackbar that outlives the fragment is shown. If you go back to the search fragment and press undo, now the shortcut list gets updated --- .../searchengine/CustomSearchEngineStore.kt | 4 +--- .../org/mozilla/fenix/search/SearchFragment.kt | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/searchengine/CustomSearchEngineStore.kt b/app/src/main/java/org/mozilla/fenix/components/searchengine/CustomSearchEngineStore.kt index a5fbf5ad47..555954082e 100644 --- a/app/src/main/java/org/mozilla/fenix/components/searchengine/CustomSearchEngineStore.kt +++ b/app/src/main/java/org/mozilla/fenix/components/searchengine/CustomSearchEngineStore.kt @@ -6,7 +6,6 @@ package org.mozilla.fenix.components.searchengine import android.content.Context import android.content.SharedPreferences -import androidx.annotation.VisibleForTesting import mozilla.components.browser.icons.IconRequest import mozilla.components.browser.search.SearchEngine import mozilla.components.browser.search.SearchEngineParser @@ -125,7 +124,6 @@ object CustomSearchEngineStore { } } - private const val PREF_KEY_CUSTOM_SEARCH_ENGINES = "pref_custom_search_engines" - @VisibleForTesting + const val PREF_KEY_CUSTOM_SEARCH_ENGINES = "pref_custom_search_engines" const val PREF_FILE_SEARCH_ENGINES = "custom-search-engines" } 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 280f411e0f..c36e80d9f0 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt @@ -46,6 +46,7 @@ import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.metrics.Event +import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.getSpannable import org.mozilla.fenix.ext.hideToolbar @@ -54,6 +55,7 @@ import org.mozilla.fenix.ext.settings import org.mozilla.fenix.search.awesomebar.AwesomeBarView import org.mozilla.fenix.search.toolbar.ToolbarView import org.mozilla.fenix.settings.SupportUtils +import org.mozilla.fenix.settings.registerOnSharedPreferenceChangeListener import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE @Suppress("TooManyFunctions", "LargeClass") @@ -128,6 +130,8 @@ class SearchFragment : Fragment(), UserInteractionHandler { awesomeBarView = AwesomeBarView(view.scrollable_area, searchInteractor, view.findViewById(R.id.awesomeBar)) + setShortcutsChangedListener() + view.scrollView.setOnScrollChangeListener { _: NestedScrollView, _: Int, _: Int, _: Int, _: Int -> view.hideKeyboard() @@ -164,6 +168,17 @@ class SearchFragment : Fragment(), UserInteractionHandler { return (speechIntent.resolveActivity(requireContext().packageManager) != null) } + private fun setShortcutsChangedListener() { + requireContext().getSharedPreferences( + CustomSearchEngineStore.PREF_FILE_SEARCH_ENGINES, + Context.MODE_PRIVATE + ).registerOnSharedPreferenceChangeListener(viewLifecycleOwner) { _, key -> + if (key == CustomSearchEngineStore.PREF_KEY_CUSTOM_SEARCH_ENGINES) { + awesomeBarView.update(searchStore.state) + } + } + } + private fun launchVoiceSearch() { // Note if a user disables speech while the app is on the search fragment // the voice button will still be available and *will* cause a crash if tapped,