From 2414f38afcbb10a9b548e0feacf7235ddafe636c Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 8 Sep 2019 10:22:28 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 3 ++ .../legado/app/ui/replacerule/DiffCallBack.kt | 12 ++++- .../app/ui/replacerule/ReplaceRuleActivity.kt | 2 + .../app/ui/replacerule/ReplaceRuleAdapter.kt | 28 ++++++++--- .../ui/replacerule/ReplaceRuleViewModel.kt | 6 +++ app/src/main/res/menu/replace_rule.xml | 49 +++++++++++++------ app/src/main/res/values/strings.xml | 3 ++ 7 files changed, 79 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index e57a2251b..e8a2be048 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -32,6 +32,9 @@ interface ReplaceRuleDao { @Query("SELECT * FROM replace_rules WHERE id in (:ids)") fun findByIds(vararg ids: Long): List + @Query("update replace_rules set isEnabled = 1 where id in (:ids)") + fun enableSection(vararg ids: Long) + @Query( """SELECT * FROM replace_rules WHERE isEnabled = 1 AND (scope LIKE '%' || :scope || '%' or scope = null or scope = '')""" diff --git a/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt index fc1f33b11..1824313b3 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt @@ -25,8 +25,18 @@ class DiffCallBack( val oldItem = oldItems[oldItemPosition] val newItem = newItems[newItemPosition] return oldItem.name == newItem.name + && oldItem.group == newItem.group && oldItem.isEnabled == newItem.isEnabled } - + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + return when { + oldItem.name == newItem.name + && oldItem.group == newItem.group + && oldItem.isEnabled != newItem.isEnabled -> 2 + else -> null + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 1082ebd3c..7ef6eea0e 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -61,6 +61,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi R.id.menu_group_manage -> GroupManageDialog().show(supportFragmentManager, "groupManage") R.id.menu_select_all -> adapter.selectAll() + R.id.menu_revert_selection -> adapter.revertSelection() + R.id.menu_enable_selection -> viewModel.enableSelection(adapter.selectedIds) } return super.onCompatOptionsItemSelected(item) } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index 5057500f5..e32b509ef 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -29,6 +29,17 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : } } + fun revertSelection() { + getItems().forEach { + if (selectedIds.contains(it.id)) { + selectedIds.remove(it.id) + } else { + selectedIds.add(it.id) + } + } + notifyItemRangeChanged(0, itemCount, 1) + } + override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList) { with(holder.itemView) { if (payloads.isEmpty()) { @@ -42,6 +53,14 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : iv_edit.onClick { callBack.edit(item) } + cb_name.isChecked = selectedIds.contains(item.id) + cb_name.onClick { + if (cb_name.isChecked) { + selectedIds.add(item.id) + } else { + selectedIds.remove(item.id) + } + } iv_menu_more.onClick { val popupMenu = PopupMenu(context, it) popupMenu.menu.add(Menu.NONE, R.id.menu_top, Menu.NONE, R.string.to_top) @@ -55,17 +74,10 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : } popupMenu.show() } - cb_name.onClick { - if (cb_name.isChecked) { - selectedIds.add(item.id) - } else { - selectedIds.remove(item.id) - } - } - cb_name.isChecked = selectedIds.contains(item.id) } else { when (payloads[0]) { 1 -> cb_name.isChecked = selectedIds.contains(item.id) + 2 -> swt_enabled.isChecked = item.isEnabled } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index ed566ab14..d6c874649 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -39,6 +39,12 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application } } + fun enableSelection(ids: LinkedHashSet) { + execute { + App.db.replaceRuleDao().enableSection(*ids.toLongArray()) + } + } + fun addGroup(group: String) { execute { val sources = App.db.replaceRuleDao().noGroup diff --git a/app/src/main/res/menu/replace_rule.xml b/app/src/main/res/menu/replace_rule.xml index b0551eee4..29b21845d 100644 --- a/app/src/main/res/menu/replace_rule.xml +++ b/app/src/main/res/menu/replace_rule.xml @@ -4,11 +4,42 @@ xmlns:tools="http://schemas.android.com/tools"> + tools:ignore="AlwaysShowAction"> + + + + + + + + + + + + + + + + - - - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8799447c5..b4a44b681 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -180,6 +180,7 @@ 清除缓存会删除所有已保存章节,是否确认删除? 书源共享 替换规则名称 + 选择操作 全选 夜间模式 启动页 @@ -496,5 +497,7 @@ 添加分组 新建替换 分组 + 启用所选 + 禁用所选