From bc7b23b036f4d800e6f69e217b577432a1e797c2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 16 Nov 2021 21:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/local/rule/TxtTocRuleActivity.kt | 31 ++++++- .../ui/book/local/rule/TxtTocRuleAdapter.kt | 85 ++++++++++++++++++- .../book/source/manage/BookSourceAdapter.kt | 4 +- .../main/res/layout/activity_txt_toc_rule.xml | 2 +- 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt index 0b09d970c..66b49eba6 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt @@ -5,21 +5,30 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.activity.viewModels +import androidx.recyclerview.widget.ItemTouchHelper import com.google.android.material.snackbar.Snackbar import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.data.appDb import io.legado.app.data.entities.TxtTocRule import io.legado.app.databinding.ActivityTxtTocRuleBinding import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogTocRegexEditBinding import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.theme.primaryColor +import io.legado.app.ui.widget.recycler.DragSelectTouchHelper +import io.legado.app.ui.widget.recycler.ItemTouchCallback +import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.ACache +import io.legado.app.utils.setEdgeEffectColor import io.legado.app.utils.snackbar import io.legado.app.utils.splitNotBlank import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class TxtTocRuleActivity : VMBaseActivity(), - TxtTocRuleAdapter.Callback { + TxtTocRuleAdapter.CallBack { override val viewModel: TxtTocRuleViewModel by viewModels() override val binding: ActivityTxtTocRuleBinding by viewBinding(ActivityTxtTocRuleBinding::inflate) @@ -30,11 +39,31 @@ class TxtTocRuleActivity : VMBaseActivity + adapter.setItems(tocRules) + upCountView() + } + } } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt index c222843fc..1b8a3f489 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt @@ -2,13 +2,18 @@ package io.legado.app.ui.book.local.rule import android.content.Context import android.view.ViewGroup +import androidx.core.os.bundleOf +import androidx.recyclerview.widget.RecyclerView import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.TxtTocRule import io.legado.app.databinding.ItemTxtTocRuleBinding +import io.legado.app.ui.widget.recycler.DragSelectTouchHelper +import io.legado.app.ui.widget.recycler.ItemTouchCallback -class TxtTocRuleAdapter(context: Context, private val callback: Callback) : - RecyclerAdapter(context) { +class TxtTocRuleAdapter(context: Context, private val callBack: CallBack) : + RecyclerAdapter(context), + ItemTouchCallback.Callback { private val selected = linkedSetOf() @@ -46,7 +51,7 @@ class TxtTocRuleAdapter(context: Context, private val callback: Callback) : } else { selected.remove(it) } - callback.upCountView() + callBack.upCountView() } } } @@ -62,7 +67,79 @@ class TxtTocRuleAdapter(context: Context, private val callback: Callback) : } } - interface Callback { + fun selectAll() { + getItems().forEach { + selected.add(it) + } + notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) + callBack.upCountView() + } + + fun revertSelection() { + getItems().forEach { + if (selected.contains(it)) { + selected.remove(it) + } else { + selected.add(it) + } + } + notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) + callBack.upCountView() + } + + override fun swap(srcPosition: Int, targetPosition: Int): Boolean { + val srcItem = getItem(srcPosition) + val targetItem = getItem(targetPosition) + if (srcItem != null && targetItem != null) { + if (srcItem.serialNumber == targetItem.serialNumber) { + callBack.upOrder() + } else { + val srcOrder = srcItem.serialNumber + srcItem.serialNumber = targetItem.serialNumber + targetItem.serialNumber = srcOrder + movedItems.add(srcItem) + movedItems.add(targetItem) + } + } + swapItem(srcPosition, targetPosition) + return true + } + + private val movedItems = hashSetOf() + + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (movedItems.isNotEmpty()) { + callBack.update(*movedItems.toTypedArray()) + movedItems.clear() + } + } + + val dragSelectCallback: DragSelectTouchHelper.Callback = + object : DragSelectTouchHelper.AdvanceCallback(Mode.ToggleAndReverse) { + override fun currentSelectedId(): MutableSet { + return selected + } + + override fun getItemId(position: Int): TxtTocRule { + return getItem(position)!! + } + + override fun updateSelectState(position: Int, isSelected: Boolean): Boolean { + getItem(position)?.let { + if (isSelected) { + selected.add(it) + } else { + selected.remove(it) + } + notifyItemChanged(position, bundleOf(Pair("selected", null))) + callBack.upCountView() + return true + } + return false + } + } + + interface CallBack { fun del(source: TxtTocRule) fun edit(source: TxtTocRule) fun update(vararg source: TxtTocRule) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index f84f01229..1c663c27b 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -19,7 +19,7 @@ import io.legado.app.lib.theme.backgroundColor import io.legado.app.model.Debug import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.widget.recycler.DragSelectTouchHelper -import io.legado.app.ui.widget.recycler.ItemTouchCallback.Callback +import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.utils.ColorUtils import io.legado.app.utils.invisible import io.legado.app.utils.startActivity @@ -28,7 +28,7 @@ import io.legado.app.utils.visible class BookSourceAdapter(context: Context, val callBack: CallBack) : RecyclerAdapter(context), - Callback { + ItemTouchCallback.Callback { private val selected = linkedSetOf() diff --git a/app/src/main/res/layout/activity_txt_toc_rule.xml b/app/src/main/res/layout/activity_txt_toc_rule.xml index 21f662944..7ac6263aa 100644 --- a/app/src/main/res/layout/activity_txt_toc_rule.xml +++ b/app/src/main/res/layout/activity_txt_toc_rule.xml @@ -12,7 +12,7 @@ app:contentInsetStartWithNavigation="0dp" app:contentLayout="@layout/view_search" app:displayHomeAsUp="true" - app:title="@string/book_source" /> + app:title="@string/txt_toc_regex" />