pull/2425/head
kunfei 2 years ago
parent 46d7d585e0
commit 6fe2f6529f
  1. 20
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 33
      app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt

@ -21,7 +21,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchKeyword import io.legado.app.data.entities.SearchKeyword
import io.legado.app.databinding.ActivityBookSearchBinding import io.legado.app.databinding.ActivityBookSearchBinding
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.* import io.legado.app.lib.theme.*
import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.about.AppLogDialog
@ -68,13 +67,12 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private var precisionSearchMenuItem: MenuItem? = null private var precisionSearchMenuItem: MenuItem? = null
private var isManualStopSearch = false private var isManualStopSearch = false
private val searchFinishCallback: (isEmpty: Boolean) -> Unit = searchFinish@{ isEmpty -> private val searchFinishCallback: (isEmpty: Boolean) -> Unit = searchFinish@{ isEmpty ->
val searchGroup = AppConfig.searchGroup if (!isEmpty || viewModel.searchScope.isAll()) return@searchFinish
if (!isEmpty || searchGroup.isEmpty()) return@searchFinish
launch { launch {
alert("搜索结果为空") { alert("搜索结果为空") {
val precisionSearch = appCtx.getPrefBoolean(PreferKey.precisionSearch) val precisionSearch = appCtx.getPrefBoolean(PreferKey.precisionSearch)
if (precisionSearch) { if (precisionSearch) {
setMessage("${searchGroup}分组搜索结果为空,是否关闭精准搜索?") setMessage("${viewModel.searchScope.display}分组搜索结果为空,是否关闭精准搜索?")
yesButton { yesButton {
appCtx.putPrefBoolean(PreferKey.precisionSearch, false) appCtx.putPrefBoolean(PreferKey.precisionSearch, false)
precisionSearchMenuItem?.isChecked = false precisionSearchMenuItem?.isChecked = false
@ -82,10 +80,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
viewModel.search(searchView.query.toString()) viewModel.search(searchView.query.toString())
} }
} else { } else {
setMessage("${searchGroup}分组搜索结果为空,是否切换到全部分组?") setMessage("${viewModel.searchScope.display}分组搜索结果为空,是否切换到全部分组?")
yesButton { yesButton {
AppConfig.searchGroup = "" viewModel.searchScope.update("")
viewModel.searchKey = "" viewModel.searchScope.save()
viewModel.search(searchView.query.toString()) viewModel.search(searchView.query.toString())
} }
} }
@ -222,7 +220,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun initData() { private fun initData() {
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames()) searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
viewModel.isSearchLiveData.observe(this) { viewModel.isSearchLiveData.observe(this) {
if (it) { if (it) {
startSearch() startSearch()
@ -245,7 +243,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
val searchScope = intent?.getStringExtra("searchScope") val searchScope = intent?.getStringExtra("searchScope")
searchScope?.let { searchScope?.let {
viewModel.searchScope.update(searchScope) viewModel.searchScope.update(searchScope)
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames()) searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
} }
val key = intent?.getStringExtra("key") val key = intent?.getStringExtra("key")
if (key.isNullOrBlank()) { if (key.isNullOrBlank()) {
@ -277,7 +275,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
if (visible) { if (visible) {
upHistory(searchView.query.toString()) upHistory(searchView.query.toString())
binding.llInputHelp.visibility = VISIBLE binding.llInputHelp.visibility = VISIBLE
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames()) searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
} else { } else {
binding.llInputHelp.visibility = GONE binding.llInputHelp.visibility = GONE
} }
@ -385,7 +383,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) { override fun onSearchScopeOk(searchScope: SearchScope) {
searchScope.save() searchScope.save()
viewModel.searchScope.update(searchScope.toString()) viewModel.searchScope.update(searchScope.toString())
searchScopeAdapter.setItems(searchScope.getShowNames()) searchScopeAdapter.setItems(searchScope.displayNames)
} }
private fun alertSearchScope() { private fun alertSearchScope() {

@ -31,20 +31,29 @@ data class SearchScope(private var scope: String) {
scope = "${source.bookSourceName}::${source.bookSourceUrl}" scope = "${source.bookSourceName}::${source.bookSourceUrl}"
} }
val display: String
get() {
if (scope.contains("::")) {
return scope.substringBefore("::")
}
return scope
}
/** /**
* 搜索范围显示 * 搜索范围显示
*/ */
fun getShowNames(): List<String> { val displayNames: List<String>
val list = arrayListOf<String>() get() {
if (scope.contains("::")) { val list = arrayListOf<String>()
list.add(scope.substringBefore("::")) if (scope.contains("::")) {
} else { list.add(scope.substringBefore("::"))
scope.splitNotBlank(",").forEach { } else {
list.add(it) scope.splitNotBlank(",").forEach {
list.add(it)
}
} }
} if (list.isEmpty()) {
if (list.isEmpty()) { list.add("全部书源")
list.add("全部书源")
} }
return list return list
} }
@ -72,6 +81,10 @@ data class SearchScope(private var scope: String) {
return list.sortedBy { it.customOrder } return list.sortedBy { it.customOrder }
} }
fun isAll(): Boolean {
return scope.isEmpty()
}
fun save() { fun save() {
AppConfig.searchScope = scope AppConfig.searchScope = scope
} }

Loading…
Cancel
Save