pull/94/head
kunfei 5 years ago
parent 11182403f5
commit 4840b79139
  1. 20
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  2. 8
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  3. 26
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  4. 34
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt

@ -45,37 +45,41 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun enableSelection(sources: LinkedHashSet<BookSource>) {
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<BookSource>) {
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<BookSource>) {
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<BookSource>) {
execute {
sources.forEach {
val list = ArrayList(sources)
list.forEach {
it.enabledExplore = false
}
App.db.bookSourceDao().update(*sources.toTypedArray())
App.db.bookSourceDao().update(*list.toTypedArray())
}
}

@ -80,12 +80,12 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(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)
}

@ -19,31 +19,31 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
SimpleRecyclerAdapter<ReplaceRule>(context, R.layout.item_replace_rule),
ItemTouchCallback.OnItemTouchCallbackListener {
private val selectedIds = linkedSetOf<Long>()
private val selected = linkedSetOf<ReplaceRule>()
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<Long> {
val selection = linkedSetOf<Long>()
fun getSelection(): LinkedHashSet<ReplaceRule> {
val selection = linkedSetOf<ReplaceRule>()
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

@ -63,34 +63,38 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
}
}
fun enableSelection(ids: LinkedHashSet<Long>) {
fun enableSelection(rules: LinkedHashSet<ReplaceRule>) {
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<Long>) {
fun disableSelection(rules: LinkedHashSet<ReplaceRule>) {
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<Long>) {
fun delSelection(rules: LinkedHashSet<ReplaceRule>) {
execute {
App.db.replaceRuleDao().delSection(*ids.toLongArray())
App.db.replaceRuleDao().delete(*rules.toTypedArray())
}
}
fun exportSelection(ids: LinkedHashSet<Long>) {
fun exportSelection(rules: LinkedHashSet<ReplaceRule>) {
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 {

Loading…
Cancel
Save