|
| 1 | +package com.OxGames.Pluvia.ui.component.dialog |
| 2 | + |
| 3 | +import android.content.res.Configuration |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.Row |
| 6 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 7 | +import androidx.compose.foundation.layout.height |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.foundation.selection.selectable |
| 10 | +import androidx.compose.foundation.selection.selectableGroup |
| 11 | +import androidx.compose.material.icons.Icons |
| 12 | +import androidx.compose.material.icons.filled.BrightnessMedium |
| 13 | +import androidx.compose.material3.AlertDialog |
| 14 | +import androidx.compose.material3.Icon |
| 15 | +import androidx.compose.material3.MaterialTheme |
| 16 | +import androidx.compose.material3.RadioButton |
| 17 | +import androidx.compose.material3.Text |
| 18 | +import androidx.compose.material3.TextButton |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.getValue |
| 21 | +import androidx.compose.runtime.mutableStateOf |
| 22 | +import androidx.compose.runtime.remember |
| 23 | +import androidx.compose.runtime.setValue |
| 24 | +import androidx.compose.ui.Alignment |
| 25 | +import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.platform.LocalContext |
| 27 | +import androidx.compose.ui.semantics.Role |
| 28 | +import androidx.compose.ui.tooling.preview.Preview |
| 29 | +import androidx.compose.ui.unit.dp |
| 30 | +import com.OxGames.Pluvia.PrefManager |
| 31 | +import com.OxGames.Pluvia.enums.AppTheme |
| 32 | +import com.OxGames.Pluvia.ui.theme.PluviaTheme |
| 33 | + |
| 34 | +@Composable |
| 35 | +fun AppThemeDialog( |
| 36 | + openDialog: Boolean, |
| 37 | + appTheme: AppTheme, |
| 38 | + onSelected: (AppTheme) -> Unit, |
| 39 | + onDismiss: () -> Unit, |
| 40 | +) { |
| 41 | + if (!openDialog) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + AlertDialog( |
| 46 | + onDismissRequest = onDismiss, |
| 47 | + icon = { |
| 48 | + Icon( |
| 49 | + imageVector = Icons.Default.BrightnessMedium, |
| 50 | + contentDescription = null, |
| 51 | + ) |
| 52 | + }, |
| 53 | + title = { Text(text = "App Theme") }, |
| 54 | + text = { |
| 55 | + Column(modifier = Modifier.selectableGroup()) { |
| 56 | + AppTheme.entries.forEach { entry -> |
| 57 | + Row( |
| 58 | + Modifier |
| 59 | + .fillMaxWidth() |
| 60 | + .height(56.dp) |
| 61 | + .selectable( |
| 62 | + selected = entry == appTheme, |
| 63 | + onClick = { onSelected(entry) }, |
| 64 | + role = Role.RadioButton, |
| 65 | + ) |
| 66 | + .padding(horizontal = 16.dp), |
| 67 | + verticalAlignment = Alignment.CenterVertically, |
| 68 | + ) { |
| 69 | + RadioButton( |
| 70 | + selected = entry == appTheme, |
| 71 | + onClick = null, |
| 72 | + ) |
| 73 | + Text( |
| 74 | + text = entry.name, |
| 75 | + style = MaterialTheme.typography.bodyLarge, |
| 76 | + modifier = Modifier.padding(start = 16.dp), |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + confirmButton = { |
| 83 | + TextButton(onClick = onDismiss) { |
| 84 | + Text(text = "Close") |
| 85 | + } |
| 86 | + }, |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL) |
| 91 | +@Composable |
| 92 | +private fun Preview_AppThemeDialog() { |
| 93 | + val content = LocalContext.current |
| 94 | + PrefManager.init(content) |
| 95 | + |
| 96 | + var theme by remember { mutableStateOf(AppTheme.DAY) } |
| 97 | + |
| 98 | + PluviaTheme { |
| 99 | + AppThemeDialog( |
| 100 | + openDialog = true, |
| 101 | + appTheme = theme, |
| 102 | + onSelected = { theme = it }, |
| 103 | + onDismiss = { }, |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
0 commit comments