From 9f8046324dc4d7878c018b1b601c47a56b2e97ea Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 8 Mar 2020 13:57:50 -0400 Subject: [PATCH] Abstract bottom action menu code to main activity --- .../kanade/tachiyomi/ui/main/MainActivity.kt | 5 ++- .../ui/recent/updates/UpdatesController.kt | 30 ++++++++------- .../tachiyomi/widget/BottomActionMenu.kt | 37 +++++++++++++++++++ .../res/layout/common_bottom_action_menu.xml | 25 +++++++++++++ app/src/main/res/layout/main_activity.xml | 5 +++ .../main/res/layout/updates_controller.xml | 18 --------- 6 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt create mode 100644 app/src/main/res/layout/common_bottom_action_menu.xml 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 8fa4cbfa9b..2e97ad1545 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 @@ -29,6 +29,7 @@ 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 @@ -49,7 +50,7 @@ class MainActivity : BaseActivity() { } lateinit var tabAnimator: ViewHeightAnimator - lateinit var bottomNavAnimator: ViewHeightAnimator + private lateinit var bottomNavAnimator: ViewHeightAnimator override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -75,7 +76,7 @@ class MainActivity : BaseActivity() { if (currentRoot?.tag()?.toIntOrNull() != id) { when (id) { R.id.nav_library -> setRoot(LibraryController(), id) - R.id.nav_updates -> setRoot(UpdatesController(), id) + R.id.nav_updates -> setRoot(UpdatesController(bottom_action_menu), id) R.id.nav_history -> setRoot(HistoryController(), id) R.id.nav_catalogues -> setRoot(CatalogueController(), id) R.id.nav_more -> setRoot(MoreController(), id) 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 50f308f631..ad70d64e2f 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 @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode +import androidx.appcompat.widget.ActionMenuView import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.jakewharton.rxbinding.support.v4.widget.refreshes @@ -27,8 +28,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 kotlinx.android.synthetic.main.updates_controller.bottom_menu -import kotlinx.android.synthetic.main.updates_controller.bottom_menu_bar +import eu.kanade.tachiyomi.widget.BottomActionMenu 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 @@ -39,7 +39,7 @@ import timber.log.Timber * Uses [R.layout.updates_controller]. * UI related actions should be called from here. */ -class UpdatesController : NucleusController(), +class UpdatesController() : NucleusController(), RootController, NoToolbarElevationController, ActionMode.Callback, @@ -49,10 +49,15 @@ class UpdatesController : NucleusController(), ConfirmDeleteChaptersDialog.Listener, UpdatesAdapter.OnCoverClickListener { + constructor(bottomActionMenu: BottomActionMenu) : this() { + this.bottomActionMenu = bottomActionMenu + } + /** * Action mode for multiple selection. */ private var actionMode: ActionMode? = null + private var bottomActionMenu: BottomActionMenu? = null /** * Adapter containing the recent chapters. @@ -102,14 +107,11 @@ class UpdatesController : NucleusController(), // It can be a very long operation, so we disable swipe refresh and show a toast. swipe_refresh.isRefreshing = false } - - bottom_menu.setOnMenuItemClickListener { onActionItemClicked(actionMode!!, it) } } override fun onDestroyView(view: View) { adapter = null actionMode = null - bottom_menu.setOnMenuItemClickListener(null) super.onDestroyView(view) } @@ -145,8 +147,9 @@ class UpdatesController : NucleusController(), * @param position position of clicked item */ override fun onItemLongClick(position: Int) { - if (actionMode == null) + if (actionMode == null) { actionMode = (activity as AppCompatActivity).startSupportActionMode(this) + } toggleSelection(position) } @@ -285,11 +288,6 @@ class UpdatesController : NucleusController(), mode.menuInflater.inflate(R.menu.generic_selection, menu) adapter?.mode = SelectableAdapter.Mode.MULTI - // Avoid reinflating the menu multiple times - if (bottom_menu.menu.size() == 0) { - mode.menuInflater.inflate(R.menu.updates_chapter_selection, bottom_menu.menu) - } - return true } @@ -301,7 +299,11 @@ class UpdatesController : NucleusController(), } else { mode.title = count.toString() - bottom_menu_bar.visibility = View.VISIBLE + bottomActionMenu?.show( + mode.menuInflater, + R.menu.updates_chapter_selection, + ActionMenuView.OnMenuItemClickListener { onActionItemClicked(actionMode!!, it) } + ) } return false @@ -330,7 +332,7 @@ class UpdatesController : NucleusController(), * @param mode the ActionMode object */ override fun onDestroyActionMode(mode: ActionMode?) { - bottom_menu_bar.visibility = View.GONE + bottomActionMenu?.hide() adapter?.mode = SelectableAdapter.Mode.IDLE adapter?.clearSelection() actionMode = null diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt new file mode 100644 index 0000000000..635bbd31aa --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/BottomActionMenu.kt @@ -0,0 +1,37 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.util.AttributeSet +import android.view.MenuInflater +import android.view.View +import android.widget.FrameLayout +import androidx.annotation.MenuRes +import androidx.appcompat.widget.ActionMenuView +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) { + + init { + inflate(context, R.layout.common_bottom_action_menu, this) + } + + fun show(menuInflater: MenuInflater, @MenuRes menuRes: Int, listener: ActionMenuView.OnMenuItemClickListener) { + // Avoid re-inflating the menu + if (bottom_menu.menu.size() == 0) { + menuInflater.inflate(menuRes, bottom_menu.menu) + bottom_menu.setOnMenuItemClickListener(listener) + } + + bottom_menu_bar.visibility = View.VISIBLE + } + + fun hide() { + bottom_menu_bar.visibility = View.GONE + + bottom_menu.setOnMenuItemClickListener(null) + bottom_menu.menu.clear() + } +} diff --git a/app/src/main/res/layout/common_bottom_action_menu.xml b/app/src/main/res/layout/common_bottom_action_menu.xml new file mode 100644 index 0000000000..010904f392 --- /dev/null +++ b/app/src/main/res/layout/common_bottom_action_menu.xml @@ -0,0 +1,25 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 5ebd768f13..10eba24d3a 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -42,6 +42,11 @@ android:layout_height="0dp" android:layout_weight="1" /> + + - - - - - -