From da099b4742d355b8b06a21f037975c42ce57cc7b Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Wed, 17 Jul 2019 16:10:24 -0400 Subject: [PATCH] [fenix] Closes https://github.com/mozilla-mobile/fenix/issues/4096: IllegalStateException in SearchFragment (https://github.com/mozilla-mobile/fenix/pull/4131) --- .../mozilla/fenix/search/SearchFragment.kt | 23 ++++++++++--------- .../org/mozilla/fenix/search/SearchStore.kt | 9 ++++++-- .../mozilla/fenix/search/SearchStoreTest.kt | 6 ++--- 3 files changed, 22 insertions(+), 16 deletions(-) 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 8f4bee1d6b..b8f2cb6d82 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt @@ -19,14 +19,14 @@ import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.whenStarted import androidx.navigation.fragment.findNavController import kotlinx.android.synthetic.main.fragment_search.* import kotlinx.android.synthetic.main.fragment_search.view.* -import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import mozilla.components.concept.storage.HistoryStorage import mozilla.components.feature.qr.QrFeature -import mozilla.components.lib.state.Store import mozilla.components.lib.state.ext.observe import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.base.feature.ViewBoundFeatureWrapper @@ -74,7 +74,7 @@ class SearchFragment : Fragment(), BackHandler { searchStore = StoreProvider.get( this, - Store( + SearchStore( SearchState( query = url, showShortcutEnginePicker = false, @@ -83,8 +83,7 @@ class SearchFragment : Fragment(), BackHandler { ), showSuggestions = Settings.getInstance(requireContext()).showSearchSuggestions, showVisitedSitesBookmarks = Settings.getInstance(requireContext()).shouldShowVisitedSitesBookmarks, - session = session), - ::searchStateReducer + session = session) ) ) @@ -168,12 +167,14 @@ class SearchFragment : Fragment(), BackHandler { } searchStore.observe(view) { - MainScope().launch { - awesomeBarView.update(it) - toolbarView.update(it) - updateSearchEngineIcon(it) - updateSearchShortuctsIcon(it) - updateSearchWithLabel(it) + viewLifecycleOwner.lifecycleScope.launch { + whenStarted { + awesomeBarView.update(it) + toolbarView.update(it) + updateSearchEngineIcon(it) + updateSearchShortuctsIcon(it) + updateSearchWithLabel(it) + } } } diff --git a/app/src/main/java/org/mozilla/fenix/search/SearchStore.kt b/app/src/main/java/org/mozilla/fenix/search/SearchStore.kt index 9c1c81c916..6ec46ffb9b 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchStore.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchStore.kt @@ -11,9 +11,14 @@ import mozilla.components.lib.state.State import mozilla.components.lib.state.Store /** - * An alias to make it easier to work with `Store` + * The [Store] for holding the [SearchState] and applying [SearchAction]s. */ -typealias SearchStore = Store +class SearchStore( + initialState: SearchState +) : Store( + initialState, + ::searchStateReducer +) /** * Wraps a `SearchEngine` to give consumers the context that it was selected as a shortcut diff --git a/app/src/test/java/org/mozilla/fenix/search/SearchStoreTest.kt b/app/src/test/java/org/mozilla/fenix/search/SearchStoreTest.kt index 58b3e195e6..24ab0e632f 100644 --- a/app/src/test/java/org/mozilla/fenix/search/SearchStoreTest.kt +++ b/app/src/test/java/org/mozilla/fenix/search/SearchStoreTest.kt @@ -16,7 +16,7 @@ class SearchStoreTest { @Test fun updateQuery() = runBlocking { val initialState = emptyDefaultState() - val store = SearchStore(initialState, ::searchStateReducer) + val store = SearchStore(initialState) val query = "test query" store.dispatch(SearchAction.UpdateQuery(query)).join() @@ -27,7 +27,7 @@ class SearchStoreTest { @Test fun selectSearchShortcutEngine() = runBlocking { val initialState = emptyDefaultState() - val store = SearchStore(initialState, ::searchStateReducer) + val store = SearchStore(initialState) val searchEngine: SearchEngine = mockk() store.dispatch(SearchAction.SearchShortcutEngineSelected(searchEngine)).join() @@ -38,7 +38,7 @@ class SearchStoreTest { @Test fun showSearchShortcutEnginePicker() = runBlocking { val initialState = emptyDefaultState() - val store = SearchStore(initialState, ::searchStateReducer) + val store = SearchStore(initialState) store.dispatch(SearchAction.ShowSearchShortcutEnginePicker(true)).join() assertNotSame(initialState, store.state)