From 4840b7913983eb97d430bf38c527ce9a8c4d13f1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 6 Feb 2020 22:30:12 +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 --- .../book/source/manage/BookSourceViewModel.kt | 20 ++++++----- .../app/ui/replacerule/ReplaceRuleActivity.kt | 8 ++--- .../app/ui/replacerule/ReplaceRuleAdapter.kt | 26 +++++++------- .../ui/replacerule/ReplaceRuleViewModel.kt | 34 +++++++++++-------- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 9bbaccfb9..58618d88c 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -45,37 +45,41 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun enableSelection(sources: LinkedHashSet) { execute { - sources.forEach { + val list = ArrayList(sources) + list.forEach { it.enabled = true } - App.db.bookSourceDao().update(*sources.toTypedArray()) + App.db.bookSourceDao().update(*list.toTypedArray()) } } fun disableSelection(sources: LinkedHashSet) { execute { - sources.forEach { + val list = ArrayList(sources) + list.forEach { it.enabled = false } - App.db.bookSourceDao().update(*sources.toTypedArray()) + App.db.bookSourceDao().update(*list.toTypedArray()) } } fun enableSelectExplore(sources: LinkedHashSet) { execute { - sources.forEach { + val list = ArrayList(sources) + list.forEach { it.enabledExplore = true } - App.db.bookSourceDao().update(*sources.toTypedArray()) + App.db.bookSourceDao().update(*list.toTypedArray()) } } fun disableSelectExplore(sources: LinkedHashSet) { execute { - sources.forEach { + val list = ArrayList(sources) + list.forEach { it.enabledExplore = false } - App.db.bookSourceDao().update(*sources.toTypedArray()) + App.db.bookSourceDao().update(*list.toTypedArray()) } } 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 db37febd7..bb2335128 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 @@ -80,12 +80,12 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi 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.getSelectionIds()) - R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelectionIds()) - R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelectionIds()) + R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelection()) + R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelection()) + R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection()) R.id.menu_import_source_onLine -> showImportDialog() R.id.menu_import_source_local -> selectFileSys() - R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelectionIds()) + R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelection()) } 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 5fc617f38..f25c0e523 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 @@ -19,31 +19,31 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_replace_rule), ItemTouchCallback.OnItemTouchCallbackListener { - private val selectedIds = linkedSetOf() + private val selected = linkedSetOf() fun selectAll() { getItems().forEach { - selectedIds.add(it.id) + selected.add(it) } notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) } fun revertSelection() { getItems().forEach { - if (selectedIds.contains(it.id)) { - selectedIds.remove(it.id) + if (selected.contains(it)) { + selected.remove(it) } else { - selectedIds.add(it.id) + selected.add(it) } } notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) } - fun getSelectionIds(): LinkedHashSet { - val selection = linkedSetOf() + fun getSelection(): LinkedHashSet { + val selection = linkedSetOf() getItems().map { - if (selectedIds.contains(it.id)) { - selection.add(it.id) + if (selected.contains(it)) { + selection.add(it) } } return selection @@ -68,12 +68,12 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : iv_edit.onClick { callBack.edit(item) } - cb_name.isChecked = selectedIds.contains(item.id) + cb_name.isChecked = selected.contains(item) cb_name.onClick { if (cb_name.isChecked) { - selectedIds.add(item.id) + selected.add(item) } else { - selectedIds.remove(item.id) + selected.remove(item) } } iv_menu_more.onClick { @@ -92,7 +92,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : } else { bundle.keySet().map { when (it) { - "selected" -> cb_name.isChecked = selectedIds.contains(item.id) + "selected" -> cb_name.isChecked = selected.contains(item) "name", "group" -> if (item.group.isNullOrEmpty()) { cb_name.text = item.name 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 f8c9dda24..6390d69e7 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 @@ -63,34 +63,38 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application } } - fun enableSelection(ids: LinkedHashSet) { + fun enableSelection(rules: LinkedHashSet) { execute { - App.db.replaceRuleDao().enableSection(*ids.toLongArray()) + val list = ArrayList(rules) + list.forEach { + it.isEnabled = true + } + App.db.replaceRuleDao().update(*list.toTypedArray()) } } - fun disableSelection(ids: LinkedHashSet) { + fun disableSelection(rules: LinkedHashSet) { execute { - App.db.replaceRuleDao().disableSection(*ids.toLongArray()) + val list = ArrayList(rules) + list.forEach { + it.isEnabled = false + } + App.db.replaceRuleDao().update(*list.toTypedArray()) } } - fun delSelection(ids: LinkedHashSet) { + fun delSelection(rules: LinkedHashSet) { execute { - App.db.replaceRuleDao().delSection(*ids.toLongArray()) + App.db.replaceRuleDao().delete(*rules.toTypedArray()) } } - fun exportSelection(ids: LinkedHashSet) { + fun exportSelection(rules: LinkedHashSet) { execute { - ids.map { - App.db.replaceRuleDao().findById(it) - }.let { - val json = GSON.toJson(it) - val file = - FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportReplaceRule.json") - file.writeText(json) - } + val json = GSON.toJson(rules) + val file = + FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportReplaceRule.json") + file.writeText(json) }.onSuccess { context.toast("成功导出至\n${Backup.exportPath}") }.onError {