From 763da19c9db0fd4e911f27df0d998122fcff3cce Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 10 Jul 2020 17:33:16 -0400 Subject: [PATCH] Add shortcut to global search query from library (closes #2183) --- .../browse/extension/ExtensionController.kt | 6 +- .../tachiyomi/ui/library/LibraryController.kt | 59 ++++++++++++++----- .../main/res/layout/library_controller.xml | 15 ++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionController.kt index eec9c7fcaa..58798a2ee3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionController.kt @@ -129,6 +129,9 @@ open class ExtensionController : val searchView = searchItem.actionView as SearchView searchView.maxWidth = Int.MAX_VALUE + // Fixes problem with the overflow icon showing up in lieu of search + searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) + if (query.isNotEmpty()) { searchItem.expandActionView() searchView.setQuery(query, true) @@ -142,9 +145,6 @@ open class ExtensionController : drawExtensions() } .launchIn(scope) - - // Fixes problem with the overflow icon showing up in lieu of search - searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) } override fun onItemClick(view: View, position: Int): Boolean { 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 4a3831fc45..25cda71f4b 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 @@ -33,18 +33,22 @@ import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.RootController import eu.kanade.tachiyomi.ui.base.controller.TabbedController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction +import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchController import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.offsetAppbarHeight import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.hasCustomCover import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.toast +import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.visible import kotlinx.android.synthetic.main.main_activity.tabs +import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import reactivecircus.flowbinding.android.view.clicks import reactivecircus.flowbinding.appcompat.queryTextChanges import reactivecircus.flowbinding.viewpager.pageSelections import rx.Subscription @@ -77,7 +81,7 @@ class LibraryController( /** * Library search query. */ - private var query = "" + private var query: String? = "" /** * Currently selected mangas. @@ -204,6 +208,14 @@ class LibraryController( binding.downloadedOnly.visible() } + binding.btnGlobalSearch.clicks() + .onEach { + router.pushController( + GlobalSearchController(query).withFadeTransaction() + ) + } + .launchIn(scope) + binding.actionToolbar.offsetAppbarHeight(activity!!) } @@ -364,33 +376,48 @@ class LibraryController( val searchItem = menu.findItem(R.id.action_search) val searchView = searchItem.actionView as SearchView searchView.maxWidth = Int.MAX_VALUE + searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) - searchView.queryTextChanges() - // Ignore events if this controller isn't at the top - .filter { router.backstack.lastOrNull()?.controller() == this } - .onEach { - query = it.toString() - searchRelay.call(query) - } - .launchIn(scope) - - if (query.isNotEmpty()) { + if (!query.isNullOrEmpty()) { searchItem.expandActionView() searchView.setQuery(query, true) searchView.clearFocus() - // Manually trigger the search since the binding doesn't trigger for some reason - searchRelay.call(query) + // If we re-enter the controller with a prior search still active + view?.post { + performSearch() + } } - searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) + searchView.queryTextChanges() + .distinctUntilChanged() + .onEach { + query = it.toString() + performSearch() + } + .launchIn(scope) // Mutate the filter icon because it needs to be tinted and the resource is shared. menu.findItem(R.id.action_filter).icon.mutate() } - fun search(query: String) { - this.query = query + fun search(query: String?) { + // Delay to let contents load first for searches from manga info + view?.post { + this.query = query + performSearch() + } + } + + private fun performSearch() { + searchRelay.call(query) + if (!query.isNullOrEmpty()) { + binding.btnGlobalSearch.visible() + binding.btnGlobalSearch.text = + resources?.getString(R.string.action_global_search_query, query) + } else { + binding.btnGlobalSearch.gone() + } } override fun onPrepareOptionsMenu(menu: Menu) { diff --git a/app/src/main/res/layout/library_controller.xml b/app/src/main/res/layout/library_controller.xml index c81728789e..75fdb112cb 100644 --- a/app/src/main/res/layout/library_controller.xml +++ b/app/src/main/res/layout/library_controller.xml @@ -1,6 +1,7 @@ @@ -14,7 +15,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/pale_green" - android:visibility="gone"> + android:visibility="gone" + tools:visibility="visible"> +