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

Commit 9bd0c7f6 authored by Darrell Shi's avatar Darrell Shi Committed by Android (Google) Code Review
Browse files

Merge "Scroll to newly added widget if needed" into main

parents 8b3392e2 e5e09e2a
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -216,7 +216,9 @@ fun CommunalHub(
    val screenWidth = windowMetrics.bounds.width()
    val layoutDirection = LocalLayoutDirection.current

    if (!viewModel.isEditMode) {
    if (viewModel.isEditMode) {
        ScrollOnNewWidgetAddedEffect(communalContent, gridState)
    } else {
        ScrollOnUpdatedLiveContentEffect(communalContent, gridState)
    }

@@ -547,6 +549,36 @@ private fun ScrollOnUpdatedLiveContentEffect(
    }
}

/** Observes communal content and scrolls to a newly added widget if any. */
@Composable
private fun ScrollOnNewWidgetAddedEffect(
    communalContent: List<CommunalContentModel>,
    gridState: LazyGridState,
) {
    val coroutineScope = rememberCoroutineScope()
    val widgetKeys = remember { mutableListOf<String>() }

    LaunchedEffect(communalContent) {
        val oldWidgetKeys = widgetKeys.toList()
        widgetKeys.clear()
        widgetKeys.addAll(communalContent.filter { it.isWidgetContent() }.map { it.key })

        // Do nothing if there is no new widget
        val indexOfFirstNewWidget = widgetKeys.indexOfFirst { !oldWidgetKeys.contains(it) }
        if (indexOfFirstNewWidget < 0) {
            return@LaunchedEffect
        }

        // Scroll if the new widget is not visible
        val lastVisibleItemIndex = gridState.layoutInfo.visibleItemsInfo.lastOrNull()?.index
        if (lastVisibleItemIndex != null && indexOfFirstNewWidget > lastVisibleItemIndex) {
            // Launching with a scope to prevent the job from being canceled in the case of a
            // recomposition during scrolling
            coroutineScope.launch { gridState.animateScrollToItem(indexOfFirstNewWidget) }
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun BoxScope.CommunalHubLazyGrid(