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

Commit cc2b6b34 authored by Bryce Lee's avatar Bryce Lee
Browse files

Adjust scroll position when smartspace content appears.

This changelist scrolls the communal hub to the beginning of the
smartspace section whenever the smartspace content grows.

Test: Manual. Verified scroll position is updated when new smartspace
timers are added.
Flag: ACONFIG com.android.systemui.communal_hub STAGING
Fixes: 325635703

Change-Id: I1b74720d9a67f32ca0ab0dcb41898884f842e19b
parent cef6c818
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
@@ -74,6 +75,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
@@ -152,6 +154,8 @@ fun CommunalHub(
    val contentPadding = gridContentPadding(viewModel.isEditMode, toolbarSize)
    val contentOffset = beforeContentPadding(contentPadding).toOffset()

    ScrollOnNewSmartspaceEffect(viewModel, gridState)

    Box(
        modifier =
            modifier
@@ -279,6 +283,34 @@ fun CommunalHub(
    }
}

@Composable
private fun ScrollOnNewSmartspaceEffect(
    viewModel: BaseCommunalViewModel,
    gridState: LazyGridState
) {
    val communalContent by viewModel.communalContent.collectAsState(initial = emptyList())
    var smartspaceCount by remember { mutableStateOf(0) }

    LaunchedEffect(communalContent) {
        snapshotFlow { gridState.firstVisibleItemIndex }
            .collect { index ->
                val existingSmartspaceCount = smartspaceCount
                smartspaceCount = communalContent.count { it.isSmartspace() }
                val firstIndex = communalContent.indexOfFirst { it.isSmartspace() }

                // Scroll to the beginning of the smartspace area whenever the number of
                // smartspace elements grows
                if (
                    existingSmartspaceCount < smartspaceCount &&
                        !viewModel.isEditMode &&
                        index > firstIndex
                ) {
                    gridState.animateScrollToItem(firstIndex)
                }
            }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun ColumnScope.CommunalHubLazyGrid(
+2 −0
Original line number Diff line number Diff line
@@ -152,4 +152,6 @@ sealed interface CommunalContentModel {
    }

    fun isWidgetContent() = this is WidgetContent

    fun isSmartspace() = this is Smartspace
}