From 82d833b4349b4f760d175b5286d9c76e0d69b4e4 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 25 Apr 2024 17:41:10 +0200 Subject: [PATCH] Timeline permalink : scroll to fetched event when known --- .../messages/impl/timeline/TimelinePresenter.kt | 17 +++++++++++------ .../messages/impl/timeline/TimelineState.kt | 1 + .../messages/impl/timeline/TimelineView.kt | 4 ++-- .../components/TimelineItemVirtualRow.kt | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenter.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenter.kt index f3ba877888..61b2add5cb 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenter.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelinePresenter.kt @@ -146,7 +146,7 @@ class TimelinePresenter @AssistedInject constructor( timelineController.focusOnEvent(event.eventId) .fold( onSuccess = { - focusRequestState.value = FocusRequestState.None + focusRequestState.value = FocusRequestState.Fetched }, onFailure = { focusRequestState.value = FocusRequestState.Failure(it) @@ -165,15 +165,20 @@ class TimelinePresenter @AssistedInject constructor( } } - // Makes sure to get back to live when there is nothing more to load forwards - LaunchedEffect(isLive) { - - } - LaunchedEffect(timelineItems.size) { computeNewItemState(timelineItems, prevMostRecentItemId, newEventState) } + LaunchedEffect(timelineItems.size, focusRequestState.value, focusedEventId.value) { + val currentFocusedEventId = focusedEventId.value + if (focusRequestState.value is FocusRequestState.Fetched && currentFocusedEventId != null) { + if (timelineItemIndexer.isKnown(currentFocusedEventId)) { + val index = timelineItemIndexer.indexOf(currentFocusedEventId) + focusRequestState.value = FocusRequestState.Cached(index) + } + } + } + LaunchedEffect(Unit) { combine(timelineController.timelineItems(), room.membersStateFlow) { items, membersState -> timelineItemsFactory.replaceWith( diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineState.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineState.kt index 4a345af90c..67a7dac574 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineState.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineState.kt @@ -40,6 +40,7 @@ sealed interface FocusRequestState { data object None : FocusRequestState data class Cached(val index: Int): FocusRequestState data object Fetching : FocusRequestState + data object Fetched: FocusRequestState data class Failure(val throwable: Throwable) : FocusRequestState } diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineView.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineView.kt index db7b89833b..101ad58703 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineView.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/TimelineView.kt @@ -195,7 +195,7 @@ private fun FocusRequestStateView( FocusRequestState.Fetching -> { ProgressDialog(modifier = modifier, onDismissRequest = onClearFocusRequestState) } - is FocusRequestState.Cached, FocusRequestState.None -> Unit + else -> Unit } } @@ -237,7 +237,7 @@ private fun BoxScope.TimelineScrollHelper( } } - LaunchedEffect(key1 = focusRequestState) { + LaunchedEffect(focusRequestState) { if (focusRequestState is FocusRequestState.Cached) { if (abs(lazyListState.firstVisibleItemIndex - focusRequestState.index) < 10) { lazyListState.animateScrollToItem(focusRequestState.index) diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemVirtualRow.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemVirtualRow.kt index 9dd1210af0..df9d355dbf 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemVirtualRow.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemVirtualRow.kt @@ -51,7 +51,7 @@ fun TimelineItemVirtualRow( TimelineItemRoomBeginningModel -> TimelineItemRoomBeginningView(roomName = timelineRoomInfo.name) is TimelineItemLoadingIndicatorModel -> { TimelineLoadingMoreIndicator() - LaunchedEffect(key1 = virtual.model.timestamp) { + LaunchedEffect(virtual.model.timestamp) { eventSink(TimelineEvents.LoadMore(virtual.model.direction)) } }