From f762598c5c54bd8fcb9ff308af94715c9dc31c35 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 31 Jul 2020 12:18:47 -0400 Subject: [PATCH] Fix library search query being lost when returning (closes #3473) --- .../ui/library/LibraryCategoryAdapter.kt | 5 +--- .../ui/library/LibraryCategoryView.kt | 2 +- .../tachiyomi/ui/library/LibraryController.kt | 29 +++++++++---------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index 5b6e0c787..11246b4e0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -38,10 +38,7 @@ class LibraryCategoryAdapter(view: LibraryCategoryView) : } fun performFilter() { - var s = getFilter(String::class.java) - if (s == null) { - s = "" - } + val s = getFilter(String::class.java) ?: "" updateDataSet(mangas.filter { it.filter(s) }) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index a9d1bc923..9a10c39bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -168,7 +168,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att * * @param event the event received. */ - fun onNextLibraryManga(event: LibraryMangaEvent) { + private fun onNextLibraryManga(event: LibraryMangaEvent) { // Get the manga list for this category. val mangaForCategory = event.getMangaForCategory(category).orEmpty() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 3377df4d5..69d30673d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -38,8 +38,9 @@ import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.toast import kotlinx.android.synthetic.main.main_activity.tabs -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.drop +import kotlinx.coroutines.flow.dropWhile +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import reactivecircus.flowbinding.android.view.clicks @@ -72,7 +73,7 @@ class LibraryController( /** * Library search query. */ - private var query: String? = "" + private var query: String = "" /** * Currently selected mangas. @@ -367,19 +368,20 @@ class LibraryController( searchView.maxWidth = Int.MAX_VALUE searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) - if (!query.isNullOrEmpty()) { + if (query.isNotEmpty()) { searchItem.expandActionView() searchView.setQuery(query, true) searchView.clearFocus() - // If we re-enter the controller with a prior search still active - view?.post { - performSearch() - } + performSearch() } searchView.queryTextChanges() - .distinctUntilChanged() + // Ignore events if this controller isn't at the top to avoid query being reset + .filter { router.backstack.lastOrNull()?.controller() == this } + // If we re-enter the controller with a prior search still active, but searchview + // content sometimes appears empty. + .dropWhile { query.isNotEmpty() && query != it.toString() } .onEach { query = it.toString() performSearch() @@ -390,17 +392,14 @@ class LibraryController( menu.findItem(R.id.action_filter).icon.mutate() } - fun search(query: String?) { - // Delay to let contents load first for searches from manga info - view?.post { - this.query = query - performSearch() - } + fun search(query: String) { + this.query = query + performSearch() } private fun performSearch() { searchRelay.call(query) - if (!query.isNullOrEmpty()) { + if (query.isNotEmpty()) { binding.btnGlobalSearch.isVisible = true binding.btnGlobalSearch.text = resources?.getString(R.string.action_global_search_query, query)