From 7134d751db26d21454eefb2b86042f6d7528322d Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 20 Nov 2021 12:34:19 +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/TxtTocRuleDao.kt | 5 +++- .../ui/book/local/rule/TxtTocRuleActivity.kt | 6 ++-- .../ui/book/local/rule/TxtTocRuleViewModel.kt | 30 +++++++++++++++++++ .../ui/book/read/config/TocRegexViewModel.kt | 2 +- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt index 98925bc78..0449e0d3e 100644 --- a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt @@ -16,8 +16,11 @@ interface TxtTocRuleDao { @get:Query("select * from txtTocRules where enable = 1 order by serialNumber") val enabled: List + @get:Query("select ifNull(min(serialNumber), 0) from txtTocRules") + val minOrder: Int + @get:Query("select ifNull(max(serialNumber), 0) from txtTocRules") - val lastOrderNum: Int + val maxOrder: Int @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg rule: TxtTocRule) diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt index 66b49eba6..1bd891429 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt @@ -108,15 +108,15 @@ class TxtTocRuleActivity : VMBaseActivity + source.serialNumber = minOrder - index + } + appDb.txtTocRuleDao.update(*rules) + } + } + + fun toBottom(vararg sources: TxtTocRule) { + execute { + val maxOrder = appDb.txtTocRuleDao.maxOrder + 1 + sources.forEachIndexed { index, source -> + source.serialNumber = maxOrder + index + } + appDb.txtTocRuleDao.update(*sources) + } + } + + fun upOrder() { + execute { + val sources = appDb.txtTocRuleDao.all + for ((index: Int, source: TxtTocRule) in sources.withIndex()) { + source.serialNumber = index + 1 + } + appDb.txtTocRuleDao.update(*sources.toTypedArray()) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt index 2b5236e50..011f4debe 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt @@ -16,7 +16,7 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) { fun saveRule(rule: TxtTocRule) { execute { if (rule.serialNumber < 0) { - rule.serialNumber = appDb.txtTocRuleDao.lastOrderNum + 1 + rule.serialNumber = appDb.txtTocRuleDao.maxOrder + 1 } appDb.txtTocRuleDao.insert(rule) }