pull/1434/head
gedoor 3 years ago
parent 207bb56f56
commit 7134d751db
  1. 5
      app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt
  3. 30
      app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleViewModel.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt

@ -16,8 +16,11 @@ interface TxtTocRuleDao {
@get:Query("select * from txtTocRules where enable = 1 order by serialNumber") @get:Query("select * from txtTocRules where enable = 1 order by serialNumber")
val enabled: List<TxtTocRule> val enabled: List<TxtTocRule>
@get:Query("select ifNull(min(serialNumber), 0) from txtTocRules")
val minOrder: Int
@get:Query("select ifNull(max(serialNumber), 0) from txtTocRules") @get:Query("select ifNull(max(serialNumber), 0) from txtTocRules")
val lastOrderNum: Int val maxOrder: Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rule: TxtTocRule) fun insert(vararg rule: TxtTocRule)

@ -108,15 +108,15 @@ class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleV
} }
override fun toTop(source: TxtTocRule) { override fun toTop(source: TxtTocRule) {
viewModel.toTop(source)
} }
override fun toBottom(source: TxtTocRule) { override fun toBottom(source: TxtTocRule) {
viewModel.toBottom(source)
} }
override fun upOrder() { override fun upOrder() {
viewModel.upOrder()
} }
override fun upCountView() { override fun upCountView() {

@ -53,4 +53,34 @@ class TxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
} }
} }
fun toTop(vararg rules: TxtTocRule) {
execute {
val minOrder = appDb.txtTocRuleDao.minOrder - 1
rules.forEachIndexed { index, source ->
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())
}
}
} }

@ -16,7 +16,7 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) {
fun saveRule(rule: TxtTocRule) { fun saveRule(rule: TxtTocRule) {
execute { execute {
if (rule.serialNumber < 0) { if (rule.serialNumber < 0) {
rule.serialNumber = appDb.txtTocRuleDao.lastOrderNum + 1 rule.serialNumber = appDb.txtTocRuleDao.maxOrder + 1
} }
appDb.txtTocRuleDao.insert(rule) appDb.txtTocRuleDao.insert(rule)
} }

Loading…
Cancel
Save