fix bookshelf scroll-to-top request handling

This commit is contained in:
HapeLee committed 2026-07-02 22:16:32 +08:00
1 parent 040f92d4f2
commit b9a54d5ae2
2 files changed
+18 -11

No files matched your search

@@ -436,6 +436,11 @@ fun MainScreen(
MainDestination.Bookshelf -> BookshelfScreen( MainDestination.Bookshelf -> BookshelfScreen(
scrollToTopRequest = bookshelfScrollToTopRequest, scrollToTopRequest = bookshelfScrollToTopRequest,
onScrollToTopRequestHandled = { handledRequest ->
if (bookshelfScrollToTopRequest == handledRequest) {
bookshelfScrollToTopRequest = 0L
}
},
onBookClick = { book -> onBookClick = { book ->
context.startActivityForBook(book) context.startActivityForBook(book)
}, },
@@ -158,6 +158,7 @@ import sh.calvin.reorderable.rememberReorderableLazyGridState
fun BookshelfScreen( fun BookshelfScreen(
viewModel: BookshelfViewModel = koinViewModel(), viewModel: BookshelfViewModel = koinViewModel(),
scrollToTopRequest: Long = 0L, scrollToTopRequest: Long = 0L,
onScrollToTopRequestHandled: (Long) -> Unit = {},
onBookClick: (BookShelfItem) -> Unit, onBookClick: (BookShelfItem) -> Unit,
onBookLongClick: (book: BookShelfItem, sharedCoverKey: String?) -> Unit, onBookLongClick: (book: BookShelfItem, sharedCoverKey: String?) -> Unit,
onNavigateToSearch: (String) -> Unit, onNavigateToSearch: (String) -> Unit,
@@ -293,9 +294,18 @@ fun BookshelfScreen(
} }
val isShowingFolderRoot = val isShowingFolderRoot =
bookGroupStyle == 2 && isInFolderRoot && !isUsingStandaloneSearchGroup bookGroupStyle == 2 && isInFolderRoot && !isUsingStandaloneSearchGroup
LaunchedEffect(scrollToTopRequest, isShowingFolderRoot) { LaunchedEffect(scrollToTopRequest) {
if (scrollToTopRequest > 0L && isShowingFolderRoot) { if (scrollToTopRequest > 0L) {
folderGridState.animateScrollToItem(0) val targetGridState = when {
isShowingFolderRoot -> folderGridState
isUsingStandaloneSearchGroup -> standaloneSearchGridState
else -> groupGridStates[currentGroupId]
}
try {
targetGridState?.animateScrollToItem(0)
} finally {
onScrollToTopRequestHandled(scrollToTopRequest)
}
} }
} }
val currentGroupBookCount by remember { derivedStateOf { uiState.currentGroupBookCount } } val currentGroupBookCount by remember { derivedStateOf { uiState.currentGroupBookCount } }
@@ -833,7 +843,6 @@ fun BookshelfScreen(
} else { } else {
if (isUsingStandaloneSearchGroup) { if (isUsingStandaloneSearchGroup) {
BookshelfPage( BookshelfPage(
scrollToTopRequest = scrollToTopRequest,
gridState = standaloneSearchGridState, gridState = standaloneSearchGridState,
paddingValues = paddingValues, paddingValues = paddingValues,
books = uiState.items, books = uiState.items,
@@ -881,7 +890,6 @@ fun BookshelfScreen(
?: uiState.bookshelfSort) == 3 && ?: uiState.bookshelfSort) == 3 &&
isSelectedGroup isSelectedGroup
BookshelfPage( BookshelfPage(
scrollToTopRequest = scrollToTopRequest,
gridState = groupGridStates.getValue(group.groupId), gridState = groupGridStates.getValue(group.groupId),
paddingValues = paddingValues, paddingValues = paddingValues,
books = books, books = books,
@@ -1232,7 +1240,6 @@ private data class BookshelfEditStickySummary(
@Composable @Composable
fun BookshelfPage( fun BookshelfPage(
scrollToTopRequest: Long,
gridState: LazyGridState, gridState: LazyGridState,
paddingValues: PaddingValues, paddingValues: PaddingValues,
books: ImmutableList<BookUiItem>, books: ImmutableList<BookUiItem>,
@@ -1319,11 +1326,6 @@ fun BookshelfPage(
val gridInnerHorizontalPadding = totalHorizontalPadding / 2 val gridInnerHorizontalPadding = totalHorizontalPadding / 2
val hapticFeedback = LocalHapticFeedback.current val hapticFeedback = LocalHapticFeedback.current
val displayBooks = draggingBooks ?: pendingSavedBooks ?: books val displayBooks = draggingBooks ?: pendingSavedBooks ?: books
LaunchedEffect(scrollToTopRequest, isCurrentPage) {
if (scrollToTopRequest > 0L && isCurrentPage) {
gridState.animateScrollToItem(0)
}
}
val reorderableState = rememberReorderableLazyGridState(gridState) { from, to -> val reorderableState = rememberReorderableLazyGridState(gridState) { from, to ->
if (canReorderBooks) { if (canReorderBooks) {
onMoveBook(from.index, to.index, displayBooks) onMoveBook(from.index, to.index, displayBooks)