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

pull/2424/head
kunfei 2 years ago
parent 24fb8dbcfa
commit c056b5d3f1
  1. 17
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 50
      app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt
  3. 31
      app/src/main/java/io/legado/app/ui/book/search/SearchScopeAdapter.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt
  5. 1
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt
  6. 7
      app/src/main/res/layout/activity_book_search.xml

@ -21,6 +21,7 @@ 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.IntentData
import io.legado.app.help.config.AppConfig 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.*
@ -51,6 +52,9 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
setHasStableIds(true) setHasStableIds(true)
} }
} }
private val searchScopeAdapter by lazy {
SearchScopeAdapter(this)
}
private val historyKeyAdapter by lazy { private val historyKeyAdapter by lazy {
HistoryKeyAdapter(this, this).apply { HistoryKeyAdapter(this, this).apply {
setHasStableIds(true) setHasStableIds(true)
@ -181,8 +185,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private fun initRecyclerView() { private fun initRecyclerView() {
binding.recyclerView.setEdgeEffectColor(primaryColor) binding.recyclerView.setEdgeEffectColor(primaryColor)
binding.rvSearchScope.setEdgeEffectColor(primaryColor)
binding.rvBookshelfSearch.setEdgeEffectColor(primaryColor) binding.rvBookshelfSearch.setEdgeEffectColor(primaryColor)
binding.rvHistoryKey.setEdgeEffectColor(primaryColor) binding.rvHistoryKey.setEdgeEffectColor(primaryColor)
binding.rvSearchScope.adapter = searchScopeAdapter
binding.rvBookshelfSearch.layoutManager = FlexboxLayoutManager(this) binding.rvBookshelfSearch.layoutManager = FlexboxLayoutManager(this)
binding.rvBookshelfSearch.adapter = bookAdapter binding.rvBookshelfSearch.adapter = bookAdapter
binding.rvHistoryKey.layoutManager = FlexboxLayoutManager(this) binding.rvHistoryKey.layoutManager = FlexboxLayoutManager(this)
@ -230,6 +236,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun initData() { private fun initData() {
searchScopeAdapter.setItems(viewModel.searchScope.getShowNames())
viewModel.isSearchLiveData.observe(this) { viewModel.isSearchLiveData.observe(this) {
if (it) { if (it) {
startSearch() startSearch()
@ -253,6 +260,12 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun receiptIntent(intent: Intent? = null) { private fun receiptIntent(intent: Intent? = null) {
val searchScopeKey = intent?.getStringExtra("searchScopeKey")
searchScopeKey?.let {
IntentData.get<SearchScope>(searchScopeKey)?.let { searchScope ->
viewModel.searchScope = searchScope
}
}
val key = intent?.getStringExtra("key") val key = intent?.getStringExtra("key")
if (key.isNullOrBlank()) { if (key.isNullOrBlank()) {
searchView.findViewById<TextView>(androidx.appcompat.R.id.search_src_text) searchView.findViewById<TextView>(androidx.appcompat.R.id.search_src_text)
@ -411,8 +424,8 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
override fun onSearchScopeOk() { override fun onSearchScopeOk(searchScope: SearchScope) {
viewModel.searchScope = searchScope
} }
private fun alertSearchScope() { private fun alertSearchScope() {

@ -0,0 +1,50 @@
package io.legado.app.ui.book.search
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
/**
* 搜索范围
*/
data class SearchScope(
private val groups: List<String>? = null,
private val sources: List<BookSource>? = null
) {
/**
* 搜索范围显示
*/
fun getShowNames(): List<String> {
val list = arrayListOf<String>()
groups?.let {
list.addAll(it)
}
sources?.forEach {
list.add(it.bookSourceName)
}
if (list.isEmpty()) {
list.add("全部书源")
}
return list
}
/**
* 搜索范围书源
*/
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 (list.isEmpty()) {
return appDb.bookSourceDao.allEnabled
}
return list.sortedBy { it.customOrder }
}
}

@ -0,0 +1,31 @@
package io.legado.app.ui.book.search
import android.content.Context
import android.view.ViewGroup
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.databinding.ItemFilletTextBinding
class SearchScopeAdapter(context: Context) :
RecyclerAdapter<String, ItemFilletTextBinding>(context) {
override fun getViewBinding(parent: ViewGroup): ItemFilletTextBinding {
return ItemFilletTextBinding.inflate(inflater, parent, false)
}
override fun convert(
holder: ItemViewHolder,
binding: ItemFilletTextBinding,
item: String,
payloads: MutableList<Any>
) {
binding.run {
textView.text = item
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemFilletTextBinding) {
}
}

@ -23,7 +23,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope, true)
/** /**
* 搜索范围确认 * 搜索范围确认
*/ */
fun onSearchScopeOk() fun onSearchScopeOk(searchScope: SearchScope)
} }

@ -34,6 +34,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}) })
var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null var searchFinishCallback: ((isEmpty: Boolean) -> Unit)? = null
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchScope: SearchScope = SearchScope()
var searchKey: String = "" var searchKey: String = ""
private var searchID = 0L private var searchID = 0L
private var searchFlowCallBack: ((searchBooks: ArrayList<SearchBook>) -> Unit)? = null private var searchFlowCallBack: ((searchBooks: ArrayList<SearchBook>) -> Unit)? = null

@ -31,7 +31,8 @@
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
tools:listitem="@layout/item_search" />
</io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout> </io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout>
@ -72,7 +73,9 @@
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_search_scope" android:id="@+id/rv_search_scope"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<TextView <TextView
android:id="@+id/tv_book_show" android:id="@+id/tv_book_show"

Loading…
Cancel
Save