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, orientation = Orientation.Horizontal,
) )
var bookshelfScrollToTopRequest by remember { mutableLongStateOf(0L) } var bookshelfScrollToTopRequest by remember { mutableLongStateOf(0L) }
var isBookshelfAtTop by remember { mutableStateOf(true) }
fun requestBookshelfScrollToTop() { fun requestBookshelfScrollToTop() {
bookshelfScrollToTopRequest++ bookshelfScrollToTopRequest++
} }
@@ -212,8 +211,7 @@ fun MainScreen(
if ( if (
destination == MainDestination.Bookshelf && destination == MainDestination.Bookshelf &&
pagerState.currentPage == index && pagerState.currentPage == index &&
pagerState.targetPage == index && pagerState.targetPage == index
!isBookshelfAtTop
) { ) {
requestBookshelfScrollToTop() requestBookshelfScrollToTop()
return return
@@ -438,9 +436,6 @@ fun MainScreen(
MainDestination.Bookshelf -> BookshelfScreen( MainDestination.Bookshelf -> BookshelfScreen(
scrollToTopRequest = bookshelfScrollToTopRequest, scrollToTopRequest = bookshelfScrollToTopRequest,
onScrollStateChanged = { isAtTop ->
isBookshelfAtTop = isAtTop
},
onBookClick = { book -> onBookClick = { book ->
context.startActivityForBook(book) context.startActivityForBook(book)
}, },
@@ -158,7 +158,6 @@ import sh.calvin.reorderable.rememberReorderableLazyGridState
fun BookshelfScreen( fun BookshelfScreen(
viewModel: BookshelfViewModel = koinViewModel(), viewModel: BookshelfViewModel = koinViewModel(),
scrollToTopRequest: Long = 0L, scrollToTopRequest: Long = 0L,
onScrollStateChanged: (isAtTop: Boolean) -> 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,
@@ -294,13 +293,6 @@ fun BookshelfScreen(
} }
val isShowingFolderRoot = val isShowingFolderRoot =
bookGroupStyle == 2 && isInFolderRoot && !isUsingStandaloneSearchGroup bookGroupStyle == 2 && isInFolderRoot && !isUsingStandaloneSearchGroup
LaunchedEffect(folderGridState, isShowingFolderRoot) {
if (isShowingFolderRoot) {
snapshotFlow { !folderGridState.canScrollBackward }
.distinctUntilChanged()
.collect(onScrollStateChanged)
}
}
LaunchedEffect(scrollToTopRequest, isShowingFolderRoot) { LaunchedEffect(scrollToTopRequest, isShowingFolderRoot) {
if (scrollToTopRequest > 0L && isShowingFolderRoot) { if (scrollToTopRequest > 0L && isShowingFolderRoot) {
folderGridState.animateScrollToItem(0) folderGridState.animateScrollToItem(0)
@@ -752,9 +744,6 @@ fun BookshelfScreen(
} }
) { isRoot -> ) { isRoot ->
if (uiState.groups.isEmpty() && !uiState.isSearch) { if (uiState.groups.isEmpty() && !uiState.isSearch) {
LaunchedEffect(Unit) {
onScrollStateChanged(true)
}
if (!uiState.isInitialLoading) { if (!uiState.isInitialLoading) {
EmptyMessage( EmptyMessage(
modifier = Modifier modifier = Modifier
@@ -846,7 +835,6 @@ fun BookshelfScreen(
BookshelfPage( BookshelfPage(
scrollToTopRequest = scrollToTopRequest, scrollToTopRequest = scrollToTopRequest,
gridState = standaloneSearchGridState, gridState = standaloneSearchGridState,
onScrollStateChanged = onScrollStateChanged,
paddingValues = paddingValues, paddingValues = paddingValues,
books = uiState.items, books = uiState.items,
uiState = uiState, uiState = uiState,
@@ -895,7 +883,6 @@ fun BookshelfScreen(
BookshelfPage( BookshelfPage(
scrollToTopRequest = scrollToTopRequest, scrollToTopRequest = scrollToTopRequest,
gridState = groupGridStates.getValue(group.groupId), gridState = groupGridStates.getValue(group.groupId),
onScrollStateChanged = onScrollStateChanged,
paddingValues = paddingValues, paddingValues = paddingValues,
books = books, books = books,
uiState = uiState, uiState = uiState,
@@ -1247,7 +1234,6 @@ private data class BookshelfEditStickySummary(
fun BookshelfPage( fun BookshelfPage(
scrollToTopRequest: Long, scrollToTopRequest: Long,
gridState: LazyGridState, gridState: LazyGridState,
onScrollStateChanged: (isAtTop: Boolean) -> Unit,
paddingValues: PaddingValues, paddingValues: PaddingValues,
books: ImmutableList<BookUiItem>, books: ImmutableList<BookUiItem>,
uiState: BookshelfUiState, uiState: BookshelfUiState,
@@ -1266,11 +1252,6 @@ fun BookshelfPage(
sharedTransitionScope: SharedTransitionScope? = null, sharedTransitionScope: SharedTransitionScope? = null,
animatedVisibilityScope: AnimatedVisibilityScope? = null, animatedVisibilityScope: AnimatedVisibilityScope? = null,
) { ) {
LaunchedEffect(books.isEmpty(), isCurrentPage) {
if (books.isEmpty() && isCurrentPage) {
onScrollStateChanged(true)
}
}
if (books.isEmpty()) { if (books.isEmpty()) {
if (!isCurrentPage) return if (!isCurrentPage) return
if (uiState.isSearch) { if (uiState.isSearch) {
@@ -1343,13 +1324,6 @@ fun BookshelfPage(
gridState.animateScrollToItem(0) gridState.animateScrollToItem(0)
} }
} }
LaunchedEffect(gridState, isCurrentPage) {
if (isCurrentPage) {
snapshotFlow { !gridState.canScrollBackward }
.distinctUntilChanged()
.collect(onScrollStateChanged)
}
}
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)