From d6ea69a115aa5a811add63920670456d0b371103 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 8 Mar 2020 16:37:49 -0400 Subject: [PATCH] Action toolbar code cleanup --- .../controller/BottomActionMenuController.kt | 8 --- .../tachiyomi/ui/library/LibraryController.kt | 22 +++---- .../kanade/tachiyomi/ui/main/MainActivity.kt | 9 --- .../ui/recent/updates/UpdatesController.kt | 21 +++---- .../kanade/tachiyomi/widget/ActionToolbar.kt | 59 +++++++++++++++++++ .../tachiyomi/widget/BottomActionMenu.kt | 51 ---------------- ...ion_menu.xml => common_action_toolbar.xml} | 4 +- .../main/res/layout/library_controller.xml | 16 +++-- app/src/main/res/layout/main_activity.xml | 5 -- .../main/res/layout/updates_controller.xml | 9 ++- 10 files changed, 96 insertions(+), 108 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BottomActionMenuController.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/ActionToolbar.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt rename app/src/main/res/layout/{common_bottom_action_menu.xml => common_action_toolbar.xml} (89%) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BottomActionMenuController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BottomActionMenuController.kt deleted file mode 100644 index 9e19ff41b..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BottomActionMenuController.kt +++ /dev/null @@ -1,8 +0,0 @@ -package eu.kanade.tachiyomi.ui.base.controller - -import eu.kanade.tachiyomi.widget.BottomActionMenu - -interface BottomActionMenuController { - - fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu) -} 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 516dad6e5..b4c4127d9 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 @@ -32,7 +32,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault -import eu.kanade.tachiyomi.ui.base.controller.BottomActionMenuController import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.RootController import eu.kanade.tachiyomi.ui.base.controller.SecondaryDrawerController @@ -42,8 +41,8 @@ import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.inflate -import eu.kanade.tachiyomi.widget.BottomActionMenu import java.io.IOException +import kotlinx.android.synthetic.main.library_controller.action_toolbar import kotlinx.android.synthetic.main.library_controller.empty_view import kotlinx.android.synthetic.main.library_controller.library_pager import kotlinx.android.synthetic.main.main_activity.drawer @@ -60,7 +59,6 @@ class LibraryController( RootController, TabbedController, SecondaryDrawerController, - BottomActionMenuController, ActionMode.Callback, ChangeMangaCategoriesDialog.Listener, DeleteLibraryMangasDialog.Listener { @@ -75,7 +73,6 @@ class LibraryController( * Action mode for selections. */ private var actionMode: ActionMode? = null - private var bottomActionMenu: BottomActionMenu? = null /** * Library search query. @@ -184,6 +181,7 @@ class LibraryController( override fun onDestroyView(view: View) { destroyActionModeIfNeeded() + action_toolbar.destroy() adapter?.onDestroy() adapter = null tabsVisibilitySubscription?.unsubscribe() @@ -318,6 +316,10 @@ class LibraryController( fun createActionModeIfNeeded() { if (actionMode == null) { actionMode = (activity as AppCompatActivity).startSupportActionMode(this) + action_toolbar.show( + actionMode!!, + R.menu.library_selection + ) { onActionItemClicked(actionMode!!, it!!) } } } @@ -430,8 +432,7 @@ class LibraryController( } else { mode.title = count.toString() - bottomActionMenu?.show(mode.menuInflater) - bottomActionMenu?.findItem(R.id.action_edit_cover)?.isVisible = count == 1 + action_toolbar.findItem(R.id.action_edit_cover)?.isVisible = count == 1 } return false } @@ -451,20 +452,13 @@ class LibraryController( } override fun onDestroyActionMode(mode: ActionMode?) { - bottomActionMenu?.hide() + action_toolbar.hide() // Clear all the manga selections and notify child views. selectedMangas.clear() selectionRelay.call(LibrarySelectionEvent.Cleared()) actionMode = null } - override fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu) { - this.bottomActionMenu = bottomActionMenu - bottomActionMenu.configure( - R.menu.library_selection - ) { onActionItemClicked(actionMode!!, it!!) } - } - fun openManga(manga: Manga) { // Notify the presenter a manga is being opened. presenter.onOpenManga() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 63ead3fe6..d2613223d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.ui.base.activity.BaseActivity -import eu.kanade.tachiyomi.ui.base.controller.BottomActionMenuController import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController import eu.kanade.tachiyomi.ui.base.controller.RootController @@ -30,7 +29,6 @@ import eu.kanade.tachiyomi.ui.more.MoreController import eu.kanade.tachiyomi.ui.recent.history.HistoryController import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController import kotlinx.android.synthetic.main.main_activity.appbar -import kotlinx.android.synthetic.main.main_activity.bottom_action_menu import kotlinx.android.synthetic.main.main_activity.bottom_nav import kotlinx.android.synthetic.main.main_activity.drawer import kotlinx.android.synthetic.main.main_activity.tabs @@ -259,13 +257,6 @@ class MainActivity : BaseActivity() { } else { appbar.enableElevation() } - - if (from is BottomActionMenuController) { - bottom_action_menu.cleanup() - } - if (to is BottomActionMenuController) { - to.configureBottomActionMenu(bottom_action_menu) - } } companion object { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index c7fc7347b..3e7119a60 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -18,7 +18,6 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.notification.Notifications -import eu.kanade.tachiyomi.ui.base.controller.BottomActionMenuController import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.RootController @@ -28,7 +27,7 @@ import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.toast -import eu.kanade.tachiyomi.widget.BottomActionMenu +import kotlinx.android.synthetic.main.updates_controller.action_toolbar import kotlinx.android.synthetic.main.updates_controller.empty_view import kotlinx.android.synthetic.main.updates_controller.recycler import kotlinx.android.synthetic.main.updates_controller.swipe_refresh @@ -42,7 +41,6 @@ import timber.log.Timber class UpdatesController : NucleusController(), RootController, NoToolbarElevationController, - BottomActionMenuController, ActionMode.Callback, FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener, @@ -54,7 +52,6 @@ class UpdatesController : NucleusController(), * Action mode for multiple selection. */ private var actionMode: ActionMode? = null - private var bottomActionMenu: BottomActionMenu? = null /** * Adapter containing the recent chapters. @@ -108,6 +105,7 @@ class UpdatesController : NucleusController(), override fun onDestroyView(view: View) { destroyActionModeIfNeeded() + action_toolbar.destroy() adapter = null super.onDestroyView(view) } @@ -146,6 +144,10 @@ class UpdatesController : NucleusController(), override fun onItemLongClick(position: Int) { if (actionMode == null) { actionMode = (activity as AppCompatActivity).startSupportActionMode(this) + action_toolbar.show( + actionMode!!, + R.menu.updates_chapter_selection + ) { onActionItemClicked(actionMode!!, it!!) } } toggleSelection(position) @@ -294,8 +296,6 @@ class UpdatesController : NucleusController(), destroyActionModeIfNeeded() } else { mode.title = count.toString() - - bottomActionMenu?.show(mode.menuInflater) } return false @@ -324,19 +324,12 @@ class UpdatesController : NucleusController(), * @param mode the ActionMode object */ override fun onDestroyActionMode(mode: ActionMode?) { - bottomActionMenu?.hide() + action_toolbar.hide() adapter?.mode = SelectableAdapter.Mode.IDLE adapter?.clearSelection() actionMode = null } - override fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu) { - this.bottomActionMenu = bottomActionMenu - bottomActionMenu.configure( - R.menu.updates_chapter_selection - ) { onActionItemClicked(actionMode!!, it!!) } - } - private fun selectAll() { val adapter = adapter ?: return adapter.selectAll() diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/ActionToolbar.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/ActionToolbar.kt new file mode 100644 index 000000000..9761c7a76 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/ActionToolbar.kt @@ -0,0 +1,59 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.util.AttributeSet +import android.view.MenuItem +import android.view.View +import android.widget.FrameLayout +import androidx.annotation.IdRes +import androidx.annotation.MenuRes +import androidx.appcompat.view.ActionMode +import eu.kanade.tachiyomi.R +import kotlinx.android.synthetic.main.common_action_toolbar.view.common_action_menu +import kotlinx.android.synthetic.main.common_action_toolbar.view.common_action_toolbar + +/** + * A toolbar holding only menu items. + */ +class ActionToolbar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + FrameLayout(context, attrs) { + + init { + inflate(context, R.layout.common_action_toolbar, this) + } + + /** + * Remove menu items and remove listener. + */ + fun destroy() { + common_action_menu.menu.clear() + common_action_menu.setOnMenuItemClickListener(null) + } + + /** + * Gets a menu item if found. + */ + fun findItem(@IdRes itemId: Int): MenuItem? { + return common_action_menu.menu.findItem(itemId) + } + + /** + * Show the menu toolbar using the provided ActionMode's context to inflate the items. + */ + fun show(mode: ActionMode, @MenuRes menuRes: Int, listener: (item: MenuItem?) -> Boolean) { + // Avoid re-inflating the menu + if (common_action_menu.menu.size() == 0) { + mode.menuInflater.inflate(menuRes, common_action_menu.menu) + common_action_menu.setOnMenuItemClickListener { listener(it) } + } + + common_action_toolbar.visibility = View.VISIBLE + } + + /** + * Hide the menu toolbar. + */ + fun hide() { + common_action_toolbar.visibility = View.GONE + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt deleted file mode 100644 index 5d5584181..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt +++ /dev/null @@ -1,51 +0,0 @@ -package eu.kanade.tachiyomi.widget - -import android.content.Context -import android.util.AttributeSet -import android.view.MenuInflater -import android.view.MenuItem -import android.view.View -import android.widget.FrameLayout -import androidx.annotation.IdRes -import androidx.annotation.MenuRes -import eu.kanade.tachiyomi.R -import kotlinx.android.synthetic.main.common_bottom_action_menu.view.bottom_menu -import kotlinx.android.synthetic.main.common_bottom_action_menu.view.bottom_menu_bar - -class BottomActionMenu @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : - FrameLayout(context, attrs) { - - @MenuRes - private var menuRes: Int = 0 - - init { - inflate(context, R.layout.common_bottom_action_menu, this) - } - - fun configure(@MenuRes menuRes: Int, listener: (item: MenuItem?) -> Boolean) { - this.menuRes = menuRes - bottom_menu.setOnMenuItemClickListener { listener(it) } - } - - fun cleanup() { - bottom_menu.menu.clear() - bottom_menu.setOnMenuItemClickListener(null) - } - - fun findItem(@IdRes itemId: Int): MenuItem? { - return bottom_menu.menu.findItem(itemId) - } - - fun show(menuInflater: MenuInflater) { - // Avoid re-inflating the menu - if (bottom_menu.menu.size() == 0) { - menuInflater.inflate(menuRes, bottom_menu.menu) - } - - bottom_menu_bar.visibility = View.VISIBLE - } - - fun hide() { - bottom_menu_bar.visibility = View.GONE - } -} diff --git a/app/src/main/res/layout/common_bottom_action_menu.xml b/app/src/main/res/layout/common_action_toolbar.xml similarity index 89% rename from app/src/main/res/layout/common_bottom_action_menu.xml rename to app/src/main/res/layout/common_action_toolbar.xml index 010904f39..714240671 100644 --- a/app/src/main/res/layout/common_bottom_action_menu.xml +++ b/app/src/main/res/layout/common_action_toolbar.xml @@ -5,7 +5,7 @@ android:layout_height="wrap_content"> diff --git a/app/src/main/res/layout/library_controller.xml b/app/src/main/res/layout/library_controller.xml index 33748bbc9..9b531fa3c 100644 --- a/app/src/main/res/layout/library_controller.xml +++ b/app/src/main/res/layout/library_controller.xml @@ -1,12 +1,20 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> + android:layout_height="0dp" + android:layout_weight="1" /> + + - + diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 10eba24d3..5ebd768f1 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -42,11 +42,6 @@ android:layout_height="0dp" android:layout_weight="1" /> - - @@ -15,8 +14,16 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" + android:paddingBottom="?attr/actionBarSize" + android:clipToPadding="false" tools:listitem="@layout/updates_item" /> + +