From 3e04619404ebf2f5a823275ad85e88984917679f Mon Sep 17 00:00:00 2001 From: MCAxiaz Date: Tue, 14 Jan 2020 22:13:01 -0800 Subject: [PATCH] Toolbar Menu Items Fix --- .../ui/base/controller/BaseController.kt | 22 +++++++- .../browse/BrowseCatalogueController.kt | 51 ++++++++++--------- .../ui/extension/ExtensionController.kt | 3 +- .../tachiyomi/ui/library/LibraryController.kt | 3 +- .../ui/setting/SettingsSourcesController.kt | 26 ++++++++-- app/src/main/res/menu/extension_main.xml | 2 +- 6 files changed, 76 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt index 860c4d5908..941e39a01b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt @@ -85,10 +85,11 @@ abstract class BaseController(bundle: Bundle? = null) : RestoreViewOnCreateContr } /** - * Workaround for disappearing menu items when collapsing an expandable item like a SearchView. + * Workaround for buggy menu item layout after expanding/collapsing an expandable item like a SearchView. * This method should be removed when fixed upstream. * Issue link: https://issuetracker.google.com/issues/37657375 */ + var expandActionViewFromInteraction = false fun MenuItem.fixExpand(onExpand: ((MenuItem) -> Boolean)? = null, onCollapse: ((MenuItem) -> Boolean)? = null) { setOnActionExpandListener(object : MenuItem.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem): Boolean { @@ -101,6 +102,25 @@ abstract class BaseController(bundle: Bundle? = null) : RestoreViewOnCreateContr return onCollapse?.invoke(item) ?: true } }) + + if (expandActionViewFromInteraction) { + expandActionViewFromInteraction = false + expandActionView() + } + } + + /** + * Workaround for menu items not disappearing when expanding an expandable item like a SearchView. + * [expandActionViewFromInteraction] should be set to true in [onOptionsItemSelected] when the expandable item is selected + * This method should be called as part of [MenuItem.OnActionExpandListener.onMenuItemActionExpand] + */ + fun invalidateMenuOnExpand(): Boolean { + return if (expandActionViewFromInteraction) { + activity?.invalidateOptionsMenu() + false + } else { + true + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 014e8bc428..b59cee9dde 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -261,36 +261,38 @@ open class BrowseCatalogueController(bundle: Bundle) : inflater.inflate(R.menu.catalogue_list, menu) // Initialize search menu - menu.findItem(R.id.action_search).apply { - val searchView = actionView as SearchView + val searchItem = menu.findItem(R.id.action_search) + val searchView = searchItem.actionView as SearchView - val query = presenter.query - if (!query.isBlank()) { - expandActionView() - searchView.setQuery(query, true) - searchView.clearFocus() - } + val query = presenter.query + if (!query.isBlank()) { + searchItem.expandActionView() + searchView.setQuery(query, true) + searchView.clearFocus() + } - val searchEventsObservable = searchView.queryTextChangeEvents() - .skip(1) - .filter { router.backstack.lastOrNull()?.controller() == this@BrowseCatalogueController } - .share() - val writingObservable = searchEventsObservable - .filter { !it.isSubmitted } - .debounce(1250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) - val submitObservable = searchEventsObservable - .filter { it.isSubmitted } + val searchEventsObservable = searchView.queryTextChangeEvents() + .skip(1) + .filter { router.backstack.lastOrNull()?.controller() == this@BrowseCatalogueController } + .share() + val writingObservable = searchEventsObservable + .filter { !it.isSubmitted } + .debounce(1250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) + val submitObservable = searchEventsObservable + .filter { it.isSubmitted } - searchViewSubscription?.unsubscribe() - searchViewSubscription = Observable.merge(writingObservable, submitObservable) - .map { it.queryText().toString() } - .subscribeUntilDestroy { searchWithQuery(it) } + searchViewSubscription?.unsubscribe() + searchViewSubscription = Observable.merge(writingObservable, submitObservable) + .map { it.queryText().toString() } + .subscribeUntilDestroy { searchWithQuery(it) } - fixExpand(onCollapse = { + searchItem.fixExpand( + onExpand = { invalidateMenuOnExpand() }, + onCollapse = { searchWithQuery("") true - }) - } + } + ) // Setup filters button menu.findItem(R.id.action_set_filter).apply { @@ -324,6 +326,7 @@ open class BrowseCatalogueController(bundle: Bundle) : override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { + R.id.action_search -> expandActionViewFromInteraction = true R.id.action_display_mode -> swapDisplayMode() R.id.action_set_filter -> navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) } R.id.action_open_in_browser -> openInBrowser() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt index 2a7dc944b9..fa5eba50fc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt @@ -85,6 +85,7 @@ open class ExtensionController : NucleusController(), override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { + R.id.action_search -> expandActionViewFromInteraction = true R.id.action_filter -> { router.pushController((RouterTransaction.with(SettingsExtensionsController())) .popChangeHandler(SettingsExtensionsFadeChangeHandler()) @@ -152,7 +153,7 @@ open class ExtensionController : NucleusController(), } // Fixes problem with the overflow icon showing up in lieu of search - searchItem.fixExpand() + searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) val autoItem = menu.findItem(R.id.action_auto_check) val preferences:PreferencesHelper = Injekt.get() 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 cc7bc18d15..4a578074a0 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 @@ -449,7 +449,7 @@ class LibraryController( searchRelay.call(query) } - searchItem.fixExpand() + searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) } fun search(query:String) { @@ -468,6 +468,7 @@ class LibraryController( override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { + R.id.action_search -> expandActionViewFromInteraction = true R.id.action_filter -> { navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSourcesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSourcesController.kt index 9280d87b9f..18d3ccbb05 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSourcesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSourcesController.kt @@ -185,16 +185,36 @@ class SettingsSourcesController : SettingsController(), } // Fixes problem with the overflow icon showing up in lieu of search - searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener { + searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) + } + + var expandActionViewFromInteraction = false + private fun MenuItem.fixExpand(onExpand: ((MenuItem) -> Boolean)? = null, onCollapse: ((MenuItem) -> Boolean)? = null) { + setOnActionExpandListener(object : MenuItem.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem): Boolean { - return true + return onExpand?.invoke(item) ?: true } override fun onMenuItemActionCollapse(item: MenuItem): Boolean { activity?.invalidateOptionsMenu() - return true + + return onCollapse?.invoke(item) ?: true } }) + + if (expandActionViewFromInteraction) { + expandActionViewFromInteraction = false + expandActionView() + } + } + + private fun invalidateMenuOnExpand(): Boolean { + return if (expandActionViewFromInteraction) { + activity?.invalidateOptionsMenu() + false + } else { + true + } } private fun drawSources() { diff --git a/app/src/main/res/menu/extension_main.xml b/app/src/main/res/menu/extension_main.xml index b2e7e5d773..d88188168f 100644 --- a/app/src/main/res/menu/extension_main.xml +++ b/app/src/main/res/menu/extension_main.xml @@ -12,7 +12,7 @@ android:id="@+id/action_filter" android:title="@string/action_filter" android:icon="@drawable/ic_filter_list_white_24dp" - app:showAsAction="always"/> + app:showAsAction="ifRoom"/>