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 30e2522e9..e57a2251b 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 @@ -11,6 +11,9 @@ interface ReplaceRuleDao { @Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC") fun liveDataAll(): LiveData> + @Query("SELECT * FROM replace_rules where `group` like :key or name like :key ORDER BY sortOrder ASC") + fun liveDataSearch(key: String): LiveData> + @get:Query("SELECT MIN(sortOrder) FROM replace_rules") val minOrder: Int 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 4f45f4b23..f39ffd797 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 @@ -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(R.layout.activi private var allEnabled = false private var groups = hashSetOf() private var groupMenu: SubMenu? = null + private var replaceRuleLiveData: LiveData>? = null override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initSearchView() - initDataObservers() + observeReplaceRuleData() + observeGroupData() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -88,13 +91,21 @@ class ReplaceRuleActivity : VMBaseActivity(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 ->