From b570626f3270abc76ae1d68c2bf663e87fa70cc5 Mon Sep 17 00:00:00 2001 From: atbest Date: Fri, 24 May 2019 01:23:23 -0400 Subject: [PATCH] Updated replace rule --- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 7 +++++- .../app/ui/replacerule/ReplaceRuleActivity.kt | 23 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) 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 9b92e4c6a..919855143 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 @@ -33,12 +33,17 @@ interface ReplaceRuleDao { @Query("SELECT * FROM replace_rules WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'") fun findEnabledByScope(scope: String): List + @get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM replace_rules") + val summary: Int + + @Query("UPDATE replace_rules SET isEnabled = :enable") + fun enableAll(enable: Boolean) + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg replaceRules: ReplaceRule) @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(replaceRule: ReplaceRule): Long - @Update fun update(vararg replaceRules: ReplaceRule) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 304000c53..464c5746b 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -18,6 +18,7 @@ import org.jetbrains.anko.toast class ReplaceRuleActivity : AppCompatActivity() { private lateinit var adapter: ReplaceRuleAdapter private var rulesLiveData: LiveData>? = null + private var allEnabled = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -32,12 +33,23 @@ class ReplaceRuleActivity : AppCompatActivity() { adapter = ReplaceRuleAdapter(this) adapter.onClickListener = object: ReplaceRuleAdapter.OnClickListener { override fun update(rule: ReplaceRule) { - doAsync { App.db.replaceRuleDao().update(rule) } + doAsync { + App.db.replaceRuleDao().update(rule) + updateEnableAll() + } } override fun delete(rule: ReplaceRule) { - doAsync { App.db.replaceRuleDao().delete(rule) } + doAsync { + App.db.replaceRuleDao().delete(rule) + updateEnableAll() + } } override fun edit(rule: ReplaceRule) { + doAsync { + App.db.replaceRuleDao().enableAll(!allEnabled) + allEnabled = !allEnabled + } + toast("Edit function not implemented!") } } @@ -50,4 +62,11 @@ class ReplaceRuleActivity : AppCompatActivity() { rulesLiveData?.observe(this, Observer> { adapter.submitList(it) }) } + private fun updateEnableAll() { + doAsync { + App.db.replaceRuleDao().summary.let { + allEnabled = it == 0 + } + } + } } \ No newline at end of file