pull/32/head
kunfei 5 years ago
parent 518353117e
commit 0897c9f592
  1. 3
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 17
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt

@ -11,6 +11,9 @@ interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
fun liveDataAll(): LiveData<List<ReplaceRule>>
@Query("SELECT * FROM replace_rules where `group` like :key or name like :key ORDER BY sortOrder ASC")
fun liveDataSearch(key: String): LiveData<List<ReplaceRule>>
@get:Query("SELECT MIN(sortOrder) FROM replace_rules")
val minOrder: Int

@ -6,6 +6,7 @@ import android.view.MenuItem
import android.view.SubMenu
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
@ -35,11 +36,13 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
private var allEnabled = false
private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null
private var replaceRuleLiveData: LiveData<List<ReplaceRule>>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initSearchView()
initDataObservers()
observeReplaceRuleData()
observeGroupData()
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -88,13 +91,21 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
search_view.setOnQueryTextListener(this)
}
private fun initDataObservers() {
App.db.replaceRuleDao().liveDataAll().observe(this, Observer {
private fun observeReplaceRuleData(key: String? = null) {
replaceRuleLiveData?.removeObservers(this)
replaceRuleLiveData = if (key.isNullOrEmpty()) {
App.db.replaceRuleDao().liveDataAll()
} else {
App.db.replaceRuleDao().liveDataSearch(key)
}
replaceRuleLiveData?.observe(this, Observer {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)
})
}
private fun observeGroupData() {
App.db.replaceRuleDao().liveGroup().observe(this, Observer {
groups.clear()
it.map { group ->

Loading…
Cancel
Save