Merge pull request #2437 from 821938089/little-fix

优化分组搜索范围
pull/2436/head
Horis 2 years ago committed by GitHub
commit e1820be375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  3. 21
      app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt

@ -134,6 +134,9 @@ interface BookSourceDao {
@get:Query("select distinct bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''") @get:Query("select distinct bookSourceGroup from book_sources where trim(bookSourceGroup) <> ''")
val allGroupsUnProcessed: List<String> val allGroupsUnProcessed: List<String>
@get:Query("select distinct bookSourceGroup from book_sources where enabled = 1 and trim(bookSourceGroup) <> ''")
val allEnabledGroupsUnProcessed: List<String>
@Query("select * from book_sources where bookSourceUrl = :key") @Query("select * from book_sources where bookSourceUrl = :key")
fun getBookSource(key: String): BookSource? fun getBookSource(key: String): BookSource?
@ -175,6 +178,11 @@ interface BookSourceDao {
return dealGroups(allGroupsUnProcessed) return dealGroups(allGroupsUnProcessed)
} }
val allEnabledGroups: List<String>
get() {
return dealGroups(allEnabledGroupsUnProcessed)
}
fun flowGroups(): Flow<List<String>> { fun flowGroups(): Flow<List<String>> {
return flowGroupsUnProcessed().map { list -> return flowGroupsUnProcessed().map { list ->
dealGroups(list) dealGroups(list)

@ -221,6 +221,9 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private fun initData() { private fun initData() {
searchScopeAdapter.setItems(viewModel.searchScope.displayNames) searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
viewModel.searchScope.stateLiveData.observe(this) {
searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
}
viewModel.isSearchLiveData.observe(this) { viewModel.isSearchLiveData.observe(this) {
if (it) { if (it) {
startSearch() startSearch()

@ -1,5 +1,6 @@
package io.legado.app.ui.book.search package io.legado.app.ui.book.search
import androidx.lifecycle.MutableLiveData
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
@ -13,12 +14,18 @@ data class SearchScope(private var scope: String) {
constructor(groups: List<String>) : this(groups.joinToString(",")) constructor(groups: List<String>) : this(groups.joinToString(","))
constructor(source: BookSource) : this("${source.bookSourceName}::${source.bookSourceUrl}") constructor(source: BookSource) : this(
"${
source.bookSourceName.replace("::", "")
}::${source.bookSourceUrl}"
)
override fun toString(): String { override fun toString(): String {
return scope return scope
} }
val stateLiveData = MutableLiveData("")
fun update(scope: String) { fun update(scope: String) {
this.scope = scope this.scope = scope
} }
@ -70,12 +77,20 @@ data class SearchScope(private var scope: String) {
} }
} }
} else { } else {
scope.splitNotBlank(",").forEach { val oldScope = scope.splitNotBlank(",")
list.addAll(appDb.bookSourceDao.getEnabledByGroup(it)) val newScope = oldScope.filter {
val bookSources = appDb.bookSourceDao.getEnabledByGroup(it)
list.addAll(bookSources)
bookSources.isNotEmpty()
}
if (oldScope.size != newScope.size) {
update(newScope)
stateLiveData.postValue("")
} }
} }
if (list.isEmpty()) { if (list.isEmpty()) {
scope = "" scope = ""
stateLiveData.postValue("")
return appDb.bookSourceDao.allEnabled return appDb.bookSourceDao.allEnabled
} }
return list.sortedBy { it.customOrder } return list.sortedBy { it.customOrder }

@ -102,7 +102,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
private fun initData() { private fun initData() {
launch { launch {
groups = withContext(IO) { groups = withContext(IO) {
appDb.bookSourceDao.allGroups appDb.bookSourceDao.allEnabledGroups
} }
sources = withContext(IO) { sources = withContext(IO) {
appDb.bookSourceDao.allEnabled appDb.bookSourceDao.allEnabled

Loading…
Cancel
Save