kunfei %!s(int64=4) %!d(string=hai) anos
pai
achega
64e0cb71a1

+ 43 - 14
app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt

@@ -61,6 +61,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
         binding.titleBar.findViewById(R.id.search_view)
         binding.titleBar.findViewById(R.id.search_view)
     }
     }
     private var menu: Menu? = null
     private var menu: Menu? = null
+    private var groups: List<String>? = null
     private var historyFlowJob: Job? = null
     private var historyFlowJob: Job? = null
     private var booksFlowJob: Job? = null
     private var booksFlowJob: Job? = null
     private var precisionSearchMenuItem: MenuItem? = null
     private var precisionSearchMenuItem: MenuItem? = null
@@ -82,8 +83,6 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
                     setMessage("${viewModel.searchScope.display}分组搜索结果为空,是否切换到全部分组?")
                     setMessage("${viewModel.searchScope.display}分组搜索结果为空,是否切换到全部分组?")
                     yesButton {
                     yesButton {
                         viewModel.searchScope.update("")
                         viewModel.searchScope.update("")
-                        viewModel.searchScope.save()
-                        viewModel.search(searchView.query.toString())
                     }
                     }
                 }
                 }
                 noButton()
                 noButton()
@@ -114,6 +113,30 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
         return super.onCompatCreateOptionsMenu(menu)
         return super.onCompatCreateOptionsMenu(menu)
     }
     }
 
 
+    override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
+        menu.removeGroup(R.id.menu_group_1)
+        menu.removeGroup(R.id.menu_group_2)
+        val searchScopeNames = viewModel.searchScope.displayNames
+        searchScopeNames.forEach {
+            menu.add(R.id.menu_group_1, Menu.NONE, Menu.NONE, it).apply {
+                isChecked = true
+            }
+        }
+        menu.add(R.id.menu_group_2, R.id.menu_1, Menu.NONE, getString(R.string.all_source)).apply {
+            if (searchScopeNames.isEmpty()) {
+                isChecked = true
+            }
+        }
+        groups?.forEach {
+            if (!searchScopeNames.contains(it)) {
+                menu.add(R.id.menu_group_2, Menu.NONE, Menu.NONE, it)
+            }
+        }
+        menu.setGroupCheckable(R.id.menu_group_1, true, false)
+        menu.setGroupCheckable(R.id.menu_group_2, true, true)
+        return super.onMenuOpened(featureId, menu)
+    }
+
     override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
     override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
         when (item.itemId) {
         when (item.itemId) {
             R.id.menu_precision_search -> {
             R.id.menu_precision_search -> {
@@ -129,6 +152,14 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
             R.id.menu_search_scope -> alertSearchScope()
             R.id.menu_search_scope -> alertSearchScope()
             R.id.menu_source_manage -> startActivity<BookSourceActivity>()
             R.id.menu_source_manage -> startActivity<BookSourceActivity>()
             R.id.menu_log -> showDialogFragment(AppLogDialog())
             R.id.menu_log -> showDialogFragment(AppLogDialog())
+            R.id.menu_1 -> viewModel.searchScope.update("")
+            else -> {
+                if (item.groupId == R.id.menu_group_1) {
+                    viewModel.searchScope.remove(item.title.toString())
+                } else if (item.groupId == R.id.menu_group_2) {
+                    viewModel.searchScope.update(item.title.toString())
+                }
+            }
         }
         }
         return super.onCompatOptionsItemSelected(item)
         return super.onCompatOptionsItemSelected(item)
     }
     }
@@ -219,7 +250,11 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
 
 
     private fun initData() {
     private fun initData() {
         viewModel.searchScope.stateLiveData.observe(this) {
         viewModel.searchScope.stateLiveData.observe(this) {
-            upSearchScope()
+            if (!binding.llInputHelp.isVisible) {
+                searchView.query?.toString()?.trim()?.let {
+                    searchView.setQuery(it, true)
+                }
+            }
         }
         }
         viewModel.isSearchLiveData.observe(this) {
         viewModel.isSearchLiveData.observe(this) {
             if (it) {
             if (it) {
@@ -234,10 +269,11 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
                 delay(1000)
                 delay(1000)
             }
             }
         }
         }
-    }
-
-    private fun upSearchScope() {
-        binding.tvSearchScope.text = viewModel.searchScope.display
+        launch {
+            appDb.bookSourceDao.flowEnabledGroups().collect {
+                groups = it
+            }
+        }
     }
     }
 
 
     /**
     /**
@@ -247,7 +283,6 @@ 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)
-            upSearchScope()
         }
         }
         val key = intent?.getStringExtra("key")
         val key = intent?.getStringExtra("key")
         if (key.isNullOrBlank()) {
         if (key.isNullOrBlank()) {
@@ -384,13 +419,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
 
 
 
 
     override fun onSearchScopeOk(searchScope: SearchScope) {
     override fun onSearchScopeOk(searchScope: SearchScope) {
-        searchScope.save()
         viewModel.searchScope.update(searchScope.toString())
         viewModel.searchScope.update(searchScope.toString())
-        if (!binding.llInputHelp.isVisible) {
-            searchView.query?.toString()?.trim()?.let {
-                searchView.setQuery(it, true)
-            }
-        }
     }
     }
 
 
     private fun alertSearchScope() {
     private fun alertSearchScope() {

+ 21 - 3
app/src/main/java/io/legado/app/ui/book/search/SearchScope.kt

@@ -29,16 +29,19 @@ data class SearchScope(private var scope: String) {
     fun update(scope: String) {
     fun update(scope: String) {
         this.scope = scope
         this.scope = scope
         stateLiveData.postValue(scope)
         stateLiveData.postValue(scope)
+        save()
     }
     }
 
 
     fun update(groups: List<String>) {
     fun update(groups: List<String>) {
         scope = groups.joinToString(",")
         scope = groups.joinToString(",")
         stateLiveData.postValue(scope)
         stateLiveData.postValue(scope)
+        save()
     }
     }
 
 
     fun update(source: BookSource) {
     fun update(source: BookSource) {
         scope = "${source.bookSourceName}::${source.bookSourceUrl}"
         scope = "${source.bookSourceName}::${source.bookSourceUrl}"
         stateLiveData.postValue(scope)
         stateLiveData.postValue(scope)
+        save()
     }
     }
 
 
     fun isSource(): Boolean {
     fun isSource(): Boolean {
@@ -69,12 +72,27 @@ data class SearchScope(private var scope: String) {
                     list.add(it)
                     list.add(it)
                 }
                 }
             }
             }
-            if (list.isEmpty()) {
-                list.add(appCtx.getString(R.string.all_source))
-            }
             return list
             return list
         }
         }
 
 
+    fun remove(scope: String) {
+        if (isSource()) {
+            this.scope = ""
+        } else {
+            val stringBuilder = StringBuilder()
+            this.scope.split(",").forEach {
+                if (it != scope) {
+                    if (stringBuilder.isNotEmpty()) {
+                        stringBuilder.append(",")
+                    }
+                    stringBuilder.append(it)
+                }
+            }
+            this.scope = stringBuilder.toString()
+        }
+        stateLiveData.postValue(this.scope)
+    }
+
     /**
     /**
      * 搜索范围书源
      * 搜索范围书源
      */
      */

+ 0 - 9
app/src/main/res/layout/activity_book_search.xml

@@ -48,15 +48,6 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/title_bar">
         app:layout_constraintTop_toBottomOf="@id/title_bar">
 
 
-        <TextView
-            android:id="@+id/tv_searchScope"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:padding="6dp"
-            android:text="@string/all_source"
-            android:singleLine="true"
-            android:gravity="center" />
-
         <TextView
         <TextView
             android:id="@+id/tv_book_show"
             android:id="@+id/tv_book_show"
             android:layout_width="match_parent"
             android:layout_width="match_parent"

+ 6 - 5
app/src/main/res/menu/book_search.xml

@@ -3,11 +3,6 @@
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
     tools:ignore="AlwaysShowAction">
     tools:ignore="AlwaysShowAction">
 
 
-    <item
-        android:id="@+id/menu_search_scope"
-        android:title="@string/search_scope"
-        app:showAsAction="never" />
-
     <item
     <item
         android:id="@+id/menu_precision_search"
         android:id="@+id/menu_precision_search"
         android:checkable="true"
         android:checkable="true"
@@ -19,9 +14,15 @@
         android:title="@string/book_source_manage"
         android:title="@string/book_source_manage"
         app:showAsAction="never" />
         app:showAsAction="never" />
 
 
+    <item
+        android:id="@+id/menu_search_scope"
+        android:title="@string/change_search_scope"
+        app:showAsAction="never" />
+
     <item
     <item
         android:id="@+id/menu_log"
         android:id="@+id/menu_log"
         android:title="@string/log"
         android:title="@string/log"
+        android:orderInCategory="9999"
         app:showAsAction="never" />
         app:showAsAction="never" />
 
 
 </menu>
 </menu>

+ 1 - 0
app/src/main/res/values-es-rES/strings.xml

@@ -1047,4 +1047,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 1 - 0
app/src/main/res/values-ja-rJP/strings.xml

@@ -1050,4 +1050,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 1 - 0
app/src/main/res/values-pt-rBR/strings.xml

@@ -1050,4 +1050,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 1 - 0
app/src/main/res/values-zh-rHK/strings.xml

@@ -1047,4 +1047,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 1 - 0
app/src/main/res/values-zh-rTW/strings.xml

@@ -1049,4 +1049,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 1 - 0
app/src/main/res/values-zh/strings.xml

@@ -1049,4 +1049,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>

+ 5 - 0
app/src/main/res/values/ids.xml

@@ -5,6 +5,11 @@
     <item name="tag1" type="id" />
     <item name="tag1" type="id" />
     <item name="tag2" type="id" />
     <item name="tag2" type="id" />
 
 
+    <item name="menu_group_1" type="id" />
+    <item name="menu_group_2" type="id" />
+
+    <item name="menu_1" type="id" />
+
     <item name="fast_scroller" type="id" />
     <item name="fast_scroller" type="id" />
 
 
     <item name="bettermovementmethod_highlight_background_span" type="id" />
     <item name="bettermovementmethod_highlight_background_span" type="id" />

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -1050,4 +1050,5 @@
     <string name="webdav_device_name">设备名称</string>
     <string name="webdav_device_name">设备名称</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock">唤醒锁</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
     <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
+    <string name="change_search_scope">切换搜索范围</string>
 </resources>
 </resources>