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") @Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
fun liveDataAll(): LiveData<List<ReplaceRule>> 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") @get:Query("SELECT MIN(sortOrder) FROM replace_rules")
val minOrder: Int val minOrder: Int

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

Loading…
Cancel
Save