HapeLee 4 месяцев назад
Родитель
Сommit
2097c0201c

+ 26 - 26
app/src/main/java/io/legado/app/ui/book/search/ScopeSelectSheet.kt

@@ -25,7 +25,6 @@ import io.legado.app.R
 import io.legado.app.data.entities.BookSourcePart
 import io.legado.app.ui.theme.LegadoTheme
 import io.legado.app.ui.widget.components.SearchBar
-import io.legado.app.ui.widget.components.button.ConfirmDismissButtonsRow
 import io.legado.app.ui.widget.components.button.series.MediumPlainButton
 import io.legado.app.ui.widget.components.card.SelectionItemCard
 import io.legado.app.ui.widget.components.icon.AppIcons
@@ -61,6 +60,19 @@ fun ScopeSelectSheet(
     val currentIsSourceScope = if (useDraftSelection) draftIsSourceScope else isSourceScope
     val currentGroups = if (useDraftSelection) draftGroups else selectedGroups
     val currentSourceUrls = if (useDraftSelection) draftSourceUrls else selectedSources
+    val applyDraftSelection = {
+        onApplyScope?.invoke(
+            ScopeSelection(
+                groupNames = if (!draftIsSourceScope) draftGroups.toList() else emptyList(),
+                sources = if (draftIsSourceScope) {
+                    sources.filter { draftSourceUrls.contains(it.bookSourceUrl) }
+                } else {
+                    emptyList()
+                },
+                isSourceScope = draftIsSourceScope,
+            )
+        )
+    }
 
     val filteredGroups = remember(groups, filterText) {
         if (filterText.isBlank()) groups else groups.filter { it.contains(filterText, ignoreCase = true) }
@@ -77,14 +89,25 @@ fun ScopeSelectSheet(
         show = show,
         onDismissRequest = onDismissRequest,
         title = title,
-        endAction = onConfirm?.let {
+        startAction = onConfirm?.let {
             {
                 MediumPlainButton(
                     onClick = it,
                     icon = AppIcons.Settings
                 )
             }
-        }
+        },
+        endAction = onApplyScope?.let {
+            {
+                MediumPlainButton(
+                    onClick = {
+                        applyDraftSelection()
+                        onDismissRequest()
+                    },
+                    text = stringResource(R.string.confirm),
+                )
+            }
+        },
     ) {
         Column {
 
@@ -203,29 +226,6 @@ fun ScopeSelectSheet(
                 }
             }
 
-            if (onApplyScope != null) {
-                Spacer(modifier = Modifier.height(16.dp))
-                ConfirmDismissButtonsRow(
-                    onDismiss = onDismissRequest,
-                    onConfirm = {
-                        onApplyScope(
-                            ScopeSelection(
-                                groupNames = if (!draftIsSourceScope) draftGroups.toList() else emptyList(),
-                                sources = if (draftIsSourceScope) {
-                                    sources.filter { draftSourceUrls.contains(it.bookSourceUrl) }
-                                } else {
-                                    emptyList()
-                                },
-                                isSourceScope = draftIsSourceScope,
-                            )
-                        )
-                        onDismissRequest()
-                    },
-                    dismissText = stringResource(R.string.cancel),
-                    confirmText = stringResource(R.string.confirm),
-                )
-            }
-
             Spacer(modifier = Modifier.height(20.dp))
         }
     }

+ 3 - 1
app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt

@@ -65,7 +65,9 @@ class SearchActivity : BaseComposeActivity() {
         fun start(context: Context, key: String?, searchScope: String? = null) {
             context.startActivity<SearchActivity> {
                 putExtra("key", key)
-                putExtra("searchScope", searchScope)
+                searchScope?.takeIf { it.isNotBlank() }?.let {
+                    putExtra("searchScope", it)
+                }
             }
         }
     }

+ 10 - 4
app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@@ -25,6 +25,7 @@ import kotlinx.collections.immutable.toImmutableSet
 import kotlinx.coroutines.CancellationException
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
 import kotlinx.coroutines.flow.MutableSharedFlow
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.SharingStarted
@@ -36,6 +37,8 @@ import kotlinx.coroutines.flow.flatMapLatest
 import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.flow.update
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
 
 @OptIn(ExperimentalCoroutinesApi::class)
@@ -84,6 +87,7 @@ class SearchViewModel(
     private var persistedSearchScopeRaw = ""
     private var hasTemporaryScope = false
     private val searchScope = SearchScope("")
+    private val preferenceWriteScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
     private val searchControl = BookSearchControl()
     private val searchResultBooks = LinkedHashMap<SearchResultKey, SearchBook>()
 
@@ -319,9 +323,10 @@ class SearchViewModel(
     }
 
     private fun initialize(key: String?, scopeRaw: String?) {
-        if (scopeRaw != null) {
+        val temporaryScope = scopeRaw?.takeIf { it.isNotBlank() }
+        if (temporaryScope != null) {
             hasTemporaryScope = true
-            searchScope.update(scopeRaw, postValue = false)
+            searchScope.update(temporaryScope, postValue = false)
             syncScopeState()
         } else if (hasTemporaryScope) {
             hasTemporaryScope = false
@@ -734,10 +739,11 @@ class SearchViewModel(
     private fun persistSearchScope() {
         hasTemporaryScope = false
         persistedSearchScopeRaw = searchScope.toString()
-        viewModelScope.launch {
+        val scopeRaw = persistedSearchScopeRaw
+        preferenceWriteScope.launch {
             localPreferencesRepository.updatePreference(
                 LocalPreferencesKeys.SEARCH_SCOPE,
-                searchScope.toString()
+                scopeRaw
             )
         }
     }

+ 3 - 1
app/src/main/java/io/legado/app/ui/main/MainIntent.kt

@@ -127,7 +127,9 @@ object MainIntent {
         return createLauncherIntent(context).apply {
             putExtra(EXTRA_START_ROUTE, MainRouteConst.ROUTE_SEARCH)
             putExtra(EXTRA_SEARCH_KEY, key)
-            putExtra(EXTRA_SEARCH_SCOPE, scopeRaw)
+            scopeRaw?.takeIf { it.isNotBlank() }?.let {
+                putExtra(EXTRA_SEARCH_SCOPE, it)
+            }
         }
     }