Action toolbar code cleanup

This commit is contained in:
arkon 2020-03-08 16:37:49 -04:00
parent 4358f9fd2a
commit d6ea69a115
10 changed files with 96 additions and 108 deletions

View file

@ -1,8 +0,0 @@
package eu.kanade.tachiyomi.ui.base.controller
import eu.kanade.tachiyomi.widget.BottomActionMenu
interface BottomActionMenuController {
fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu)
}

View file

@ -32,7 +32,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault 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.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.RootController import eu.kanade.tachiyomi.ui.base.controller.RootController
import eu.kanade.tachiyomi.ui.base.controller.SecondaryDrawerController 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.ui.manga.MangaController
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.inflate import eu.kanade.tachiyomi.util.view.inflate
import eu.kanade.tachiyomi.widget.BottomActionMenu
import java.io.IOException 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.empty_view
import kotlinx.android.synthetic.main.library_controller.library_pager import kotlinx.android.synthetic.main.library_controller.library_pager
import kotlinx.android.synthetic.main.main_activity.drawer import kotlinx.android.synthetic.main.main_activity.drawer
@ -60,7 +59,6 @@ class LibraryController(
RootController, RootController,
TabbedController, TabbedController,
SecondaryDrawerController, SecondaryDrawerController,
BottomActionMenuController,
ActionMode.Callback, ActionMode.Callback,
ChangeMangaCategoriesDialog.Listener, ChangeMangaCategoriesDialog.Listener,
DeleteLibraryMangasDialog.Listener { DeleteLibraryMangasDialog.Listener {
@ -75,7 +73,6 @@ class LibraryController(
* Action mode for selections. * Action mode for selections.
*/ */
private var actionMode: ActionMode? = null private var actionMode: ActionMode? = null
private var bottomActionMenu: BottomActionMenu? = null
/** /**
* Library search query. * Library search query.
@ -184,6 +181,7 @@ class LibraryController(
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
destroyActionModeIfNeeded() destroyActionModeIfNeeded()
action_toolbar.destroy()
adapter?.onDestroy() adapter?.onDestroy()
adapter = null adapter = null
tabsVisibilitySubscription?.unsubscribe() tabsVisibilitySubscription?.unsubscribe()
@ -318,6 +316,10 @@ class LibraryController(
fun createActionModeIfNeeded() { fun createActionModeIfNeeded() {
if (actionMode == null) { if (actionMode == null) {
actionMode = (activity as AppCompatActivity).startSupportActionMode(this) actionMode = (activity as AppCompatActivity).startSupportActionMode(this)
action_toolbar.show(
actionMode!!,
R.menu.library_selection
) { onActionItemClicked(actionMode!!, it!!) }
} }
} }
@ -430,8 +432,7 @@ class LibraryController(
} else { } else {
mode.title = count.toString() mode.title = count.toString()
bottomActionMenu?.show(mode.menuInflater) action_toolbar.findItem(R.id.action_edit_cover)?.isVisible = count == 1
bottomActionMenu?.findItem(R.id.action_edit_cover)?.isVisible = count == 1
} }
return false return false
} }
@ -451,20 +452,13 @@ class LibraryController(
} }
override fun onDestroyActionMode(mode: ActionMode?) { override fun onDestroyActionMode(mode: ActionMode?) {
bottomActionMenu?.hide() action_toolbar.hide()
// Clear all the manga selections and notify child views. // Clear all the manga selections and notify child views.
selectedMangas.clear() selectedMangas.clear()
selectionRelay.call(LibrarySelectionEvent.Cleared()) selectionRelay.call(LibrarySelectionEvent.Cleared())
actionMode = null actionMode = null
} }
override fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu) {
this.bottomActionMenu = bottomActionMenu
bottomActionMenu.configure(
R.menu.library_selection
) { onActionItemClicked(actionMode!!, it!!) }
}
fun openManga(manga: Manga) { fun openManga(manga: Manga) {
// Notify the presenter a manga is being opened. // Notify the presenter a manga is being opened.
presenter.onOpenManga() presenter.onOpenManga()

View file

@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.Migrations
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity 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.DialogController
import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController
import eu.kanade.tachiyomi.ui.base.controller.RootController 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.history.HistoryController
import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController
import kotlinx.android.synthetic.main.main_activity.appbar 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.bottom_nav
import kotlinx.android.synthetic.main.main_activity.drawer import kotlinx.android.synthetic.main.main_activity.drawer
import kotlinx.android.synthetic.main.main_activity.tabs import kotlinx.android.synthetic.main.main_activity.tabs
@ -259,13 +257,6 @@ class MainActivity : BaseActivity() {
} else { } else {
appbar.enableElevation() appbar.enableElevation()
} }
if (from is BottomActionMenuController) {
bottom_action_menu.cleanup()
}
if (to is BottomActionMenuController) {
to.configureBottomActionMenu(bottom_action_menu)
}
} }
companion object { companion object {

View file

@ -18,7 +18,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.Notifications 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.NoToolbarElevationController
import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.RootController 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.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
import eu.kanade.tachiyomi.util.system.toast 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.empty_view
import kotlinx.android.synthetic.main.updates_controller.recycler import kotlinx.android.synthetic.main.updates_controller.recycler
import kotlinx.android.synthetic.main.updates_controller.swipe_refresh import kotlinx.android.synthetic.main.updates_controller.swipe_refresh
@ -42,7 +41,6 @@ import timber.log.Timber
class UpdatesController : NucleusController<UpdatesPresenter>(), class UpdatesController : NucleusController<UpdatesPresenter>(),
RootController, RootController,
NoToolbarElevationController, NoToolbarElevationController,
BottomActionMenuController,
ActionMode.Callback, ActionMode.Callback,
FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener, FlexibleAdapter.OnItemLongClickListener,
@ -54,7 +52,6 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
* Action mode for multiple selection. * Action mode for multiple selection.
*/ */
private var actionMode: ActionMode? = null private var actionMode: ActionMode? = null
private var bottomActionMenu: BottomActionMenu? = null
/** /**
* Adapter containing the recent chapters. * Adapter containing the recent chapters.
@ -108,6 +105,7 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
destroyActionModeIfNeeded() destroyActionModeIfNeeded()
action_toolbar.destroy()
adapter = null adapter = null
super.onDestroyView(view) super.onDestroyView(view)
} }
@ -146,6 +144,10 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
if (actionMode == null) { if (actionMode == null) {
actionMode = (activity as AppCompatActivity).startSupportActionMode(this) actionMode = (activity as AppCompatActivity).startSupportActionMode(this)
action_toolbar.show(
actionMode!!,
R.menu.updates_chapter_selection
) { onActionItemClicked(actionMode!!, it!!) }
} }
toggleSelection(position) toggleSelection(position)
@ -294,8 +296,6 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
destroyActionModeIfNeeded() destroyActionModeIfNeeded()
} else { } else {
mode.title = count.toString() mode.title = count.toString()
bottomActionMenu?.show(mode.menuInflater)
} }
return false return false
@ -324,19 +324,12 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
* @param mode the ActionMode object * @param mode the ActionMode object
*/ */
override fun onDestroyActionMode(mode: ActionMode?) { override fun onDestroyActionMode(mode: ActionMode?) {
bottomActionMenu?.hide() action_toolbar.hide()
adapter?.mode = SelectableAdapter.Mode.IDLE adapter?.mode = SelectableAdapter.Mode.IDLE
adapter?.clearSelection() adapter?.clearSelection()
actionMode = null actionMode = null
} }
override fun configureBottomActionMenu(bottomActionMenu: BottomActionMenu) {
this.bottomActionMenu = bottomActionMenu
bottomActionMenu.configure(
R.menu.updates_chapter_selection
) { onActionItemClicked(actionMode!!, it!!) }
}
private fun selectAll() { private fun selectAll() {
val adapter = adapter ?: return val adapter = adapter ?: return
adapter.selectAll() adapter.selectAll()

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -5,7 +5,7 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar <androidx.appcompat.widget.Toolbar
android:id="@+id/bottom_menu_bar" android:id="@+id/common_action_toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom" android:layout_gravity="bottom"
@ -16,7 +16,7 @@
app:contentInsetEnd="8dp"> app:contentInsetEnd="8dp">
<androidx.appcompat.widget.ActionMenuView <androidx.appcompat.widget.ActionMenuView
android:id="@+id/bottom_menu" android:id="@+id/common_action_menu"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View file

@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager <androidx.viewpager.widget.ViewPager
android:id="@+id/library_pager" android:id="@+id/library_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="0dp"
android:layout_weight="1" />
<eu.kanade.tachiyomi.widget.ActionToolbar
android:id="@+id/action_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
<eu.kanade.tachiyomi.widget.EmptyView <eu.kanade.tachiyomi.widget.EmptyView
android:id="@+id/empty_view" android:id="@+id/empty_view"
@ -15,4 +23,4 @@
android:layout_gravity="center" android:layout_gravity="center"
android:visibility="gone" /> android:visibility="gone" />
</FrameLayout> </LinearLayout>

View file

@ -42,11 +42,6 @@
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" /> android:layout_weight="1" />
<eu.kanade.tachiyomi.widget.BottomActionMenu
android:id="@+id/bottom_action_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav" android:id="@+id/bottom_nav"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_refresh" android:id="@+id/swipe_refresh"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -15,8 +14,16 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" android:descendantFocusability="blocksDescendants"
android:paddingBottom="?attr/actionBarSize"
android:clipToPadding="false"
tools:listitem="@layout/updates_item" /> tools:listitem="@layout/updates_item" />
<eu.kanade.tachiyomi.widget.ActionToolbar
android:id="@+id/action_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
<eu.kanade.tachiyomi.widget.EmptyView <eu.kanade.tachiyomi.widget.EmptyView
android:id="@+id/empty_view" android:id="@+id/empty_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"