From 18061d107772f2374263236197e120f0cb5c7eb7 Mon Sep 17 00:00:00 2001 From: lmj0011 <9396189+lmj0011@users.noreply.github.com> Date: Sun, 6 Sep 2020 23:59:41 -0500 Subject: [PATCH] - retain search query when navigating *away* from SettingsSearchController - keep `searchItem` in `expandActionView` state until user goes back (fixes the empty view in `SettingsSearchSearchController` issue) --- .../data/preference/PreferencesHelper.kt | 5 ++++ .../ui/setting/SettingsMainController.kt | 24 +++++++++---------- .../SettingsSearchController.kt | 16 +++++++------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 11bffb186..9a83794ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.data.preference import android.content.Context import android.os.Environment +import androidx.core.content.edit import androidx.core.net.toUri import androidx.preference.PreferenceManager import com.tfcporciuncula.flow.FlowSharedPreferences @@ -249,4 +250,8 @@ class PreferencesHelper(val context: Context) { fun trustedSignatures() = flowPrefs.getStringSet("trusted_signatures", emptySet()) fun enableDoh() = prefs.getBoolean(Keys.enableDoh, false) + + fun lastSearchQuerySearchSettings() = prefs.getString("last_search_query", "") + + fun lastSearchQuerySearchSettings(query: String) = prefs.edit { putString("last_search_query", query) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt index f4ad880e2..a7794073f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.setting import android.view.Menu import android.view.MenuInflater +import android.view.MenuItem import androidx.appcompat.widget.SearchView import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R @@ -13,11 +14,6 @@ import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.system.getResourceColor -import kotlinx.coroutines.flow.filterIsInstance -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach -import reactivecircus.flowbinding.appcompat.QueryTextEvent -import reactivecircus.flowbinding.appcompat.queryTextEvents class SettingsMainController : SettingsController() { @@ -104,14 +100,16 @@ class SettingsMainController : SettingsController() { // Change hint to show global search. searchView.queryHint = applicationContext?.getString(R.string.action_search_settings) - // Create query listener which opens the global search view. - searchView.queryTextEvents() - .filterIsInstance() - .onEach { - router.pushController( - SettingsSearchController().withFadeTransaction() - ) + searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener { + override fun onMenuItemActionExpand(item: MenuItem?): Boolean { + preferences.lastSearchQuerySearchSettings("") // reset saved search query + router.pushController(SettingsSearchController().withFadeTransaction()) + return true } - .launchIn(scope) + + override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { + return true + } + }) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt index 14b43f374..3c57e612c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt @@ -27,6 +27,7 @@ class SettingsSearchController : * Adapter containing search results grouped by lang. */ protected var adapter: SettingsSearchAdapter? = null + lateinit var searchView: SearchView init { setHasOptionsMenu(true) @@ -69,7 +70,7 @@ class SettingsSearchController : // Initialize search menu val searchItem = menu.findItem(R.id.action_search) - val searchView = searchItem.actionView as SearchView + searchView = searchItem.actionView as SearchView searchView.maxWidth = Int.MAX_VALUE // Change hint to show "search settings." @@ -80,22 +81,17 @@ class SettingsSearchController : searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem?): Boolean { - searchView.onActionViewExpanded() // Required to show the query in the view - setItems(getResultSet()) return true } override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { - searchView.onActionViewCollapsed() // Required to show the query in the view router.popCurrentController() - return true + return false } }) searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { - searchItem.collapseActionView() - setTitle(query) // Update toolbar title setItems(getResultSet(query)) return false } @@ -105,6 +101,8 @@ class SettingsSearchController : return false } }) + + searchView.setQuery(presenter.preferences.lastSearchQuerySearchSettings(), true) } override fun onViewCreated(view: View) { @@ -161,6 +159,10 @@ class SettingsSearchController : * Opens a catalogue with the given search. */ override fun onTitleClick(ctrl: SettingsController) { + searchView.query.let { + presenter.preferences.lastSearchQuerySearchSettings(it.toString()) + } + router.pushController(ctrl.withFadeTransaction()) } }