From 7e66f7589dc5415468d7dbca490a1b569cc6b865 Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Sun, 14 Jun 2026 00:11:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=95=E7=AB=A0=E6=8D=A2=E6=BA=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usecase/GetChapterContentUseCase.kt | 14 ++++++++++++-- .../ChangeChapterSourceContract.kt | 1 + .../ChangeChapterSourceViewModel.kt | 19 +++++++++++++++---- .../read/sheet/ChangeChapterSourceSheet.kt | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/domain/usecase/GetChapterContentUseCase.kt b/app/src/main/java/io/legado/app/domain/usecase/GetChapterContentUseCase.kt index eaf2d34d9..e4e1d92ca 100644 --- a/app/src/main/java/io/legado/app/domain/usecase/GetChapterContentUseCase.kt +++ b/app/src/main/java/io/legado/app/domain/usecase/GetChapterContentUseCase.kt @@ -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): Int { - return io.legado.app.help.book.BookHelp.getDurChapter(chapterIndex, chapterTitle, toc) + fun getDurChapterIndex( + chapterIndex: Int, + chapterTitle: String, + toc: List, + totalChapterNum: Int = 0, + ): Int { + return io.legado.app.help.book.BookHelp.getDurChapter( + chapterIndex, + chapterTitle, + toc, + totalChapterNum, + ) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceContract.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceContract.kt index 36b8c6272..e70cffd4c 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceContract.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceContract.kt @@ -24,6 +24,7 @@ data class ChangeChapterSourceUiState( val selectedSourceName: String = "", val tocItems: ImmutableList = persistentListOf(), val isLoadingToc: Boolean = false, + val currentTocIndex: Int = -1, // Scope filter val scopeState: ScopeUiState = ScopeUiState( isAll = true, diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt index 20eaf4d8c..006bbfe3a 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt @@ -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("获取目录失败")) diff --git a/app/src/main/java/io/legado/app/ui/book/read/sheet/ChangeChapterSourceSheet.kt b/app/src/main/java/io/legado/app/ui/book/read/sheet/ChangeChapterSourceSheet.kt index ebac6b55c..79da96455 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/sheet/ChangeChapterSourceSheet.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/sheet/ChangeChapterSourceSheet.kt @@ -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)) },