From 8aa68e6199efd7d94e47f908e3f97354cf843703 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 3 Jan 2021 20:29:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E9=80=89=E6=8B=A9=E5=88=86?= =?UTF-8?q?=E7=BB=84=E6=97=B6=E5=8F=AA=E6=90=9C=E7=B4=A2=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 5 ++++- .../app/ui/main/explore/ExploreFragment.kt | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) 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 560a92a76..3655e0117 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 @@ -29,6 +29,9 @@ interface BookSourceDao { @Query("select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and (bookSourceGroup like :key or bookSourceName like :key) order by customOrder asc") fun liveExplore(key: String): LiveData> + @Query("select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and (bookSourceGroup like :key) order by customOrder asc") + fun liveGroupExplore(key: String): LiveData> + @Query("select bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''") fun liveGroup(): LiveData> @@ -36,7 +39,7 @@ interface BookSourceDao { fun liveGroupEnabled(): LiveData> @Query("select bookSourceGroup from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and trim(bookSourceGroup) <> ''") - fun liveGroupExplore(): LiveData> + fun liveExploreGroup(): LiveData> @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index e4ea63bd7..ff311f503 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -97,7 +97,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo private fun initGroupData() { liveGroup?.removeObservers(viewLifecycleOwner) - liveGroup = App.db.bookSourceDao.liveGroupExplore() + liveGroup = App.db.bookSourceDao.liveExploreGroup() liveGroup?.observe(viewLifecycleOwner, { groups.clear() it.map { group -> @@ -107,12 +107,19 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo }) } - private fun initExploreData(key: String? = null) { + private fun initExploreData(searchKey: String? = null) { liveExplore?.removeObservers(viewLifecycleOwner) - liveExplore = if (key.isNullOrBlank()) { - App.db.bookSourceDao.liveExplore() - } else { - App.db.bookSourceDao.liveExplore("%$key%") + liveExplore = when { + searchKey.isNullOrBlank() -> { + App.db.bookSourceDao.liveExplore() + } + searchKey.startsWith("group:") -> { + val key = searchKey.substringAfter("group:") + App.db.bookSourceDao.liveGroupExplore("%$key%") + } + else -> { + App.db.bookSourceDao.liveExplore("%$searchKey%") + } } liveExplore?.observe(viewLifecycleOwner, { binding.tvEmptyMsg.isGone = it.isNotEmpty() || searchView.query.isNotEmpty() @@ -135,7 +142,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo override fun onCompatOptionsItemSelected(item: MenuItem) { super.onCompatOptionsItemSelected(item) if (item.groupId == R.id.menu_group_text) { - searchView.setQuery(item.title, true) + searchView.setQuery("group:${item.title}", true) } }