搜索添加搜索范围可选择单个书源和分组,未完成

This commit is contained in:
kunfei 2022-10-12 22:10:55 +08:00
parent c056b5d3f1
commit 3aa346c764
4 changed files with 46 additions and 22 deletions

View File

@ -342,6 +342,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
val progressBarBehavior: String?
get() = appCtx.getPrefString(PreferKey.progressBarBehavior, "page")
var searchScope: String
get() = appCtx.getPrefString("searchScope") ?: ""
set(value) {
appCtx.putPrefString("searchScope", value)
}
var searchGroup: String
get() = appCtx.getPrefString("searchGroup") ?: ""
set(value) {

View File

@ -21,7 +21,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchKeyword
import io.legado.app.databinding.ActivityBookSearchBinding
import io.legado.app.help.IntentData
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.*
@ -260,11 +259,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
private fun receiptIntent(intent: Intent? = null) {
val searchScopeKey = intent?.getStringExtra("searchScopeKey")
searchScopeKey?.let {
IntentData.get<SearchScope>(searchScopeKey)?.let { searchScope ->
viewModel.searchScope = searchScope
}
val searchScope = intent?.getStringExtra("searchScope")
searchScope?.let {
viewModel.searchScope.scope = searchScope
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames())
}
val key = intent?.getStringExtra("key")
if (key.isNullOrBlank()) {
@ -296,6 +294,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
if (visible) {
upHistory(searchView.query.toString())
binding.llInputHelp.visibility = VISIBLE
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames())
} else {
binding.llInputHelp.visibility = GONE
}
@ -426,6 +425,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) {
viewModel.searchScope = searchScope
searchScopeAdapter.setItems(searchScope.getShowNames())
}
private fun alertSearchScope() {

View File

@ -2,25 +2,34 @@ package io.legado.app.ui.book.search
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.splitNotBlank
/**
* 搜索范围
*/
data class SearchScope(
private val groups: List<String>? = null,
private val sources: List<BookSource>? = null
) {
@Suppress("unused")
data class SearchScope(var scope: String) {
constructor(groups: List<String>) : this(groups.joinToString(","))
constructor(source: BookSource) : this("${source.bookSourceName}::${source.bookSourceUrl}")
override fun toString(): String {
return scope
}
/**
* 搜索范围显示
*/
fun getShowNames(): List<String> {
val list = arrayListOf<String>()
groups?.let {
list.addAll(it)
}
sources?.forEach {
list.add(it.bookSourceName)
if (scope.contains("::")) {
list.add(scope.substringBefore("::"))
} else {
scope.splitNotBlank(",").forEach {
list.add(it)
}
}
if (list.isEmpty()) {
list.add("全部书源")
@ -33,18 +42,26 @@ data class SearchScope(
*/
fun getBookSources(): List<BookSource> {
val list = hashSetOf<BookSource>()
sources?.let {
list.addAll(sources)
}
groups?.forEach { group ->
appDb.bookSourceDao.getEnabledByGroup(group).let {
list.addAll(it)
if (scope.contains("::")) {
scope.substringAfter("::").let {
appDb.bookSourceDao.getBookSource(it)?.let { source ->
list.add(source)
}
}
} else {
scope.splitNotBlank(",").forEach {
list.addAll(appDb.bookSourceDao.getByGroup(it))
}
}
if (list.isEmpty()) {
scope = ""
return appDb.bookSourceDao.allEnabled
}
return list.sortedBy { it.customOrder }
}
fun save() {
AppConfig.searchScope = scope
}
}

View File

@ -7,6 +7,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchKeyword
import io.legado.app.help.config.AppConfig
import io.legado.app.model.webBook.SearchModel
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.channels.awaitClose
@ -34,7 +35,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
})
var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null
var isSearchLiveData = MutableLiveData<Boolean>()
var searchScope: SearchScope = SearchScope()
var searchScope: SearchScope = SearchScope(AppConfig.searchScope)
var searchKey: String = ""
private var searchID = 0L
private var searchFlowCallBack: ((searchBooks: ArrayList<SearchBook>) -> Unit)? = null