Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 13d424b6 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Don't scroll to live content on initial content load" into main

parents 59d8e60d d6888b31
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -545,18 +545,35 @@ private fun ScrollOnUpdatedLiveContentEffect(
) {
    val coroutineScope = rememberCoroutineScope()
    val liveContentKeys = remember { mutableListOf<String>() }
    var communalContentPending by remember { mutableStateOf(true) }

    LaunchedEffect(communalContent) {
        // Do nothing until any communal content comes in
        if (communalContentPending && communalContent.isEmpty()) {
            return@LaunchedEffect
        }

        val prevLiveContentKeys = liveContentKeys.toList()
        val newLiveContentKeys = communalContent.filter { it.isLiveContent() }.map { it.key }
        liveContentKeys.clear()
        liveContentKeys.addAll(communalContent.filter { it.isLiveContent() }.map { it.key })
        liveContentKeys.addAll(newLiveContentKeys)

        // Find the first updated content
        // Do nothing on first communal content since we don't have a delta
        if (communalContentPending) {
            communalContentPending = false
            return@LaunchedEffect
        }

        // Do nothing if there is no new live content
        val indexOfFirstUpdatedContent =
            liveContentKeys.indexOfFirst { !prevLiveContentKeys.contains(it) }
            newLiveContentKeys.indexOfFirst { !prevLiveContentKeys.contains(it) }
        if (indexOfFirstUpdatedContent < 0) {
            return@LaunchedEffect
        }

        // Scroll if current position is behind the first updated content
        if (indexOfFirstUpdatedContent in 0 until gridState.firstVisibleItemIndex) {
        // Scroll if the live content is not visible
        val lastVisibleItemIndex = gridState.layoutInfo.visibleItemsInfo.lastOrNull()?.index
        if (lastVisibleItemIndex != null && indexOfFirstUpdatedContent > lastVisibleItemIndex) {
            // Launching with a scope to prevent the job from being canceled in the case of a
            // recomposition during scrolling
            coroutineScope.launch { gridState.animateScrollToItem(indexOfFirstUpdatedContent) }
+0 −5
Original line number Diff line number Diff line
@@ -614,11 +614,6 @@ constructor(
        _firstVisibleItemOffset = firstVisibleItemOffset
    }

    fun resetScrollPosition() {
        _firstVisibleItemIndex = 0
        _firstVisibleItemOffset = 0
    }

    val firstVisibleItemIndex: Int
        get() = _firstVisibleItemIndex