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) }