修复发现某些点击项错误响应的问题

This commit is contained in:
HapeLee
2026-05-11 03:28:12 +08:00
parent 5154a59e0d
commit bd814dea8f
3 changed files with 26 additions and 38 deletions
@@ -53,6 +53,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.legado.app.R import io.legado.app.R
import io.legado.app.data.entities.BookSourcePart 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.widget.components.explore.ExploreKindUiUseCase
import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.book.search.SearchScope 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.menuItem.RoundDropdownMenuItem
import io.legado.app.ui.widget.components.text.AppText import io.legado.app.ui.widget.components.text.AppText
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.androidx.compose.koinViewModel import org.koin.androidx.compose.koinViewModel
import org.koin.compose.koinInject import org.koin.compose.koinInject
import top.yukonga.miuix.kmp.theme.MiuixTheme import top.yukonga.miuix.kmp.theme.MiuixTheme
@@ -104,13 +107,17 @@ fun ExploreScreen(
viewModel.effects.collect { effect -> viewModel.effects.collect { effect ->
when (effect) { when (effect) {
is ExploreEffect.ExecuteKindAction -> { is ExploreEffect.ExecuteKindAction -> {
exploreKindUseCase.executeAction( withContext(Dispatchers.IO) {
action = effect.kind.action, val infoMap = getExploreInfoMap(effect.sourceUrl)
title = effect.kind.title, exploreKindUseCase.executeAction(
sourceUrl = effect.sourceUrl, action = effect.kind.action,
activity = activity, title = effect.kind.title,
onRefreshKinds = { viewModel.refreshExploreKinds(effect.sourceUrl) } sourceUrl = effect.sourceUrl,
) infoMap = infoMap,
activity = activity,
onRefreshKinds = { viewModel.refreshExploreKinds(effect.sourceUrl) }
)
}
} }
} }
} }
@@ -201,13 +208,7 @@ fun ExploreScreen(
) { ) {
items( items(
items = uiState.listItems, items = uiState.listItems,
key = { it.key }, key = { it.key }
contentType = {
when (it) {
is ExploreListItem.Header -> "source-header"
is ExploreListItem.KindRow -> "kind-row"
}
}
) { listItem -> ) { listItem ->
when (listItem) { when (listItem) {
is ExploreListItem.Header -> { is ExploreListItem.Header -> {
@@ -21,8 +21,6 @@ import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -30,10 +28,6 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest 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.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
@@ -53,8 +47,7 @@ class ExploreViewModel(
private val _effects = MutableSharedFlow<ExploreEffect>(extraBufferCapacity = 8) private val _effects = MutableSharedFlow<ExploreEffect>(extraBufferCapacity = 8)
val effects = _effects.asSharedFlow() val effects = _effects.asSharedFlow()
private val searchKeyFlow = MutableStateFlow("") private var exploreJob: Job? = null
private val groupFlow = MutableStateFlow("")
private var kindsJob: Job? = null private var kindsJob: Job? = null
init { init {
@@ -73,13 +66,13 @@ class ExploreViewModel(
} }
fun search(key: String) { fun search(key: String) {
searchKeyFlow.value = key
_uiState.update { it.copy(searchKey = key, expandedId = null) } _uiState.update { it.copy(searchKey = key, expandedId = null) }
observeExplore()
} }
fun setGroup(group: String) { fun setGroup(group: String) {
groupFlow.value = group
_uiState.update { it.copy(selectedGroup = group, expandedId = null) } _uiState.update { it.copy(selectedGroup = group, expandedId = null) }
observeExplore()
} }
fun toggleSearchVisible(visible: Boolean) { fun toggleSearchVisible(visible: Boolean) {
@@ -89,20 +82,14 @@ class ExploreViewModel(
} }
} }
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
private fun observeExplore() { private fun observeExplore() {
viewModelScope.launch { exploreJob?.cancel()
combine( exploreJob = viewModelScope.launch {
searchKeyFlow val state = _uiState.value
.debounce(250) val query = state.searchKey
.distinctUntilChanged(), val selectedGroup = state.selectedGroup
groupFlow
) { query, selectedGroup -> exploreRepository.getExploreSources(query, selectedGroup)
query to selectedGroup
}
.flatMapLatest { (query, selectedGroup) ->
exploreRepository.getExploreSources(query, selectedGroup)
}
.flowOn(IO) .flowOn(IO)
.collectLatest { items -> .collectLatest { items ->
_uiState.update { it.copy(items = items.toImmutableList()) } _uiState.update { it.copy(items = items.toImmutableList()) }
@@ -73,7 +73,7 @@ fun ExploreKindItem(
modifier = modifier, modifier = modifier,
border = if (enableBorder) { border = if (enableBorder) {
BorderStroke(borderWidth, borderColor) BorderStroke(borderWidth, borderColor)
} else BorderStroke(1.dp, LegadoTheme.colorScheme.outline) } else BorderStroke(1.dp, LegadoTheme.colorScheme.outlineVariant)
) { ) {
KindText( KindText(
text = displayText, text = displayText,