Add timeline prefetching

This should trigger when getting close the start of the loaded timeline, making scrolling back smoother, specially when combined with the persistent event cache.
This commit is contained in:
Jorge Martín
2025-03-12 16:19:36 +01:00
parent 70c8524e20
commit 98bb5720d1
2 changed files with 15 additions and 0 deletions

View File

@@ -70,8 +70,10 @@ import io.element.android.libraries.designsystem.theme.components.Icon
import io.element.android.libraries.designsystem.utils.animateScrollToItemCenter
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.timeline.Timeline
import io.element.android.libraries.ui.strings.CommonStrings
import kotlinx.coroutines.launch
import timber.log.Timber
@Composable
fun TimelineView(
@@ -175,6 +177,17 @@ fun TimelineView(
onClearFocusRequestState = ::clearFocusRequestState
)
val isCloseToStartOfLoadedTimeline by remember { derivedStateOf {
lazyListState.firstVisibleItemIndex + lazyListState.layoutInfo.visibleItemsInfo.size >= lazyListState.layoutInfo.totalItemsCount - 10
} }
LaunchedEffect(isCloseToStartOfLoadedTimeline) {
// Only back paginate when we're close to the start of the loaded timeline items and the user is actively scrolling
if (lazyListState.isScrollInProgress && isCloseToStartOfLoadedTimeline) {
Timber.d("Prefetching pagination with ${lazyListState.layoutInfo.totalItemsCount} items")
state.eventSink(TimelineEvents.LoadMore(Timeline.PaginationDirection.BACKWARDS))
}
}
TimelineScrollHelper(
hasAnyEvent = state.hasAnyEvent,
lazyListState = lazyListState,

View File

@@ -28,6 +28,7 @@ import io.element.android.features.messages.impl.timeline.model.virtual.Timeline
import io.element.android.features.messages.impl.timeline.model.virtual.TimelineItemRoomBeginningModel
import io.element.android.features.messages.impl.timeline.model.virtual.TimelineItemTypingNotificationModel
import io.element.android.features.messages.impl.typing.TypingNotificationView
import timber.log.Timber
@Composable
fun TimelineItemVirtualRow(
@@ -45,6 +46,7 @@ fun TimelineItemVirtualRow(
TimelineLoadingMoreIndicator(virtual.model.direction)
val latestEventSink by rememberUpdatedState(eventSink)
LaunchedEffect(virtual.model.timestamp) {
Timber.d("Pagination triggered by load more indicator")
latestEventSink(TimelineEvents.LoadMore(virtual.model.direction))
}
}