refactor(bookshelf): simplify scroll-to-top handling

This commit is contained in:
HapeLee
2026-07-01 23:43:14 +08:00
parent 6623fb7294
commit 6e4917fb5d
2 changed files with 1 additions and 32 deletions
@@ -203,7 +203,6 @@ fun MainScreen(
orientation = Orientation.Horizontal,
)
var bookshelfScrollToTopRequest by remember { mutableLongStateOf(0L) }
var isBookshelfAtTop by remember { mutableStateOf(true) }
fun requestBookshelfScrollToTop() {
bookshelfScrollToTopRequest++
}
@@ -212,8 +211,7 @@ fun MainScreen(
if (
destination == MainDestination.Bookshelf &&
pagerState.currentPage == index &&
pagerState.targetPage == index &&
!isBookshelfAtTop
pagerState.targetPage == index
) {
requestBookshelfScrollToTop()
return
@@ -438,9 +436,6 @@ fun MainScreen(
MainDestination.Bookshelf -> BookshelfScreen(
scrollToTopRequest = bookshelfScrollToTopRequest,
onScrollStateChanged = { isAtTop ->
isBookshelfAtTop = isAtTop
},
onBookClick = { book ->
context.startActivityForBook(book)
},
@@ -158,7 +158,6 @@ import sh.calvin.reorderable.rememberReorderableLazyGridState
fun BookshelfScreen(
viewModel: BookshelfViewModel = koinViewModel(),
scrollToTopRequest: Long = 0L,
onScrollStateChanged: (isAtTop: Boolean) -> Unit = {},
onBookClick: (BookShelfItem) -> Unit,
onBookLongClick: (book: BookShelfItem, sharedCoverKey: String?) -> Unit,
onNavigateToSearch: (String) -> Unit,
@@ -294,13 +293,6 @@ fun BookshelfScreen(
}
val isShowingFolderRoot =
bookGroupStyle == 2 && isInFolderRoot && !isUsingStandaloneSearchGroup
LaunchedEffect(folderGridState, isShowingFolderRoot) {
if (isShowingFolderRoot) {
snapshotFlow { !folderGridState.canScrollBackward }
.distinctUntilChanged()
.collect(onScrollStateChanged)
}
}
LaunchedEffect(scrollToTopRequest, isShowingFolderRoot) {
if (scrollToTopRequest > 0L && isShowingFolderRoot) {
folderGridState.animateScrollToItem(0)
@@ -752,9 +744,6 @@ fun BookshelfScreen(
}
) { isRoot ->
if (uiState.groups.isEmpty() && !uiState.isSearch) {
LaunchedEffect(Unit) {
onScrollStateChanged(true)
}
if (!uiState.isInitialLoading) {
EmptyMessage(
modifier = Modifier
@@ -846,7 +835,6 @@ fun BookshelfScreen(
BookshelfPage(
scrollToTopRequest = scrollToTopRequest,
gridState = standaloneSearchGridState,
onScrollStateChanged = onScrollStateChanged,
paddingValues = paddingValues,
books = uiState.items,
uiState = uiState,
@@ -895,7 +883,6 @@ fun BookshelfScreen(
BookshelfPage(
scrollToTopRequest = scrollToTopRequest,
gridState = groupGridStates.getValue(group.groupId),
onScrollStateChanged = onScrollStateChanged,
paddingValues = paddingValues,
books = books,
uiState = uiState,
@@ -1247,7 +1234,6 @@ private data class BookshelfEditStickySummary(
fun BookshelfPage(
scrollToTopRequest: Long,
gridState: LazyGridState,
onScrollStateChanged: (isAtTop: Boolean) -> Unit,
paddingValues: PaddingValues,
books: ImmutableList<BookUiItem>,
uiState: BookshelfUiState,
@@ -1266,11 +1252,6 @@ fun BookshelfPage(
sharedTransitionScope: SharedTransitionScope? = null,
animatedVisibilityScope: AnimatedVisibilityScope? = null,
) {
LaunchedEffect(books.isEmpty(), isCurrentPage) {
if (books.isEmpty() && isCurrentPage) {
onScrollStateChanged(true)
}
}
if (books.isEmpty()) {
if (!isCurrentPage) return
if (uiState.isSearch) {
@@ -1343,13 +1324,6 @@ fun BookshelfPage(
gridState.animateScrollToItem(0)
}
}
LaunchedEffect(gridState, isCurrentPage) {
if (isCurrentPage) {
snapshotFlow { !gridState.canScrollBackward }
.distinctUntilChanged()
.collect(onScrollStateChanged)
}
}
val reorderableState = rememberReorderableLazyGridState(gridState) { from, to ->
if (canReorderBooks) {
onMoveBook(from.index, to.index, displayBooks)