pull/2457/head
kunfei 2 years ago
parent 42b8fcc2b4
commit b7ffeb930b
  1. 21
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt
  3. 29
      app/src/main/res/layout/activity_book_search.xml

@ -52,9 +52,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
setHasStableIds(true)
}
}
private val searchScopeAdapter by lazy {
SearchScopeAdapter(this)
}
private val historyKeyAdapter by lazy {
HistoryKeyAdapter(this, this).apply {
setHasStableIds(true)
@ -63,6 +60,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private val searchView: SearchView by lazy {
binding.titleBar.findViewById(R.id.search_view)
}
private var menu: Menu? = null
private var historyFlowJob: Job? = null
private var booksFlowJob: Job? = null
private var precisionSearchMenuItem: MenuItem? = null
@ -110,6 +108,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.book_search, menu)
this.menu = menu
upSearchScopeMenu()
precisionSearchMenuItem = menu.findItem(R.id.menu_precision_search)
precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch)
return super.onCompatCreateOptionsMenu(menu)
@ -171,10 +171,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private fun initRecyclerView() {
binding.recyclerView.setEdgeEffectColor(primaryColor)
binding.rvSearchScope.setEdgeEffectColor(primaryColor)
binding.rvBookshelfSearch.setEdgeEffectColor(primaryColor)
binding.rvHistoryKey.setEdgeEffectColor(primaryColor)
binding.rvSearchScope.adapter = searchScopeAdapter
binding.rvBookshelfSearch.layoutManager = FlexboxLayoutManager(this)
binding.rvBookshelfSearch.adapter = bookAdapter
binding.rvHistoryKey.layoutManager = FlexboxLayoutManager(this)
@ -217,14 +215,12 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
viewModel.stop()
binding.refreshProgressBar.isAutoLoading = false
}
binding.tvToggleSearchScope.setOnClickListener { alertSearchScope() }
binding.tvClearHistory.setOnClickListener { alertClearHistory() }
}
private fun initData() {
searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
viewModel.searchScope.stateLiveData.observe(this) {
searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
upSearchScopeMenu()
}
viewModel.isSearchLiveData.observe(this) {
if (it) {
@ -241,6 +237,11 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
}
private fun upSearchScopeMenu() {
menu?.findItem(R.id.menu_search_scope)?.title =
"${getString(R.string.search_scope)}-${viewModel.searchScope.display}"
}
/**
* 处理传入数据
*/
@ -248,7 +249,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
val searchScope = intent?.getStringExtra("searchScope")
searchScope?.let {
viewModel.searchScope.update(searchScope)
searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
upSearchScopeMenu()
}
val key = intent?.getStringExtra("key")
if (key.isNullOrBlank()) {
@ -280,7 +281,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
if (visible) {
upHistory(searchView.query.toString())
binding.llInputHelp.visibility = VISIBLE
searchScopeAdapter.setItems(viewModel.searchScope.displayNames)
} else {
binding.llInputHelp.visibility = GONE
}
@ -388,7 +388,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) {
searchScope.save()
viewModel.searchScope.update(searchScope.toString())
searchScopeAdapter.setItems(searchScope.displayNames)
if (!binding.llInputHelp.isVisible) {
searchView.query?.toString()?.trim()?.let {
searchView.setQuery(it, true)

@ -1,10 +1,12 @@
package io.legado.app.ui.book.search
import androidx.lifecycle.MutableLiveData
import io.legado.app.R
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
import splitties.init.appCtx
/**
* 搜索范围
@ -26,6 +28,7 @@ data class SearchScope(private var scope: String) {
fun update(scope: String) {
this.scope = scope
stateLiveData.postValue(scope)
}
fun update(groups: List<String>) {
@ -41,6 +44,9 @@ data class SearchScope(private var scope: String) {
if (scope.contains("::")) {
return scope.substringBefore("::")
}
if (scope.isEmpty()) {
return appCtx.getString(R.string.all_source)
}
return scope
}
@ -58,7 +64,7 @@ data class SearchScope(private var scope: String) {
}
}
if (list.isEmpty()) {
list.add("全部书源")
list.add(appCtx.getString(R.string.all_source))
}
return list
}
@ -83,12 +89,12 @@ data class SearchScope(private var scope: String) {
}
if (oldScope.size != newScope.size) {
update(newScope)
stateLiveData.postValue("")
stateLiveData.postValue(scope)
}
}
if (list.isEmpty()) {
scope = ""
stateLiveData.postValue("")
stateLiveData.postValue(scope)
return appDb.bookSourceDao.allEnabled
}
return list.sortedBy { it.customOrder }

@ -48,35 +48,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_bar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="6dp"
android:text="@string/search_scope" />
<TextView
android:id="@+id/tv_toggle_search_scope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="6dp"
android:text="@string/toggle_search_scope" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_search_scope"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<TextView
android:id="@+id/tv_book_show"
android:layout_width="match_parent"

Loading…
Cancel
Save