Browse Source

Abstract bottom action menu code to main activity

arkon 5 years ago
parent
commit
9f8046324d

+ 3 - 2
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)

+ 16 - 14
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<UpdatesPresenter>(),
+class UpdatesController() : NucleusController<UpdatesPresenter>(),
         RootController,
         NoToolbarElevationController,
         ActionMode.Callback,
@@ -49,10 +49,15 @@ class UpdatesController : NucleusController<UpdatesPresenter>(),
         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<UpdatesPresenter>(),
             // 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<UpdatesPresenter>(),
      * @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<UpdatesPresenter>(),
         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<UpdatesPresenter>(),
         } 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<UpdatesPresenter>(),
      * @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

+ 37 - 0
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()
+    }
+}

+ 25 - 0
app/src/main/res/layout/common_bottom_action_menu.xml

@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+
+    <androidx.appcompat.widget.Toolbar
+        android:id="@+id/bottom_menu_bar"
+        android:layout_width="match_parent"
+        android:layout_height="?attr/actionBarSize"
+        android:layout_gravity="bottom"
+        android:background="?attr/colorPrimary"
+        android:theme="?attr/actionBarTheme"
+        android:visibility="gone"
+        app:contentInsetStart="8dp"
+        app:contentInsetEnd="8dp">
+
+        <androidx.appcompat.widget.ActionMenuView
+            android:id="@+id/bottom_menu"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+
+    </androidx.appcompat.widget.Toolbar>
+
+</FrameLayout>

+ 5 - 0
app/src/main/res/layout/main_activity.xml

@@ -42,6 +42,11 @@
             android:layout_height="0dp"
             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
             android:id="@+id/bottom_nav"
             android:layout_width="match_parent"

+ 0 - 18
app/src/main/res/layout/updates_controller.xml

@@ -24,24 +24,6 @@
             android:layout_gravity="center"
             android:visibility="gone" />
 
-        <androidx.appcompat.widget.Toolbar
-            android:id="@+id/bottom_menu_bar"
-            android:layout_width="match_parent"
-            android:layout_height="?attr/actionBarSize"
-            android:layout_gravity="bottom"
-            android:background="?attr/colorPrimary"
-            android:theme="?attr/actionBarTheme"
-            android:visibility="gone"
-            app:contentInsetStart="8dp"
-            app:contentInsetEnd="8dp">
-
-            <androidx.appcompat.widget.ActionMenuView
-                android:id="@+id/bottom_menu"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent" />
-
-        </androidx.appcompat.widget.Toolbar>
-
     </FrameLayout>
 
 </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>