1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.statusbar.policy.ui.dialog.composable 18 19 import androidx.compose.foundation.layout.Arrangement 20 import androidx.compose.foundation.layout.Column 21 import androidx.compose.foundation.layout.fillMaxHeight 22 import androidx.compose.foundation.layout.fillMaxWidth 23 import androidx.compose.foundation.layout.heightIn 24 import androidx.compose.foundation.layout.padding 25 import androidx.compose.foundation.lazy.grid.GridCells 26 import androidx.compose.foundation.lazy.grid.LazyVerticalGrid 27 import androidx.compose.foundation.pager.HorizontalPager 28 import androidx.compose.foundation.pager.rememberPagerState 29 import androidx.compose.material3.MaterialTheme 30 import androidx.compose.runtime.Composable 31 import androidx.compose.runtime.getValue 32 import androidx.compose.ui.Alignment 33 import androidx.compose.ui.Modifier 34 import androidx.compose.ui.unit.dp 35 import androidx.lifecycle.compose.collectAsStateWithLifecycle 36 import com.android.systemui.Flags 37 import com.android.systemui.common.ui.compose.PagerDots 38 import com.android.systemui.statusbar.policy.ui.dialog.viewmodel.ModesDialogViewModel 39 40 @Composable 41 fun ModeTileGrid(viewModel: ModesDialogViewModel) { 42 val tiles by viewModel.tiles.collectAsStateWithLifecycle(initialValue = emptyList()) 43 44 if (Flags.modesUiDialogPaging()) { 45 val tilesPerPage = 3 46 val totalPages = { (tiles.size + tilesPerPage - 1) / tilesPerPage } 47 val pagerState = rememberPagerState(initialPage = 0, pageCount = totalPages) 48 49 Column { 50 HorizontalPager( 51 state = pagerState, 52 modifier = Modifier.fillMaxWidth(), 53 pageSpacing = 16.dp, 54 verticalAlignment = Alignment.Top, 55 // Pre-emptively layout and compose the next page, to make sure the height stays 56 // the same even if we have fewer than [tilesPerPage] tiles on the last page. 57 beyondViewportPageCount = 1, 58 ) { page -> 59 Column( 60 modifier = Modifier.fillMaxWidth().fillMaxHeight(), 61 verticalArrangement = Arrangement.spacedBy(8.dp, alignment = Alignment.Top), 62 ) { 63 val startIndex = page * tilesPerPage 64 val endIndex = minOf((page + 1) * tilesPerPage, tiles.size) 65 for (index in startIndex until endIndex) { 66 ModeTile(viewModel = tiles[index], modifier = Modifier.fillMaxWidth()) 67 } 68 } 69 } 70 71 PagerDots( 72 pagerState = pagerState, 73 activeColor = MaterialTheme.colorScheme.primary, 74 nonActiveColor = MaterialTheme.colorScheme.onSurfaceVariant, 75 modifier = Modifier.align(Alignment.CenterHorizontally).padding(top = 8.dp), 76 ) 77 } 78 } else { 79 LazyVerticalGrid( 80 columns = GridCells.Fixed(1), 81 modifier = Modifier.fillMaxWidth().heightIn(max = 280.dp), 82 verticalArrangement = Arrangement.spacedBy(8.dp), 83 horizontalArrangement = Arrangement.spacedBy(8.dp), 84 ) { 85 items(tiles.size, key = { index -> tiles[index].id }) { index -> 86 ModeTile(viewModel = tiles[index]) 87 } 88 } 89 } 90 } 91