From d6888b31a6f32ffe248265893d6d0a566bf43610 Mon Sep 17 00:00:00 2001 From: Lucas Silva Date: Fri, 23 Aug 2024 11:25:18 -0400 Subject: 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 --- .../systemui/communal/ui/compose/CommunalHub.kt | 27 ++++++++++++++++++---- .../domain/interactor/CommunalInteractor.kt | 5 ---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalHub.kt b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalHub.kt index 6f415ea334af..b0590e06d3bd 100644 --- a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalHub.kt +++ b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalHub.kt @@ -545,18 +545,35 @@ private fun ScrollOnUpdatedLiveContentEffect( ) { val coroutineScope = rememberCoroutineScope() val liveContentKeys = remember { mutableListOf() } + 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) } diff --git a/packages/SystemUI/src/com/android/systemui/communal/domain/interactor/CommunalInteractor.kt b/packages/SystemUI/src/com/android/systemui/communal/domain/interactor/CommunalInteractor.kt index 2aa6c1947c6d..f9744187cb34 100644 --- a/packages/SystemUI/src/com/android/systemui/communal/domain/interactor/CommunalInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/communal/domain/interactor/CommunalInteractor.kt @@ -607,11 +607,6 @@ constructor( _firstVisibleItemOffset = firstVisibleItemOffset } - fun resetScrollPosition() { - _firstVisibleItemIndex = 0 - _firstVisibleItemOffset = 0 - } - val firstVisibleItemIndex: Int get() = _firstVisibleItemIndex -- cgit v1.2.3-59-g8ed1b