|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.meshtastic.feature.settings.radio.component |
| 19 | + |
| 20 | +import androidx.compose.foundation.layout.Column |
| 21 | +import androidx.compose.foundation.layout.Spacer |
| 22 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 23 | +import androidx.compose.foundation.layout.height |
| 24 | +import androidx.compose.foundation.layout.padding |
| 25 | +import androidx.compose.material.icons.Icons |
| 26 | +import androidx.compose.material.icons.rounded.Warning |
| 27 | +import androidx.compose.material3.AlertDialog |
| 28 | +import androidx.compose.material3.Button |
| 29 | +import androidx.compose.material3.Icon |
| 30 | +import androidx.compose.material3.MaterialTheme |
| 31 | +import androidx.compose.material3.Text |
| 32 | +import androidx.compose.material3.TextButton |
| 33 | +import androidx.compose.runtime.Composable |
| 34 | +import androidx.compose.ui.Modifier |
| 35 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 36 | +import androidx.compose.ui.text.font.FontWeight |
| 37 | +import androidx.compose.ui.text.style.TextAlign |
| 38 | +import androidx.compose.ui.tooling.preview.Preview |
| 39 | +import androidx.compose.ui.unit.dp |
| 40 | +import org.jetbrains.compose.resources.stringResource |
| 41 | +import org.meshtastic.core.database.model.Node |
| 42 | +import org.meshtastic.core.strings.Res |
| 43 | +import org.meshtastic.core.strings.cancel |
| 44 | +import org.meshtastic.core.strings.send |
| 45 | +import org.meshtastic.core.strings.shutdown_node_name |
| 46 | +import org.meshtastic.core.strings.shutdown_warning |
| 47 | +import org.meshtastic.core.ui.theme.AppTheme |
| 48 | +import org.meshtastic.proto.MeshProtos |
| 49 | + |
| 50 | +@Composable |
| 51 | +fun ShutdownConfirmationDialog( |
| 52 | + title: String, |
| 53 | + node: Node?, |
| 54 | + onDismiss: () -> Unit, |
| 55 | + isShutdown: Boolean = true, |
| 56 | + icon: ImageVector? = Icons.Rounded.Warning, |
| 57 | + onConfirm: () -> Unit, |
| 58 | +) { |
| 59 | + val nodeLongName = node?.user?.longName ?: "Unknown Node" |
| 60 | + |
| 61 | + AlertDialog( |
| 62 | + onDismissRequest = {}, |
| 63 | + icon = { icon?.let { Icon(imageVector = it, contentDescription = null) } }, |
| 64 | + title = { Text(text = title) }, |
| 65 | + text = { ShutdownDialogContent(nodeLongName = nodeLongName, isShutdown = isShutdown) }, |
| 66 | + dismissButton = { TextButton(onClick = { onDismiss() }) { Text(stringResource(Res.string.cancel)) } }, |
| 67 | + confirmButton = { |
| 68 | + Button( |
| 69 | + onClick = { |
| 70 | + onDismiss() |
| 71 | + onConfirm() |
| 72 | + }, |
| 73 | + ) { |
| 74 | + Text(stringResource(Res.string.send)) |
| 75 | + } |
| 76 | + }, |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +@Composable |
| 81 | +private fun ShutdownDialogContent(nodeLongName: String, isShutdown: Boolean) { |
| 82 | + Column(modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)) { |
| 83 | + Text( |
| 84 | + text = stringResource(Res.string.shutdown_node_name, nodeLongName), |
| 85 | + style = MaterialTheme.typography.titleMedium, |
| 86 | + fontWeight = FontWeight.Bold, |
| 87 | + modifier = Modifier.fillMaxWidth(), |
| 88 | + textAlign = TextAlign.Center, |
| 89 | + ) |
| 90 | + |
| 91 | + if (isShutdown) { |
| 92 | + Spacer(modifier = Modifier.height(8.dp)) |
| 93 | + Text( |
| 94 | + text = stringResource(Res.string.shutdown_warning), |
| 95 | + style = MaterialTheme.typography.bodyMedium, |
| 96 | + color = MaterialTheme.colorScheme.error, |
| 97 | + modifier = Modifier.fillMaxWidth(), |
| 98 | + textAlign = TextAlign.Center, |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +@Preview |
| 105 | +@Composable |
| 106 | +private fun ShutdownConfirmationDialogPreview() { |
| 107 | + val mockNode = |
| 108 | + Node( |
| 109 | + num = 123, |
| 110 | + user = MeshProtos.User.newBuilder().setLongName("Rooftop Router Node").setShortName("ROOF").build(), |
| 111 | + ) |
| 112 | + |
| 113 | + AppTheme { ShutdownConfirmationDialog(title = "Shutdown?", node = mockNode, onDismiss = {}, onConfirm = {}) } |
| 114 | +} |
0 commit comments