force menu invalidation when expanding actionView from user interaction to properly layout menu items (#2503)

This commit is contained in:
MCAxiaz 2020-01-16 18:44:22 -08:00 committed by arkon
parent 73fbc81067
commit bed978a26a
4 changed files with 57 additions and 32 deletions

View File

@ -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. * This method should be removed when fixed upstream.
* Issue link: https://issuetracker.google.com/issues/37657375 * Issue link: https://issuetracker.google.com/issues/37657375
*/ */
var expandActionViewFromInteraction = false
fun MenuItem.fixExpand(onExpand: ((MenuItem) -> Boolean)? = null, onCollapse: ((MenuItem) -> Boolean)? = null) { fun MenuItem.fixExpand(onExpand: ((MenuItem) -> Boolean)? = null, onCollapse: ((MenuItem) -> Boolean)? = null) {
setOnActionExpandListener(object : MenuItem.OnActionExpandListener { setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem): Boolean { override fun onMenuItemActionExpand(item: MenuItem): Boolean {
@ -101,6 +102,25 @@ abstract class BaseController(bundle: Bundle? = null) : RestoreViewOnCreateContr
return onCollapse?.invoke(item) ?: true 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
}
} }
} }

View File

@ -215,12 +215,12 @@ open class BrowseCatalogueController(bundle: Bundle) :
inflater.inflate(R.menu.catalogue_list, menu) inflater.inflate(R.menu.catalogue_list, menu)
// Initialize search menu // Initialize search menu
menu.findItem(R.id.action_search).apply { val searchItem = menu.findItem(R.id.action_search)
val searchView = actionView as SearchView val searchView = searchItem.actionView as SearchView
val query = presenter.query val query = presenter.query
if (!query.isBlank()) { if (!query.isBlank()) {
expandActionView() searchItem.expandActionView()
searchView.setQuery(query, true) searchView.setQuery(query, true)
searchView.clearFocus() searchView.clearFocus()
} }
@ -240,11 +240,13 @@ open class BrowseCatalogueController(bundle: Bundle) :
.map { it.queryText().toString() } .map { it.queryText().toString() }
.subscribeUntilDestroy { searchWithQuery(it) } .subscribeUntilDestroy { searchWithQuery(it) }
fixExpand(onCollapse = { searchItem.fixExpand(
onExpand = { invalidateMenuOnExpand() },
onCollapse = {
searchWithQuery("") searchWithQuery("")
true true
})
} }
)
// Setup filters button // Setup filters button
menu.findItem(R.id.action_set_filter).apply { menu.findItem(R.id.action_set_filter).apply {
@ -278,6 +280,7 @@ open class BrowseCatalogueController(bundle: Bundle) :
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.action_search -> expandActionViewFromInteraction = true
R.id.action_display_mode -> swapDisplayMode() R.id.action_display_mode -> swapDisplayMode()
R.id.action_set_filter -> navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) } R.id.action_set_filter -> navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) }
R.id.action_open_in_browser -> openInBrowser() R.id.action_open_in_browser -> openInBrowser()

View File

@ -80,6 +80,7 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.action_search -> expandActionViewFromInteraction = true
R.id.action_settings -> { R.id.action_settings -> {
router.pushController((RouterTransaction.with(ExtensionFilterController())) router.pushController((RouterTransaction.with(ExtensionFilterController()))
.popChangeHandler(SettingsExtensionsFadeChangeHandler()) .popChangeHandler(SettingsExtensionsFadeChangeHandler())
@ -137,7 +138,7 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
} }
// Fixes problem with the overflow icon showing up in lieu of search // Fixes problem with the overflow icon showing up in lieu of search
searchItem.fixExpand() searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() })
} }
override fun onItemClick(view: View, position: Int): Boolean { override fun onItemClick(view: View, position: Int): Boolean {

View File

@ -321,7 +321,7 @@ class LibraryController(
val searchItem = menu.findItem(R.id.action_search) val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView val searchView = searchItem.actionView as SearchView
if (!query.isEmpty()) { if (query.isNotEmpty()) {
searchItem.expandActionView() searchItem.expandActionView()
searchView.setQuery(query, true) searchView.setQuery(query, true)
searchView.clearFocus() searchView.clearFocus()
@ -339,7 +339,7 @@ class LibraryController(
searchRelay.call(query) searchRelay.call(query)
} }
searchItem.fixExpand() searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() })
} }
override fun onPrepareOptionsMenu(menu: Menu) { override fun onPrepareOptionsMenu(menu: Menu) {
@ -354,6 +354,7 @@ class LibraryController(
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.action_search -> expandActionViewFromInteraction = true
R.id.action_filter -> { R.id.action_filter -> {
navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) } navView?.let { activity?.drawer?.openDrawer(GravityCompat.END) }
} }