kunfei 4 лет назад
Родитель
Сommit
293769b69c
1 измененных файлов с 37 добавлено и 6 удалено
  1. 37 6
      app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt

+ 37 - 6
app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
 import android.os.Bundle
 import android.view.View
 import android.view.ViewGroup
+import androidx.appcompat.widget.SearchView
 import androidx.recyclerview.widget.RecyclerView
 import io.legado.app.R
 import io.legado.app.base.BaseDialogFragment
@@ -25,9 +26,10 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
 
     private val binding by viewBinding(DialogSearchScopeBinding::bind)
     val callback: Callback get() = parentFragment as? Callback ?: activity as Callback
-    var groups: List<String> = arrayListOf()
-    var sources: List<BookSource> = arrayListOf()
-
+    var groups: List<String> = emptyList()
+    var sources: List<BookSource> = emptyList()
+    val screenSources = arrayListOf<BookSource>()
+    var screenText: String? = null
 
     val adapter by lazy {
         RecyclerAdapter()
@@ -42,6 +44,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
         binding.toolBar.setBackgroundColor(primaryColor)
         binding.recyclerView.adapter = adapter
         initMenu()
+        initSearchView()
         initOtherView()
         initData()
         upData()
@@ -52,6 +55,22 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
         binding.toolBar.menu.applyTint(requireContext())
     }
 
+    private fun initSearchView() {
+        val searchView = binding.toolBar.menu.findItem(R.id.menu_screen).actionView as SearchView
+        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
+            override fun onQueryTextSubmit(query: String?): Boolean {
+                return false
+            }
+
+            override fun onQueryTextChange(newText: String?): Boolean {
+                screenText = newText
+                upData()
+                return false
+            }
+
+        })
+    }
+
     private fun initOtherView() {
         binding.rgScope.setOnCheckedChangeListener { group, checkedId ->
             binding.toolBar.menu.findItem(R.id.menu_screen)?.isVisible = checkedId == R.id.rb_source
@@ -94,6 +113,18 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
     @SuppressLint("NotifyDataSetChanged")
     private fun upData() {
         launch {
+            withContext(IO) {
+                if (binding.rbSource.isChecked) {
+                    sources.filter { source ->
+                        screenText?.let { screenText ->
+                            source.bookSourceName.contains(screenText)
+                        } ?: true
+                    }.let {
+                        screenSources.clear()
+                        screenSources.addAll(it)
+                    }
+                }
+            }
             adapter.notifyDataSetChanged()
         }
     }
@@ -135,7 +166,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
                         }
                     }
                     is ItemRadioButtonBinding -> {
-                        sources.getOrNull(position)?.let {
+                        screenSources.getOrNull(position)?.let {
                             holder.binding.radioButton.isChecked = selectSource == it
                             holder.binding.radioButton.text = it.bookSourceName
                         }
@@ -163,7 +194,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
                     }
                 }
                 is ItemRadioButtonBinding -> {
-                    sources.getOrNull(position)?.let {
+                    screenSources.getOrNull(position)?.let {
                         holder.binding.radioButton.isChecked = selectSource == it
                         holder.binding.radioButton.text = it.bookSourceName
                         holder.binding.radioButton.setOnCheckedChangeListener { buttonView, isChecked ->
@@ -181,7 +212,7 @@ class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) {
 
         override fun getItemCount(): Int {
             return if (binding.rbSource.isChecked) {
-                sources.size
+                screenSources.size
             } else {
                 groups.size
             }