Updated replace rule

pull/32/head
atbest 6 years ago
parent 2f33336d74
commit b570626f32
  1. 7
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 23
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt

@ -33,12 +33,17 @@ interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'")
fun findEnabledByScope(scope: String): List<ReplaceRule>
@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)

@ -18,6 +18,7 @@ import org.jetbrains.anko.toast
class ReplaceRuleActivity : AppCompatActivity() {
private lateinit var adapter: ReplaceRuleAdapter
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = 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<PagedList<ReplaceRule>> { adapter.submitList(it) })
}
private fun updateEnableAll() {
doAsync {
App.db.replaceRuleDao().summary.let {
allEnabled = it == 0
}
}
}
}
Loading…
Cancel
Save