- retain search query when navigating *away* from SettingsSearchController

- keep `searchItem` in `expandActionView` state until user goes back (fixes the empty view in `SettingsSearchSearchController` issue)
This commit is contained in:
lmj0011 2020-09-06 23:59:41 -05:00
parent 381c061ebc
commit 18061d1077
3 changed files with 25 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.data.preference
import android.content.Context import android.content.Context
import android.os.Environment import android.os.Environment
import androidx.core.content.edit
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.tfcporciuncula.flow.FlowSharedPreferences import com.tfcporciuncula.flow.FlowSharedPreferences
@ -249,4 +250,8 @@ class PreferencesHelper(val context: Context) {
fun trustedSignatures() = flowPrefs.getStringSet("trusted_signatures", emptySet()) fun trustedSignatures() = flowPrefs.getStringSet("trusted_signatures", emptySet())
fun enableDoh() = prefs.getBoolean(Keys.enableDoh, false) 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) }
} }

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.setting
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R 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.preference
import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.getResourceColor 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() { class SettingsMainController : SettingsController() {
@ -104,14 +100,16 @@ class SettingsMainController : SettingsController() {
// Change hint to show global search. // Change hint to show global search.
searchView.queryHint = applicationContext?.getString(R.string.action_search_settings) searchView.queryHint = applicationContext?.getString(R.string.action_search_settings)
// Create query listener which opens the global search view. searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
searchView.queryTextEvents() override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
.filterIsInstance<QueryTextEvent.QueryChanged>() preferences.lastSearchQuerySearchSettings("") // reset saved search query
.onEach { router.pushController(SettingsSearchController().withFadeTransaction())
router.pushController( return true
SettingsSearchController().withFadeTransaction()
)
} }
.launchIn(scope)
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
return true
}
})
} }
} }

View File

@ -27,6 +27,7 @@ class SettingsSearchController :
* Adapter containing search results grouped by lang. * Adapter containing search results grouped by lang.
*/ */
protected var adapter: SettingsSearchAdapter? = null protected var adapter: SettingsSearchAdapter? = null
lateinit var searchView: SearchView
init { init {
setHasOptionsMenu(true) setHasOptionsMenu(true)
@ -69,7 +70,7 @@ class SettingsSearchController :
// Initialize search menu // Initialize search menu
val searchItem = menu.findItem(R.id.action_search) val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView searchView = searchItem.actionView as SearchView
searchView.maxWidth = Int.MAX_VALUE searchView.maxWidth = Int.MAX_VALUE
// Change hint to show "search settings." // Change hint to show "search settings."
@ -80,22 +81,17 @@ class SettingsSearchController :
searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener { searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean { override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
searchView.onActionViewExpanded() // Required to show the query in the view
setItems(getResultSet())
return true return true
} }
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
searchView.onActionViewCollapsed() // Required to show the query in the view
router.popCurrentController() router.popCurrentController()
return true return false
} }
}) })
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean { override fun onQueryTextSubmit(query: String?): Boolean {
searchItem.collapseActionView()
setTitle(query) // Update toolbar title
setItems(getResultSet(query)) setItems(getResultSet(query))
return false return false
} }
@ -105,6 +101,8 @@ class SettingsSearchController :
return false return false
} }
}) })
searchView.setQuery(presenter.preferences.lastSearchQuerySearchSettings(), true)
} }
override fun onViewCreated(view: View) { override fun onViewCreated(view: View) {
@ -161,6 +159,10 @@ class SettingsSearchController :
* Opens a catalogue with the given search. * Opens a catalogue with the given search.
*/ */
override fun onTitleClick(ctrl: SettingsController) { override fun onTitleClick(ctrl: SettingsController) {
searchView.query.let {
presenter.preferences.lastSearchQuerySearchSettings(it.toString())
}
router.pushController(ctrl.withFadeTransaction()) router.pushController(ctrl.withFadeTransaction())
} }
} }