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 6895039aa..1082ebd3c 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 @@ -23,7 +23,6 @@ import io.legado.app.utils.getViewModel import io.legado.app.utils.splitNotBlank import kotlinx.android.synthetic.main.activity_replace_rule.* import kotlinx.android.synthetic.main.view_search.* -import org.jetbrains.anko.doAsync class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule), @@ -33,7 +32,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi get() = getViewModel(ReplaceRuleViewModel::class.java) private lateinit var adapter: ReplaceRuleAdapter - private var allEnabled = false private var groups = hashSetOf() private var groupMenu: SubMenu? = null private var replaceRuleLiveData: LiveData>? = null @@ -62,6 +60,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi ReplaceEditDialog().show(supportFragmentManager, "replaceNew") R.id.menu_group_manage -> GroupManageDialog().show(supportFragmentManager, "groupManage") + R.id.menu_select_all -> adapter.selectAll() } return super.onCompatOptionsItemSelected(item) } @@ -100,6 +99,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } replaceRuleLiveData?.observe(this, Observer { val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it)) + adapter.selectedIds.clear() adapter.setItemsNoNotify(it) diffResult.dispatchUpdatesTo(adapter) }) @@ -122,13 +122,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } } - private fun updateEnableAll() { - doAsync { - App.db.replaceRuleDao().summary.let { - allEnabled = it == 0 - } - } - } override fun onQueryTextChange(newText: String?): Boolean { observeReplaceRuleData("%$newText%") 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 4386996f9..5057500f5 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 @@ -17,30 +17,56 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_replace_rule), ItemTouchCallback.OnItemTouchCallbackListener { + val selectedIds = linkedSetOf() + + fun selectAll() { + if (selectedIds.size == itemCount) { + selectedIds.clear() + notifyItemRangeChanged(0, itemCount, 1) + } else { + getItems().forEach { selectedIds.add(it.id) } + notifyItemRangeChanged(0, itemCount, 1) + } + } + override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList) { with(holder.itemView) { - this.setBackgroundColor(context.backgroundColor) - cb_name.text = item.name - swt_enabled.isChecked = item.isEnabled - swt_enabled.onClick { - item.isEnabled = swt_enabled.isChecked - callBack.update(item) - } - iv_edit.onClick { - callBack.edit(item) - } - iv_menu_more.onClick { - val popupMenu = PopupMenu(context, it) - popupMenu.menu.add(Menu.NONE, R.id.menu_top, Menu.NONE, R.string.to_top) - popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete) - popupMenu.setOnMenuItemClickListener { menuItem -> - when (menuItem.itemId) { - R.id.menu_top -> callBack.toTop(item) - R.id.menu_del -> callBack.delete(item) + if (payloads.isEmpty()) { + this.setBackgroundColor(context.backgroundColor) + cb_name.text = item.name + swt_enabled.isChecked = item.isEnabled + swt_enabled.onClick { + item.isEnabled = swt_enabled.isChecked + callBack.update(item) + } + iv_edit.onClick { + callBack.edit(item) + } + iv_menu_more.onClick { + val popupMenu = PopupMenu(context, it) + popupMenu.menu.add(Menu.NONE, R.id.menu_top, Menu.NONE, R.string.to_top) + popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete) + popupMenu.setOnMenuItemClickListener { menuItem -> + when (menuItem.itemId) { + R.id.menu_top -> callBack.toTop(item) + R.id.menu_del -> callBack.delete(item) + } + true + } + popupMenu.show() + } + cb_name.onClick { + if (cb_name.isChecked) { + selectedIds.add(item.id) + } else { + selectedIds.remove(item.id) } - true } - popupMenu.show() + cb_name.isChecked = selectedIds.contains(item.id) + } else { + when (payloads[0]) { + 1 -> cb_name.isChecked = selectedIds.contains(item.id) + } } } }