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.
*/
fun getDurChapterIndex(chapterIndex: Int, chapterTitle: String, toc: List<BookChapter>): Int {
return io.legado.app.help.book.BookHelp.getDurChapter(chapterIndex, chapterTitle, toc)
fun getDurChapterIndex(
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 tocItems: ImmutableList<BookChapter> = persistentListOf(),
val isLoadingToc: Boolean = false,
val currentTocIndex: Int = -1,
// Scope filter
val scopeState: ScopeUiState = ScopeUiState(
isAll = true,
@@ -78,6 +78,7 @@ class ChangeChapterSourceViewModel(
showToc = false,
tocItems = persistentListOf(),
isLoadingToc = false,
currentTocIndex = -1,
selectedSourceName = "",
)
}
@@ -113,7 +114,8 @@ class ChangeChapterSourceViewModel(
it.copy(
showToc = false,
tocItems = persistentListOf(),
isLoadingToc = false
isLoadingToc = false,
currentTocIndex = -1,
)
}
}
@@ -313,23 +315,32 @@ class ChangeChapterSourceViewModel(
it.copy(
showToc = true,
selectedSourceName = searchBook.originName,
isLoadingToc = true
isLoadingToc = true,
currentTocIndex = -1,
)
}
viewModelScope.launch {
try {
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 {
it.copy(
tocItems = toc.toImmutableList(),
isLoadingToc = false
isLoadingToc = false,
currentTocIndex = currentTocIndex,
)
}
} catch (e: Exception) {
_uiState.update {
it.copy(
showToc = false,
isLoadingToc = false
isLoadingToc = false,
currentTocIndex = -1,
)
}
_effects.tryEmit(ChangeChapterSourceEffect.ShowToast("获取目录失败"))
@@ -10,6 +10,8 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
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.filled.FilterList
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.Refresh
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -175,16 +178,23 @@ private fun TocContent(
AppCircularProgressIndicator()
}
} else {
val listState = rememberLazyListState()
LaunchedEffect(state.currentTocIndex, state.tocItems) {
if (state.currentTocIndex >= 0) {
listState.scrollToItem(state.currentTocIndex)
}
}
LazyColumn(
modifier = Modifier.fillMaxWidth(),
state = listState,
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(state.tocItems) { chapter ->
itemsIndexed(state.tocItems) { index, chapter ->
SelectionItemCard(
title = chapter.title,
containerColor = LegadoTheme.colorScheme.surfaceContainerLow,
selectedContainerColor = LegadoTheme.colorScheme.primaryContainer.copy(alpha = 0.32f),
isSelected = false,
isSelected = index == state.currentTocIndex,
onToggleSelection = {
onIntent(ChangeChapterSourceIntent.SelectChapter(chapter))
},