Merge the read receipts code with TimelineScrollHelper (#736)

This commit is contained in:
Jorge Martin Espinosa 2023-06-30 20:54:29 +02:00 committed by GitHub
parent 5f7f770332
commit a52f35db01

View file

@ -77,6 +77,10 @@ fun TimelineView(
state.eventSink(TimelineEvents.LoadMore) state.eventSink(TimelineEvents.LoadMore)
} }
fun onScrollFinishedAt(firstVisibleIndex: Int) {
state.eventSink(TimelineEvents.OnScrollFinished(firstVisibleIndex))
}
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
fun inReplyToClicked(eventId: EventId) { fun inReplyToClicked(eventId: EventId) {
@ -121,7 +125,8 @@ fun TimelineView(
TimelineScrollHelper( TimelineScrollHelper(
lazyListState = lazyListState, lazyListState = lazyListState,
timelineItems = state.timelineItems timelineItems = state.timelineItems,
onScrollFinishedAt = ::onScrollFinishedAt,
) )
} }
} }
@ -219,21 +224,29 @@ fun TimelineItemRow(
internal fun BoxScope.TimelineScrollHelper( internal fun BoxScope.TimelineScrollHelper(
lazyListState: LazyListState, lazyListState: LazyListState,
timelineItems: ImmutableList<TimelineItem>, timelineItems: ImmutableList<TimelineItem>,
onScrollFinishedAt: (Int) -> Unit = {},
) { ) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val firstVisibleItemIndex by remember { derivedStateOf { lazyListState.firstVisibleItemIndex } } val firstVisibleItemIndex by remember { derivedStateOf { lazyListState.firstVisibleItemIndex } }
val isScrollFinished by remember { derivedStateOf { !lazyListState.isScrollInProgress } }
val shouldAutoScrollToBottom by remember { derivedStateOf { lazyListState.firstVisibleItemIndex < 2 } }
// Auto-scroll when new timeline items appear LaunchedEffect(timelineItems, firstVisibleItemIndex, isScrollFinished) {
LaunchedEffect(timelineItems, firstVisibleItemIndex) { if (!isScrollFinished) return@LaunchedEffect
if (!lazyListState.isScrollInProgress &&
firstVisibleItemIndex < 2 // Notify the parent composable about the first visible item index when scrolling finishes
) coroutineScope.launch { onScrollFinishedAt(firstVisibleItemIndex)
lazyListState.animateScrollToItem(0)
// Auto-scroll when new timeline items appear
if (shouldAutoScrollToBottom) {
coroutineScope.launch {
lazyListState.animateScrollToItem(0)
}
} }
} }
// Jump to bottom button // Jump to bottom button
if (firstVisibleItemIndex > 2) { if (!shouldAutoScrollToBottom) {
FloatingActionButton( FloatingActionButton(
onClick = { onClick = {
coroutineScope.launch { coroutineScope.launch {