From 2097c0201c521f16e8b1a06a39a336426b1119a3 Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Sun, 14 Jun 2026 16:43:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=9D=E5=AD=98=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/search/ScopeSelectSheet.kt | 52 +++++++++---------- .../app/ui/book/search/SearchActivity.kt | 4 +- .../app/ui/book/search/SearchViewModel.kt | 14 +++-- .../java/io/legado/app/ui/main/MainIntent.kt | 4 +- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/ScopeSelectSheet.kt b/app/src/main/java/io/legado/app/ui/book/search/ScopeSelectSheet.kt index 37fd8f475..238764e43 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/ScopeSelectSheet.kt +++ b/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)) } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 468129d12..670846c8b 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/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 { putExtra("key", key) - putExtra("searchScope", searchScope) + searchScope?.takeIf { it.isNotBlank() }?.let { + putExtra("searchScope", it) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index dbb5dd587..6f15947ad 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/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() @@ -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 ) } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainIntent.kt b/app/src/main/java/io/legado/app/ui/main/MainIntent.kt index 7d9e8d156..00c1684a4 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainIntent.kt +++ b/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) + } } }