diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 3655e0117..c2f82bf13 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -12,10 +12,10 @@ interface BookSourceDao { fun liveDataAll(): LiveData> @Query("select * from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey or bookSourceComment like :searchKey order by customOrder asc") - fun liveDataSearch(searchKey: String = ""): LiveData> + fun liveDataSearch(searchKey: String): LiveData> @Query("select * from book_sources where bookSourceGroup like :searchKey order by customOrder asc") - fun liveDataGroupSearch(searchKey: String = ""): LiveData> + fun liveDataGroupSearch(searchKey: String): LiveData> @Query("select * from book_sources where enabled = 1 order by customOrder asc") fun liveDataEnabled(): LiveData> diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index ad2b25706..001877246 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -28,9 +28,15 @@ interface RssSourceDao { @Query("SELECT * FROM rssSources where sourceGroup like :key order by customOrder") fun liveGroupSearch(key: String): LiveData> - @Query("SELECT * FROM rssSources where enabled = 1 and (sourceName like '%'||:searchKey||'%' or sourceGroup like '%'||:searchKey||'%' or sourceUrl like '%'||:searchKey||'%') order by customOrder") + @Query("SELECT * FROM rssSources where enabled = 1 order by customOrder") + fun liveEnabled(): LiveData> + + @Query("SELECT * FROM rssSources where enabled = 1 and (sourceName like :searchKey or sourceGroup like :searchKey or sourceUrl like :searchKey) order by customOrder") fun liveEnabled(searchKey: String): LiveData> + @Query("SELECT * FROM rssSources where enabled = 1 and sourceGroup like :searchKey order by customOrder") + fun liveEnabledByGroup(searchKey: String): LiveData> + @Query("select sourceGroup from rssSources where trim(sourceGroup) <> ''") fun liveGroup(): LiveData> diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index 60db06649..8cc1a1f00 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -92,7 +92,7 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } override fun onQueryTextChange(newText: String?): Boolean { - initData(newText ?: "") + initData(newText) return false } }) @@ -113,9 +113,16 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } } - private fun initData(searchKey: String = "") { + private fun initData(searchKey: String? = null) { liveRssData?.removeObservers(this) - liveRssData = App.db.rssSourceDao.liveEnabled(searchKey).apply { + liveRssData = when { + searchKey.isNullOrEmpty() -> App.db.rssSourceDao.liveEnabled() + searchKey.startsWith("group:") -> { + val key = searchKey.substringAfter("group:") + App.db.rssSourceDao.liveEnabledByGroup("%$key%") + } + else -> App.db.rssSourceDao.liveEnabled("%$searchKey%") + }.apply { observe(viewLifecycleOwner, { adapter.setItems(it) }) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 0f7d10e8d..4a5ac68cd 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -199,22 +199,22 @@ class RssSourceActivity : VMBaseActivity { - App.db.rssSourceDao.liveAll() - } - searchKey.startsWith("group:") -> { - val key = searchKey.substringAfter("group:") - App.db.rssSourceDao.liveGroupSearch("%$key%") - } - else -> { - App.db.rssSourceDao.liveSearch("%$searchKey%") - } + sourceLiveData = when { + searchKey.isNullOrBlank() -> { + App.db.rssSourceDao.liveAll() } - sourceLiveData?.observe(this, { - adapter.setItems(it, adapter.diffItemCallback) - }) + searchKey.startsWith("group:") -> { + val key = searchKey.substringAfter("group:") + App.db.rssSourceDao.liveGroupSearch("%$key%") + } + else -> { + App.db.rssSourceDao.liveSearch("%$searchKey%") + } + }.apply { + observe(this@RssSourceActivity, { + adapter.setItems(it, adapter.diffItemCallback) + }) + } } private fun showHelp() {