From 53a0e9f1f88c2d25086ce2cf37ecf26266e3b5c0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 12 Sep 2022 19:30:56 +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 --- .../io/legado/app/data/dao/BookSourceDao.kt | 8 ++--- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 31 +++++++++++++++++-- .../io/legado/app/data/dao/RssSourceDao.kt | 8 ++--- .../ui/association/ImportReplaceRuleDialog.kt | 6 +--- .../app/ui/replace/GroupManageDialog.kt | 10 ++---- .../app/ui/replace/ReplaceRuleActivity.kt | 7 ++--- 6 files changed, 42 insertions(+), 28 deletions(-) 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 100a58694..a7d25a40a 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 @@ -131,8 +131,8 @@ interface BookSourceDao { @get:Query("select * from book_sources where enabled = 1 and bookSourceType = 0 order by customOrder") val allTextEnabled: List - @Query("select distinct bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''") - fun getAllGroupsUnProcessed(): List + @get:Query("select distinct bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''") + val allGroupsUnProcessed: List @Query("select * from book_sources where bookSourceUrl = :key") fun getBookSource(key: String): BookSource? @@ -158,7 +158,7 @@ interface BookSourceDao { @get:Query("select max(customOrder) from book_sources") val maxOrder: Int - fun dealGroups(list: List): List { + private fun dealGroups(list: List): List { val groups = linkedSetOf() list.forEach { it.splitNotBlank(AppPattern.splitGroupRegex).forEach { group -> @@ -172,7 +172,7 @@ interface BookSourceDao { val allGroups: List get() { - return dealGroups(getAllGroupsUnProcessed()) + return dealGroups(allGroupsUnProcessed) } fun flowGroups(): Flow> { 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 bd4f4a549..60a723710 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 @@ -1,8 +1,12 @@ package io.legado.app.data.dao import androidx.room.* +import io.legado.app.constant.AppPattern import io.legado.app.data.entities.ReplaceRule +import io.legado.app.utils.cnCompare +import io.legado.app.utils.splitNotBlank import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map @Dao @@ -18,7 +22,7 @@ interface ReplaceRuleDao { fun flowGroupSearch(key: String): Flow> @Query("select `group` from replace_rules where `group` is not null and `group` <> ''") - fun flowGroup(): Flow> + fun flowGroupsUnProcessed(): Flow> @get:Query("SELECT MIN(sortOrder) FROM replace_rules") val minOrder: Int @@ -30,7 +34,7 @@ interface ReplaceRuleDao { val all: List @get:Query("select distinct `group` from replace_rules where trim(`group`) <> ''") - val allGroup: List + val allGroupsUnProcessed: List @get:Query("SELECT * FROM replace_rules WHERE isEnabled = 1 ORDER BY sortOrder ASC") val allEnabled: List @@ -75,4 +79,27 @@ interface ReplaceRuleDao { @Delete fun delete(vararg replaceRules: ReplaceRule) + + private fun dealGroups(list: List): List { + val groups = linkedSetOf() + list.forEach { + it.splitNotBlank(AppPattern.splitGroupRegex).forEach { group -> + groups.add(group) + } + } + return groups.sortedWith { o1, o2 -> + o1.cnCompare(o2) + } + } + + val allGroups: List + get() { + return dealGroups(allGroupsUnProcessed) + } + + fun flowGroups(): Flow> { + return flowGroupsUnProcessed().map { list -> + dealGroups(list) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index 145745c89..cf91fdfad 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -83,8 +83,8 @@ interface RssSourceDao { @Query("select distinct sourceGroup from rssSources where trim(sourceGroup) <> '' and enabled = 1") fun flowGroupEnabled(): Flow> - @Query("select distinct sourceGroup from rssSources where trim(sourceGroup) <> ''") - fun getAllGroupsUnProcessed(): List + @get:Query("select distinct sourceGroup from rssSources where trim(sourceGroup) <> ''") + val allGroupsUnProcessed: List @get:Query("select min(customOrder) from rssSources") val minOrder: Int @@ -113,7 +113,7 @@ interface RssSourceDao { @Query("select 1 from rssSources where sourceUrl = :key") fun has(key: String): Boolean? - fun dealGroups(list: List): List { + private fun dealGroups(list: List): List { val groups = linkedSetOf() list.forEach { it.splitNotBlank(AppPattern.splitGroupRegex).forEach { group -> @@ -127,7 +127,7 @@ interface RssSourceDao { val allGroups: List get() { - return dealGroups(getAllGroupsUnProcessed()) + return dealGroups(allGroupsUnProcessed) } fun flowGroups(): Flow> { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt index e2d7474da..abc34de86 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt @@ -14,7 +14,6 @@ import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.constant.AppPattern import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.ReplaceRule @@ -134,10 +133,7 @@ class ImportReplaceRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_vi private fun alertCustomGroup(item: MenuItem) { alert(R.string.diy_edit_source_group) { val alertBinding = DialogCustomGroupBinding.inflate(layoutInflater).apply { - val groups = linkedSetOf() - appDb.replaceRuleDao.allGroup.forEach { group -> - groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) - } + val groups = appDb.replaceRuleDao.allGroups textInputLayout.setHint(R.string.group_name) editView.setFilterValues(groups.toList()) editView.dropDownHeight = 180.dpToPx() diff --git a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt index 84d49f1f9..4f54a77ed 100644 --- a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt @@ -13,7 +13,6 @@ import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogRecyclerViewBinding @@ -25,7 +24,6 @@ import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.applyTint import io.legado.app.utils.requestInputMethod import io.legado.app.utils.setLayout -import io.legado.app.utils.splitNotBlank import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch @@ -62,12 +60,8 @@ class GroupManageDialog : BaseDialogFragment(R.layout.dialog_recycler_view), private fun initData() { launch { - appDb.replaceRuleDao.flowGroup().conflate().collect { - val groups = linkedSetOf() - it.map { group -> - groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) - } - adapter.setItems(groups.toList()) + appDb.replaceRuleDao.flowGroups().conflate().collect { + adapter.setItems(it) } } } diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index 0dd286842..de2cb4919 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -15,7 +15,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppLog -import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.ReplaceRule import io.legado.app.databinding.ActivityReplaceRuleBinding @@ -205,11 +204,9 @@ class ReplaceRuleActivity : VMBaseActivity - groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) - } + groups.addAll(it) upGroupMenu() } }