diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 159622898..ce8dfe950 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -29,21 +29,6 @@ interface BookSourceDao { @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List - @Query("update book_sources set enabled = 1 where bookSourceUrl = :sourceUrl") - fun enableSection(sourceUrl: String) - - @Query("update book_sources set enabled = 0 where bookSourceUrl = :sourceUrl") - fun disableSection(sourceUrl: String) - - @Query("update book_sources set enabledExplore = 1 where bookSourceUrl = :sourceUrl") - fun enableSectionExplore(sourceUrl: String) - - @Query("update book_sources set enabledExplore = 0 where bookSourceUrl = :sourceUrl") - fun disableSectionExplore(sourceUrl: String) - - @Query("delete from book_sources where bookSourceUrl = :sourceUrl") - fun delSection(sourceUrl: String) - @Query("select * from book_sources where enabledExplore = 1 order by customOrder asc") fun observeFind(): DataSource.Factory @@ -78,7 +63,7 @@ interface BookSourceDao { fun update(vararg bookSource: BookSource) @Delete - fun delete(bookSource: BookSource) + fun delete(vararg bookSource: BookSource) @Query("delete from book_sources where bookSourceUrl = :key") fun delete(key: String) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index bab3a7b5e..78852a4ec 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -47,6 +47,18 @@ data class BookSource( var ruleToc: String? = null, // 目录页规则 var ruleContent: String? = null // 正文页规则 ) : Parcelable, JsExtensions { + + override fun hashCode(): Int { + return bookSourceUrl.hashCode() + } + + override fun equals(other: Any?): Boolean { + if (other is BookSource) { + return other.bookSourceUrl == bookSourceUrl + } + return false + } + @Ignore @IgnoredOnParcel private var searchRuleV: SearchRule? = null diff --git a/app/src/main/java/io/legado/app/service/help/CheckSource.kt b/app/src/main/java/io/legado/app/service/help/CheckSource.kt index 057813337..1f13723a2 100644 --- a/app/src/main/java/io/legado/app/service/help/CheckSource.kt +++ b/app/src/main/java/io/legado/app/service/help/CheckSource.kt @@ -4,16 +4,21 @@ import android.content.Context import android.content.Intent import io.legado.app.R import io.legado.app.constant.IntentAction +import io.legado.app.data.entities.BookSource import io.legado.app.service.CheckSourceService import org.jetbrains.anko.toast object CheckSource { - fun start(context: Context, selectedIds: ArrayList) { - if (selectedIds.isEmpty()) { + fun start(context: Context, sources: LinkedHashSet) { + if (sources.isEmpty()) { context.toast(R.string.non_select) return } + val selectedIds: ArrayList = arrayListOf() + sources.map { + selectedIds.add(it.bookSourceUrl) + } Intent(context, CheckSourceService::class.java).let { it.action = IntentAction.start it.putExtra("selectIds", selectedIds) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 82d823a0a..4f9fea1df 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -80,20 +80,18 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity when (item.itemId) { R.id.menu_add_book_source -> startActivity() R.id.menu_import_source_qr -> startActivityForResult(qrRequestCode) - R.id.menu_group_manage -> GroupManageDialog().show( - supportFragmentManager, - "groupManage" - ) + R.id.menu_group_manage -> + GroupManageDialog().show(supportFragmentManager, "groupManage") R.id.menu_import_source_local -> selectFileSys() 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_enable_explore -> viewModel.enableSelectExplore(adapter.getSelectionIds()) - R.id.menu_disable_explore -> viewModel.disableSelectExplore(adapter.getSelectionIds()) - R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelectionIds()) - R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelectionIds()) - R.id.menu_check_source -> CheckSource.start(this, ArrayList(adapter.getSelectionIds())) + R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelection()) + R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelection()) + R.id.menu_enable_explore -> viewModel.enableSelectExplore(adapter.getSelection()) + R.id.menu_disable_explore -> viewModel.disableSelectExplore(adapter.getSelection()) + R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection()) + R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelection()) + R.id.menu_check_source -> CheckSource.start(this, adapter.getSelection()) R.id.menu_import_source_onLine -> showImportDialog() } if (item.groupId == R.id.source_group) { 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 c7c432e16..e29a39d2b 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 @@ -16,31 +16,31 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_book_source), OnItemTouchCallbackListener { - private val selectedIds = linkedSetOf() + private val selected = linkedSetOf() fun selectAll() { getItems().forEach { - selectedIds.add(it.bookSourceUrl) + selected.add(it) } notifyItemRangeChanged(0, itemCount, 1) } fun revertSelection() { getItems().forEach { - if (selectedIds.contains(it.bookSourceUrl)) { - selectedIds.remove(it.bookSourceUrl) + if (selected.contains(it)) { + selected.remove(it) } else { - selectedIds.add(it.bookSourceUrl) + selected.add(it) } } notifyItemRangeChanged(0, itemCount, 1) } - fun getSelectionIds(): LinkedHashSet { - val selection = linkedSetOf() + fun getSelection(): LinkedHashSet { + val selection = linkedSetOf() getItems().map { - if (selectedIds.contains(it.bookSourceUrl)) { - selection.add(it.bookSourceUrl) + if (selected.contains(it)) { + selection.add(it) } } return selection @@ -81,12 +81,12 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : item.enabled = swt_enabled.isChecked callBack.update(item) } - cb_book_source.isChecked = selectedIds.contains(item.bookSourceUrl) + cb_book_source.isChecked = selected.contains(item) cb_book_source.setOnClickListener { if (cb_book_source.isChecked) { - selectedIds.add(item.bookSourceUrl) + selected.add(item) } else { - selectedIds.remove(item.bookSourceUrl) + selected.remove(item) } } iv_edit.onClick { callBack.edit(item) } @@ -105,7 +105,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } } else { when (payloads[0]) { - 1 -> cb_book_source.isChecked = selectedIds.contains(item.bookSourceUrl) + 1 -> cb_book_source.isChecked = selected.contains(item) 2 -> swt_enabled.isChecked = item.enabled } } 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 554ae4165..9bbaccfb9 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 @@ -43,56 +43,54 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } - fun enableSelection(ids: LinkedHashSet) { + fun enableSelection(sources: LinkedHashSet) { execute { - ids.forEach { - App.db.bookSourceDao().enableSection(it) + sources.forEach { + it.enabled = true } + App.db.bookSourceDao().update(*sources.toTypedArray()) } } - fun disableSelection(ids: LinkedHashSet) { + fun disableSelection(sources: LinkedHashSet) { execute { - ids.forEach { - App.db.bookSourceDao().disableSection(it) + sources.forEach { + it.enabled = false } + App.db.bookSourceDao().update(*sources.toTypedArray()) } } - fun enableSelectExplore(ids: LinkedHashSet) { + fun enableSelectExplore(sources: LinkedHashSet) { execute { - ids.forEach { - App.db.bookSourceDao().enableSectionExplore(it) + sources.forEach { + it.enabledExplore = true } + App.db.bookSourceDao().update(*sources.toTypedArray()) } } - fun disableSelectExplore(ids: LinkedHashSet) { + fun disableSelectExplore(sources: LinkedHashSet) { execute { - ids.forEach { - App.db.bookSourceDao().disableSectionExplore(it) + sources.forEach { + it.enabledExplore = false } + App.db.bookSourceDao().update(*sources.toTypedArray()) } } - fun delSelection(ids: LinkedHashSet) { + fun delSelection(sources: LinkedHashSet) { execute { - ids.forEach { - App.db.bookSourceDao().delSection(it) - } + App.db.bookSourceDao().delete(*sources.toTypedArray()) } } - fun exportSelection(ids: LinkedHashSet) { + fun exportSelection(sources: LinkedHashSet) { execute { - ids.map { - App.db.bookSourceDao().getBookSource(it) - }.let { - val json = GSON.toJson(it) - val file = - FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportBookSource.json") - file.writeText(json) - } + val json = GSON.toJson(sources) + val file = + FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportBookSource.json") + file.writeText(json) }.onSuccess { context.toast("成功导出至\n${Backup.exportPath}") }.onError {