Skip to content

Commit

Permalink
timeline : do not call focusOnLive too soon #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ganfra committed Oct 1, 2024
1 parent f925cd9 commit 6463af8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ class TimelineController @Inject constructor(
return detachedTimeline.map { !it.isPresent }
}

fun currentPaginationStatus(direction: Timeline.PaginationDirection): Timeline.PaginationStatus {
return currentTimelineFlow.value.paginationStatus(direction).value
@OptIn(ExperimentalCoroutinesApi::class)
fun paginationStatus(direction: Timeline.PaginationDirection): Flow<Timeline.PaginationStatus> {
return currentTimelineFlow.flatMapLatest { timeline -> timeline.paginationStatus(direction)}
}

suspend fun invokeOnCurrentTimeline(block: suspend (Timeline.() -> Any)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -130,6 +131,8 @@ class TimelinePresenter @AssistedInject constructor(
val renderReadReceipts by sessionPreferencesStore.isRenderReadReceiptsEnabled().collectAsState(initial = true)
val isLive by timelineController.isLive().collectAsState(initial = true)

var shouldFocusOnLive by rememberSaveable { mutableStateOf(false) }

fun handleEvents(event: TimelineEvents) {
when (event) {
is TimelineEvents.LoadMore -> {
Expand All @@ -142,7 +145,7 @@ class TimelinePresenter @AssistedInject constructor(
if (event.firstIndex == 0) {
newEventState.value = NewEventState.None
}
println("## sendReadReceiptIfNeeded firstVisibleIndex: ${event.firstIndex}")
shouldFocusOnLive = false
appScope.sendReadReceiptIfNeeded(
firstVisibleIndex = event.firstIndex,
timelineItems = timelineItems,
Expand All @@ -151,12 +154,7 @@ class TimelinePresenter @AssistedInject constructor(
)
} else {
newEventState.value = NewEventState.None
if (event.firstIndex == 0) {
val forwardPaginationStatus = timelineController.currentPaginationStatus(Timeline.PaginationDirection.FORWARDS)
if (!forwardPaginationStatus.hasMoreToLoad) {
timelineController.focusOnLive()
}
}
shouldFocusOnLive = event.firstIndex == 0
}
}
is TimelineEvents.SelectPollAnswer -> appScope.launch {
Expand Down Expand Up @@ -193,6 +191,18 @@ class TimelinePresenter @AssistedInject constructor(
}
}

val hasMoreToLoadForward by remember {
timelineController
.paginationStatus(Timeline.PaginationDirection.FORWARDS)
.map { it.hasMoreToLoad }
}.collectAsState(false)

LaunchedEffect(shouldFocusOnLive, hasMoreToLoadForward) {
if (shouldFocusOnLive && !hasMoreToLoadForward) {
timelineController.focusOnLive()
}
}

LaunchedEffect(focusRequestState) {
when (val currentFocusRequestState = focusRequestState) {
is FocusRequestState.Requested -> {
Expand Down

0 comments on commit 6463af8

Please sign in to comment.