BackupDialogs.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/data/BackupDialogs.kt
1package eu.friedcutlet.yokan.ui.screens.settings.data23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.fillMaxWidth6import androidx.compose.material3.AlertDialog7import androidx.compose.material3.MaterialTheme8import androidx.compose.material3.OutlinedTextField9import androidx.compose.material3.Text10import androidx.compose.material3.TextButton11import androidx.compose.runtime.Composable12import androidx.compose.runtime.getValue13import androidx.compose.runtime.mutableStateOf14import androidx.compose.runtime.remember15import androidx.compose.runtime.setValue16import androidx.compose.ui.Modifier17import androidx.compose.ui.text.input.PasswordVisualTransformation18import androidx.compose.ui.unit.dp19import eu.friedcutlet.yokan.services.backup.MAX_BACKUP_PASSWORD_BYTES20import eu.friedcutlet.yokan.services.backup.SafetySnapshot21import eu.friedcutlet.yokan.services.backup.isValidBackupPassword22import eu.friedcutlet.yokan.ui.components.ConfirmDialog23import org.jetbrains.compose.resources.stringResource24import yokan.composeapp.generated.resources.Res25import yokan.composeapp.generated.resources.action_cancel26import yokan.composeapp.generated.resources.action_open27import yokan.composeapp.generated.resources.ok2829/** Asks for a backup encryption password twice; [onConfirm] only receives a password the backup envelope accepts. */30@Composable31internal fun BackupPasswordDialog(32 onConfirm: (String) -> Unit,33 onDismiss: () -> Unit,34 title: String = "Set backup password",35 confirmLabel: String = stringResource(Res.string.ok),36) {37 var password by remember { mutableStateOf("") }38 var confirmation by remember { mutableStateOf("") }39 var error by remember { mutableStateOf<String?>(null) }40 AlertDialog(41 onDismissRequest = onDismiss,42 title = { Text(title) },43 text = {44 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {45 Text(46 "Enter a password (1–$MAX_BACKUP_PASSWORD_BYTES UTF-8 bytes) to encrypt backup archives using AES-256-GCM. You will need this password to restore your backups.",47 style = MaterialTheme.typography.bodySmall,48 color = MaterialTheme.colorScheme.onSurfaceVariant,49 )50 OutlinedTextField(51 value = password,52 onValueChange = {53 password = it54 error = null55 },56 label = { Text("Password") },57 visualTransformation = PasswordVisualTransformation(),58 singleLine = true,59 modifier = Modifier.fillMaxWidth(),60 )61 OutlinedTextField(62 value = confirmation,63 onValueChange = {64 confirmation = it65 error = null66 },67 label = { Text("Confirm password") },68 visualTransformation = PasswordVisualTransformation(),69 singleLine = true,70 isError = error != null,71 supportingText = error?.let { message -> { Text(message) } },72 modifier = Modifier.fillMaxWidth(),73 )74 }75 },76 confirmButton = {77 TextButton(78 onClick = {79 error = when {80 password != confirmation -> "Passwords do not match"81 !isValidBackupPassword(password) -> "Password must be 1–$MAX_BACKUP_PASSWORD_BYTES UTF-8 bytes"82 else -> null83 }84 if (error == null) onConfirm(password)85 },86 enabled = password.isNotEmpty() && confirmation.isNotEmpty(),87 ) { Text(confirmLabel) }88 },89 dismissButton = {90 TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) }91 },92 )93}9495/** Confirms rolling the database back to [snapshot], taken at [dateLabel]. */96@Composable97internal fun RollbackConfirmDialog(98 snapshot: SafetySnapshot,99 dateLabel: String,100 onConfirm: () -> Unit,101 onDismiss: () -> Unit,102) {103 ConfirmDialog(104 title = "Roll back last change",105 message = "Roll back to safety snapshot from $dateLabel (${snapshot.description})? This will undo the changes and return your database to its state prior to that operation.",106 confirmLabel = "Roll back",107 danger = true,108 onConfirm = onConfirm,109 onDismiss = onDismiss,110 )111}112113/** Explains what a restore does before the file picker opens. */114@Composable115internal fun RestoreBackupWarningDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {116 ConfirmDialog(117 title = "Restore backup",118 message = "Choose a backup file to restore. You will be able to review its contents and choose between Merge (keeping existing titles) or Replace (overwriting library). A safety backup will be created automatically beforehand so this change can be rolled back.",119 confirmLabel = "Choose file",120 danger = false,121 onConfirm = onConfirm,122 onDismiss = onDismiss,123 )124}125126/** Asks for the password of an encrypted backup being opened; every open starts with an empty draft. */127@Composable128internal fun RestorePasswordDialog(onSubmit: (String) -> Unit, onDismiss: () -> Unit) {129 var password by remember { mutableStateOf("") }130 AlertDialog(131 onDismissRequest = onDismiss,132 title = { Text("Backup password") },133 text = {134 OutlinedTextField(135 value = password,136 onValueChange = { password = it },137 label = { Text("Password") },138 visualTransformation = PasswordVisualTransformation(),139 singleLine = true,140 )141 },142 confirmButton = {143 TextButton(onClick = { onSubmit(password) }) { Text(stringResource(Res.string.action_open)) }144 },145 dismissButton = {146 TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) }147 },148 )149}150