From bd814dea8fc16ce0b31747db59b2ffdde22e0c42 Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Mon, 11 May 2026 02:51:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=91=E7=8E=B0=E6=9F=90?= =?UTF-8?q?=E4=BA=9B=E7=82=B9=E5=87=BB=E9=A1=B9=E9=94=99=E8=AF=AF=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/main/explore/ExploreScreen.kt | 29 ++++++++-------- .../app/ui/main/explore/ExploreViewModel.kt | 33 ++++++------------- .../components/explore/ExploreKindItem.kt | 2 +- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreScreen.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreScreen.kt index c3da15a31..fb9d10d44 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreScreen.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreScreen.kt @@ -53,6 +53,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import io.legado.app.R import io.legado.app.data.entities.BookSourcePart +import io.legado.app.help.source.getExploreInfoMap import io.legado.app.ui.widget.components.explore.ExploreKindUiUseCase import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.book.search.SearchScope @@ -77,8 +78,10 @@ import io.legado.app.ui.widget.components.menuItem.RoundDropdownMenu import io.legado.app.ui.widget.components.menuItem.RoundDropdownMenuItem import io.legado.app.ui.widget.components.text.AppText import io.legado.app.utils.startActivity +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.koin.androidx.compose.koinViewModel import org.koin.compose.koinInject import top.yukonga.miuix.kmp.theme.MiuixTheme @@ -104,13 +107,17 @@ fun ExploreScreen( viewModel.effects.collect { effect -> when (effect) { is ExploreEffect.ExecuteKindAction -> { - exploreKindUseCase.executeAction( - action = effect.kind.action, - title = effect.kind.title, - sourceUrl = effect.sourceUrl, - activity = activity, - onRefreshKinds = { viewModel.refreshExploreKinds(effect.sourceUrl) } - ) + withContext(Dispatchers.IO) { + val infoMap = getExploreInfoMap(effect.sourceUrl) + exploreKindUseCase.executeAction( + action = effect.kind.action, + title = effect.kind.title, + sourceUrl = effect.sourceUrl, + infoMap = infoMap, + activity = activity, + onRefreshKinds = { viewModel.refreshExploreKinds(effect.sourceUrl) } + ) + } } } } @@ -201,13 +208,7 @@ fun ExploreScreen( ) { items( items = uiState.listItems, - key = { it.key }, - contentType = { - when (it) { - is ExploreListItem.Header -> "source-header" - is ExploreListItem.KindRow -> "kind-row" - } - } + key = { it.key } ) { listItem -> when (listItem) { is ExploreListItem.Header -> { diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreViewModel.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreViewModel.kt index 8ae0b21fb..5cb91c8c4 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreViewModel.kt @@ -21,8 +21,6 @@ import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableMap import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -30,10 +28,6 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.debounce -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn @@ -53,8 +47,7 @@ class ExploreViewModel( private val _effects = MutableSharedFlow(extraBufferCapacity = 8) val effects = _effects.asSharedFlow() - private val searchKeyFlow = MutableStateFlow("") - private val groupFlow = MutableStateFlow("") + private var exploreJob: Job? = null private var kindsJob: Job? = null init { @@ -73,13 +66,13 @@ class ExploreViewModel( } fun search(key: String) { - searchKeyFlow.value = key _uiState.update { it.copy(searchKey = key, expandedId = null) } + observeExplore() } fun setGroup(group: String) { - groupFlow.value = group _uiState.update { it.copy(selectedGroup = group, expandedId = null) } + observeExplore() } fun toggleSearchVisible(visible: Boolean) { @@ -89,20 +82,14 @@ class ExploreViewModel( } } - @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) private fun observeExplore() { - viewModelScope.launch { - combine( - searchKeyFlow - .debounce(250) - .distinctUntilChanged(), - groupFlow - ) { query, selectedGroup -> - query to selectedGroup - } - .flatMapLatest { (query, selectedGroup) -> - exploreRepository.getExploreSources(query, selectedGroup) - } + exploreJob?.cancel() + exploreJob = viewModelScope.launch { + val state = _uiState.value + val query = state.searchKey + val selectedGroup = state.selectedGroup + + exploreRepository.getExploreSources(query, selectedGroup) .flowOn(IO) .collectLatest { items -> _uiState.update { it.copy(items = items.toImmutableList()) } diff --git a/app/src/main/java/io/legado/app/ui/widget/components/explore/ExploreKindItem.kt b/app/src/main/java/io/legado/app/ui/widget/components/explore/ExploreKindItem.kt index c97e55a01..278a643d3 100644 --- a/app/src/main/java/io/legado/app/ui/widget/components/explore/ExploreKindItem.kt +++ b/app/src/main/java/io/legado/app/ui/widget/components/explore/ExploreKindItem.kt @@ -73,7 +73,7 @@ fun ExploreKindItem( modifier = modifier, border = if (enableBorder) { BorderStroke(borderWidth, borderColor) - } else BorderStroke(1.dp, LegadoTheme.colorScheme.outline) + } else BorderStroke(1.dp, LegadoTheme.colorScheme.outlineVariant) ) { KindText( text = displayText,