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

Commit 12537140 authored by Coco Duan's avatar Coco Duan
Browse files

Move communal hub lazygrid logic to a separate function

As CommunalHub renders more stuff, move the grid logic to its own
compose function.

Bug: b/310969547
Flag: ACONFIG com.android.systemui.communal_hub DEVELOPMENT
Test: manual
Change-Id: Icc3721d91e0081b5c8c033a049401989a198df38
parent 03f5b4dc
Loading
Loading
Loading
Loading
+68 −53
Original line number Diff line number Diff line
@@ -61,7 +61,6 @@ import com.android.systemui.media.controls.ui.MediaHierarchyManager
import com.android.systemui.media.controls.ui.MediaHostState
import com.android.systemui.res.R

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CommunalHub(
    modifier: Modifier = Modifier,
@@ -72,11 +71,47 @@ fun CommunalHub(
    Box(
        modifier = modifier.fillMaxSize().background(Color.White),
    ) {
        var gridModifier = Modifier.height(Dimensions.GridHeight).align(Alignment.CenterStart)
        CommunalHubLazyGrid(
            modifier = Modifier.height(Dimensions.GridHeight).align(Alignment.CenterStart),
            communalContent = communalContent,
            isEditMode = viewModel.isEditMode,
            viewModel = viewModel,
        )
        if (viewModel.isEditMode && onOpenWidgetPicker != null) {
            IconButton(onClick = onOpenWidgetPicker) {
                Icon(Icons.Default.Add, stringResource(R.string.hub_mode_add_widget_button_text))
            }
        } else {
            IconButton(onClick = viewModel::onOpenWidgetEditor) {
                Icon(Icons.Default.Edit, stringResource(R.string.button_to_open_widget_editor))
            }
        }

        // This spacer covers the edge of the LazyHorizontalGrid and prevents it from receiving
        // touches, so that the SceneTransitionLayout can intercept the touches and allow an edge
        // swipe back to the blank scene.
        Spacer(
            Modifier.height(Dimensions.GridHeight)
                .align(Alignment.CenterStart)
                .width(Dimensions.Spacing)
                .pointerInput(Unit) {}
        )
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun CommunalHubLazyGrid(
    communalContent: List<CommunalContentModel>,
    isEditMode: Boolean,
    viewModel: BaseCommunalViewModel,
    modifier: Modifier = Modifier,
) {
    var gridModifier = modifier
    val gridState = rememberLazyGridState()
    var list = communalContent
    var dragDropState: GridDragDropState? = null
        if (viewModel.isEditMode && viewModel is CommunalEditModeViewModel) {
    if (isEditMode && viewModel is CommunalEditModeViewModel) {
        val contentListState = rememberContentListState(communalContent, viewModel)
        list = contentListState.list
        dragDropState = rememberGridDragDropState(gridState, contentListState)
@@ -101,7 +136,7 @@ fun CommunalHub(
                    Dimensions.CardWidth.value,
                    list[index].size.dp().value,
                )
                if (viewModel.isEditMode && dragDropState != null) {
            if (isEditMode && dragDropState != null) {
                DraggableItem(dragDropState = dragDropState, enabled = true, index = index) {
                    isDragging ->
                    val elevation by animateDpAsState(if (isDragging) 4.dp else 1.dp)
@@ -124,26 +159,6 @@ fun CommunalHub(
            }
        }
    }
        if (viewModel.isEditMode && onOpenWidgetPicker != null) {
            IconButton(onClick = onOpenWidgetPicker) {
                Icon(Icons.Default.Add, stringResource(R.string.hub_mode_add_widget_button_text))
            }
        } else {
            IconButton(onClick = viewModel::onOpenWidgetEditor) {
                Icon(Icons.Default.Edit, stringResource(R.string.button_to_open_widget_editor))
            }
        }

        // This spacer covers the edge of the LazyHorizontalGrid and prevents it from receiving
        // touches, so that the SceneTransitionLayout can intercept the touches and allow an edge
        // swipe back to the blank scene.
        Spacer(
            Modifier.height(Dimensions.GridHeight)
                .align(Alignment.CenterStart)
                .width(Dimensions.Spacing)
                .pointerInput(Unit) {}
        )
    }
}

@Composable