fix: 单章换源问题

This commit is contained in:
HapeLee
2026-06-14 00:21:15 +08:00
parent 3fdea120da
commit 7e66f7589d
4 changed files with 40 additions and 8 deletions
@@ -42,7 +42,17 @@ class GetChapterContentUseCase(
/** /**
* Find the chapter index in a new TOC matching the current chapter. * Find the chapter index in a new TOC matching the current chapter.
*/ */
fun getDurChapterIndex(chapterIndex: Int, chapterTitle: String, toc: List<BookChapter>): Int { fun getDurChapterIndex(
return io.legado.app.help.book.BookHelp.getDurChapter(chapterIndex, chapterTitle, toc) chapterIndex: Int,
chapterTitle: String,
toc: List<BookChapter>,
totalChapterNum: Int = 0,
): Int {
return io.legado.app.help.book.BookHelp.getDurChapter(
chapterIndex,
chapterTitle,
toc,
totalChapterNum,
)
} }
} }
@@ -24,6 +24,7 @@ data class ChangeChapterSourceUiState(
val selectedSourceName: String = "", val selectedSourceName: String = "",
val tocItems: ImmutableList<BookChapter> = persistentListOf(), val tocItems: ImmutableList<BookChapter> = persistentListOf(),
val isLoadingToc: Boolean = false, val isLoadingToc: Boolean = false,
val currentTocIndex: Int = -1,
// Scope filter // Scope filter
val scopeState: ScopeUiState = ScopeUiState( val scopeState: ScopeUiState = ScopeUiState(
isAll = true, isAll = true,
@@ -78,6 +78,7 @@ class ChangeChapterSourceViewModel(
showToc = false, showToc = false,
tocItems = persistentListOf(), tocItems = persistentListOf(),
isLoadingToc = false, isLoadingToc = false,
currentTocIndex = -1,
selectedSourceName = "", selectedSourceName = "",
) )
} }
@@ -113,7 +114,8 @@ class ChangeChapterSourceViewModel(
it.copy( it.copy(
showToc = false, showToc = false,
tocItems = persistentListOf(), tocItems = persistentListOf(),
isLoadingToc = false isLoadingToc = false,
currentTocIndex = -1,
) )
} }
} }
@@ -313,23 +315,32 @@ class ChangeChapterSourceViewModel(
it.copy( it.copy(
showToc = true, showToc = true,
selectedSourceName = searchBook.originName, selectedSourceName = searchBook.originName,
isLoadingToc = true isLoadingToc = true,
currentTocIndex = -1,
) )
} }
viewModelScope.launch { viewModelScope.launch {
try { try {
val (toc, _) = getChapterContentUseCase.getToc(book) val (toc, _) = getChapterContentUseCase.getToc(book)
val currentTocIndex = getChapterContentUseCase.getDurChapterIndex(
chapterIndex = chapterIndex,
chapterTitle = chapterTitle,
toc = toc,
totalChapterNum = oldBook?.totalChapterNum ?: 0,
).takeIf { it in toc.indices } ?: -1
_uiState.update { _uiState.update {
it.copy( it.copy(
tocItems = toc.toImmutableList(), tocItems = toc.toImmutableList(),
isLoadingToc = false isLoadingToc = false,
currentTocIndex = currentTocIndex,
) )
} }
} catch (e: Exception) { } catch (e: Exception) {
_uiState.update { _uiState.update {
it.copy( it.copy(
showToc = false, showToc = false,
isLoadingToc = false isLoadingToc = false,
currentTocIndex = -1,
) )
} }
_effects.tryEmit(ChangeChapterSourceEffect.ShowToast("获取目录失败")) _effects.tryEmit(ChangeChapterSourceEffect.ShowToast("获取目录失败"))
@@ -10,6 +10,8 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FilterList import androidx.compose.material.icons.filled.FilterList
import androidx.compose.material.icons.filled.MoreVert import androidx.compose.material.icons.filled.MoreVert
@@ -17,6 +19,7 @@ import androidx.compose.material.icons.filled.PauseCircleOutline
import androidx.compose.material.icons.filled.PushPin import androidx.compose.material.icons.filled.PushPin
import androidx.compose.material.icons.filled.Refresh import androidx.compose.material.icons.filled.Refresh
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@@ -175,16 +178,23 @@ private fun TocContent(
AppCircularProgressIndicator() AppCircularProgressIndicator()
} }
} else { } else {
val listState = rememberLazyListState()
LaunchedEffect(state.currentTocIndex, state.tocItems) {
if (state.currentTocIndex >= 0) {
listState.scrollToItem(state.currentTocIndex)
}
}
LazyColumn( LazyColumn(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
state = listState,
verticalArrangement = Arrangement.spacedBy(4.dp) verticalArrangement = Arrangement.spacedBy(4.dp)
) { ) {
items(state.tocItems) { chapter -> itemsIndexed(state.tocItems) { index, chapter ->
SelectionItemCard( SelectionItemCard(
title = chapter.title, title = chapter.title,
containerColor = LegadoTheme.colorScheme.surfaceContainerLow, containerColor = LegadoTheme.colorScheme.surfaceContainerLow,
selectedContainerColor = LegadoTheme.colorScheme.primaryContainer.copy(alpha = 0.32f), selectedContainerColor = LegadoTheme.colorScheme.primaryContainer.copy(alpha = 0.32f),
isSelected = false, isSelected = index == state.currentTocIndex,
onToggleSelection = { onToggleSelection = {
onIntent(ChangeChapterSourceIntent.SelectChapter(chapter)) onIntent(ChangeChapterSourceIntent.SelectChapter(chapter))
}, },