fix: 保存搜索范围选择

This commit is contained in:
HapeLee
2026-06-14 16:43:08 +08:00
parent 4ad6b1396b
commit 2097c0201c
4 changed files with 42 additions and 32 deletions
@@ -25,7 +25,6 @@ import io.legado.app.R
import io.legado.app.data.entities.BookSourcePart import io.legado.app.data.entities.BookSourcePart
import io.legado.app.ui.theme.LegadoTheme import io.legado.app.ui.theme.LegadoTheme
import io.legado.app.ui.widget.components.SearchBar 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.button.series.MediumPlainButton
import io.legado.app.ui.widget.components.card.SelectionItemCard import io.legado.app.ui.widget.components.card.SelectionItemCard
import io.legado.app.ui.widget.components.icon.AppIcons import io.legado.app.ui.widget.components.icon.AppIcons
@@ -61,6 +60,19 @@ fun ScopeSelectSheet(
val currentIsSourceScope = if (useDraftSelection) draftIsSourceScope else isSourceScope val currentIsSourceScope = if (useDraftSelection) draftIsSourceScope else isSourceScope
val currentGroups = if (useDraftSelection) draftGroups else selectedGroups val currentGroups = if (useDraftSelection) draftGroups else selectedGroups
val currentSourceUrls = if (useDraftSelection) draftSourceUrls else selectedSources 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) { val filteredGroups = remember(groups, filterText) {
if (filterText.isBlank()) groups else groups.filter { it.contains(filterText, ignoreCase = true) } if (filterText.isBlank()) groups else groups.filter { it.contains(filterText, ignoreCase = true) }
@@ -77,14 +89,25 @@ fun ScopeSelectSheet(
show = show, show = show,
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
title = title, title = title,
endAction = onConfirm?.let { startAction = onConfirm?.let {
{ {
MediumPlainButton( MediumPlainButton(
onClick = it, onClick = it,
icon = AppIcons.Settings icon = AppIcons.Settings
) )
} }
} },
endAction = onApplyScope?.let {
{
MediumPlainButton(
onClick = {
applyDraftSelection()
onDismissRequest()
},
text = stringResource(R.string.confirm),
)
}
},
) { ) {
Column { 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)) Spacer(modifier = Modifier.height(20.dp))
} }
} }
@@ -65,7 +65,9 @@ class SearchActivity : BaseComposeActivity() {
fun start(context: Context, key: String?, searchScope: String? = null) { fun start(context: Context, key: String?, searchScope: String? = null) {
context.startActivity<SearchActivity> { context.startActivity<SearchActivity> {
putExtra("key", key) putExtra("key", key)
putExtra("searchScope", searchScope) searchScope?.takeIf { it.isNotBlank() }?.let {
putExtra("searchScope", it)
}
} }
} }
} }
@@ -25,6 +25,7 @@ import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
@@ -36,6 +37,8 @@ import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@@ -84,6 +87,7 @@ class SearchViewModel(
private var persistedSearchScopeRaw = "" private var persistedSearchScopeRaw = ""
private var hasTemporaryScope = false private var hasTemporaryScope = false
private val searchScope = SearchScope("") private val searchScope = SearchScope("")
private val preferenceWriteScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val searchControl = BookSearchControl() private val searchControl = BookSearchControl()
private val searchResultBooks = LinkedHashMap<SearchResultKey, SearchBook>() private val searchResultBooks = LinkedHashMap<SearchResultKey, SearchBook>()
@@ -319,9 +323,10 @@ class SearchViewModel(
} }
private fun initialize(key: String?, scopeRaw: String?) { private fun initialize(key: String?, scopeRaw: String?) {
if (scopeRaw != null) { val temporaryScope = scopeRaw?.takeIf { it.isNotBlank() }
if (temporaryScope != null) {
hasTemporaryScope = true hasTemporaryScope = true
searchScope.update(scopeRaw, postValue = false) searchScope.update(temporaryScope, postValue = false)
syncScopeState() syncScopeState()
} else if (hasTemporaryScope) { } else if (hasTemporaryScope) {
hasTemporaryScope = false hasTemporaryScope = false
@@ -734,10 +739,11 @@ class SearchViewModel(
private fun persistSearchScope() { private fun persistSearchScope() {
hasTemporaryScope = false hasTemporaryScope = false
persistedSearchScopeRaw = searchScope.toString() persistedSearchScopeRaw = searchScope.toString()
viewModelScope.launch { val scopeRaw = persistedSearchScopeRaw
preferenceWriteScope.launch {
localPreferencesRepository.updatePreference( localPreferencesRepository.updatePreference(
LocalPreferencesKeys.SEARCH_SCOPE, LocalPreferencesKeys.SEARCH_SCOPE,
searchScope.toString() scopeRaw
) )
} }
} }
@@ -127,7 +127,9 @@ object MainIntent {
return createLauncherIntent(context).apply { return createLauncherIntent(context).apply {
putExtra(EXTRA_START_ROUTE, MainRouteConst.ROUTE_SEARCH) putExtra(EXTRA_START_ROUTE, MainRouteConst.ROUTE_SEARCH)
putExtra(EXTRA_SEARCH_KEY, key) putExtra(EXTRA_SEARCH_KEY, key)
putExtra(EXTRA_SEARCH_SCOPE, scopeRaw) scopeRaw?.takeIf { it.isNotBlank() }?.let {
putExtra(EXTRA_SEARCH_SCOPE, it)
}
} }
} }