diff options
| author | 2024-03-05 14:05:44 -0800 | |
|---|---|---|
| committer | 2024-03-07 01:28:05 +0000 | |
| commit | cc2b6b34e776c0f9f92c461d396eb00cfd82b8b9 (patch) | |
| tree | 1330ea482bfbc09aaf5317847a68c880ee41ebaf | |
| parent | cef6c818a0a049a2a0a196f305e5e9163c4cd6f4 (diff) | |
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
2 files changed, 34 insertions, 0 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 515c8169f1c4..abfba3d6db86 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 @@ -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( diff --git a/packages/SystemUI/src/com/android/systemui/communal/domain/model/CommunalContentModel.kt b/packages/SystemUI/src/com/android/systemui/communal/domain/model/CommunalContentModel.kt index c64f666ebf10..12576d466823 100644 --- a/packages/SystemUI/src/com/android/systemui/communal/domain/model/CommunalContentModel.kt +++ b/packages/SystemUI/src/com/android/systemui/communal/domain/model/CommunalContentModel.kt @@ -152,4 +152,6 @@ sealed interface CommunalContentModel { } fun isWidgetContent() = this is WidgetContent + + fun isSmartspace() = this is Smartspace } |