added auto dismiss snackbar to category controller

This commit is contained in:
Jay 2019-11-11 19:21:42 -08:00
parent 223490d281
commit e2a36e49ce

View File

@ -3,8 +3,6 @@ package eu.kanade.tachiyomi.ui.category
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.* import android.view.*
import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.BaseTransientBottomBar
import com.jakewharton.rxbinding.view.clicks import com.jakewharton.rxbinding.view.clicks
@ -14,6 +12,7 @@ import eu.davidea.flexibleadapter.helpers.UndoHelper
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.util.doOnApplyWindowInsets import eu.kanade.tachiyomi.util.doOnApplyWindowInsets
import eu.kanade.tachiyomi.util.marginBottom import eu.kanade.tachiyomi.util.marginBottom
import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.snack
@ -47,7 +46,7 @@ class CategoryController : NucleusController<CategoryPresenter>(),
/** /**
* Undo helper used for restoring a deleted category. * Undo helper used for restoring a deleted category.
*/ */
private var undoHelper: Snackbar? = null private var snack: Snackbar? = null
/** /**
* Creates the presenter for this controller. Not to be manually called. * Creates the presenter for this controller. Not to be manually called.
@ -106,9 +105,9 @@ class CategoryController : NucleusController<CategoryPresenter>(),
*/ */
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
// Manually call callback to delete categories if required // Manually call callback to delete categories if required
undoHelper?.dismiss() snack?.dismiss()
confirmDelete() confirmDelete()
undoHelper = null snack = null
actionMode = null actionMode = null
adapter = null adapter = null
super.onDestroyView(view) super.onDestroyView(view)
@ -181,24 +180,22 @@ class CategoryController : NucleusController<CategoryPresenter>(),
when (item.itemId) { when (item.itemId) {
R.id.action_delete -> { R.id.action_delete -> {
//undoHelper = UndoHelper(adapter, this)
// undoHelper?.start(adapter.selectedPositions, view!!,
// R.string.snack_categories_deleted, R.string.action_undo, 3000)
adapter.removeItems(adapter.selectedPositions) adapter.removeItems(adapter.selectedPositions)
undoHelper = view?.snack(R.string.snack_categories_deleted, 3000) { snack =
var undoing = false view?.snack(R.string.snack_categories_deleted, Snackbar.LENGTH_INDEFINITE) {
setAction(R.string.action_undo) { var undoing = false
adapter.restoreDeletedItems() setAction(R.string.action_undo) {
undoing = true adapter.restoreDeletedItems()
} undoing = true
addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
if (!undoing)
confirmDelete()
} }
}) addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
} override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
if (!undoing) confirmDelete()
}
})
}
(activity as? MainActivity)?.setUndoSnackBar(snack)
mode.finish() mode.finish()
} }
R.id.action_edit -> { R.id.action_edit -> {
@ -299,7 +296,7 @@ class CategoryController : NucleusController<CategoryPresenter>(),
*/ */
override fun onActionCanceled(action: Int, positions: MutableList<Int>?) { override fun onActionCanceled(action: Int, positions: MutableList<Int>?) {
adapter?.restoreDeletedItems() adapter?.restoreDeletedItems()
undoHelper = null snack = null
} }
/** /**
@ -311,13 +308,13 @@ class CategoryController : NucleusController<CategoryPresenter>(),
override fun onActionConfirmed(action: Int, event: Int) { override fun onActionConfirmed(action: Int, event: Int) {
val adapter = adapter ?: return val adapter = adapter ?: return
presenter.deleteCategories(adapter.deletedItems.map { it.category }) presenter.deleteCategories(adapter.deletedItems.map { it.category })
undoHelper = null snack = null
} }
fun confirmDelete() { fun confirmDelete() {
val adapter = adapter ?: return val adapter = adapter ?: return
presenter.deleteCategories(adapter.deletedItems.map { it.category }) presenter.deleteCategories(adapter.deletedItems.map { it.category })
undoHelper = null snack = null
} }
/** /**