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

Commit d6888b31 authored by Lucas Silva's avatar Lucas Silva
Browse files

Don't scroll to live content on initial content load

When going from edit mode -> GH, we save the scroll position and restore
it. In this case, we should not immediately scroll away from the restored
scroll position. This change avoids scrolling on the initial content
load, and only scrolls on subsequent content changes.

Fixes: 357977239
Test: enter/exit edit mode with music playing
Flag: com.android.systemui.communal_hub
Change-Id: I5f846d5b12a7f416326c8a9cef5806bc5b00f09f
parent e935760f
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
@@ -607,11 +607,6 @@ constructor(
        _firstVisibleItemOffset = firstVisibleItemOffset
    }

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

    val firstVisibleItemIndex: Int
        get() = _firstVisibleItemIndex