发现选择分组时只搜索分组

pull/783/head
gedoor 4 years ago
parent a9155e6af9
commit 8aa68e6199
  1. 5
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 21
      app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.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<List<BookSource>>
@Query("select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and (bookSourceGroup like :key) order by customOrder asc")
fun liveGroupExplore(key: String): LiveData<List<BookSource>>
@Query("select bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''")
fun liveGroup(): LiveData<List<String>>
@ -36,7 +39,7 @@ interface BookSourceDao {
fun liveGroupEnabled(): LiveData<List<String>>
@Query("select bookSourceGroup from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and trim(bookSourceGroup) <> ''")
fun liveGroupExplore(): LiveData<List<String>>
fun liveExploreGroup(): LiveData<List<String>>
@Query("select distinct enabled from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey")
fun searchIsEnable(searchKey: String = ""): List<Boolean>

@ -97,7 +97,7 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(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<ExploreViewModel>(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<ExploreViewModel>(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)
}
}

Loading…
Cancel
Save