ProfileBackupScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/profiles/ProfileBackupScreen.kt
1package eu.friedcutlet.yokan.ui.screens.settings.profiles23import androidx.compose.foundation.layout.*4import androidx.compose.foundation.lazy.items5import androidx.compose.material3.*6import androidx.compose.runtime.*7import androidx.compose.ui.Modifier8import androidx.compose.ui.text.input.PasswordVisualTransformation9import androidx.compose.ui.unit.dp10import eu.friedcutlet.yokan.domain.ProfileId11import eu.friedcutlet.yokan.services.backup.*12import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold13import org.jetbrains.compose.resources.stringResource14import yokan.composeapp.generated.resources.*1516internal data class ProfileBackupControls(17 val state: ProfileBackupsState,18 val select: (ProfileId) -> Unit,19 val password: (String) -> Unit,20 val format: (BackupFormat) -> Unit,21 val mode: (RestoreMode) -> Unit,22 val export: () -> Unit,23 val chooseFile: () -> Unit,24 val unlock: () -> Unit,25 val apply: () -> Unit,26 val snapshots: () -> Unit,27 val rollback: (ProfileId, SafetySnapshot) -> Unit,28)29internal val LocalProfileBackupControls = staticCompositionLocalOf<ProfileBackupControls> { error("Backup controls unavailable") }3031@Composable32internal fun ProfileBackupScreen(export: Boolean, onBack: () -> Unit) {33 val controls = LocalProfileBackupControls.current34 val state = controls.state35 val targetName = state.profiles.firstOrNull { it.id == state.target }?.name.orEmpty()36 SettingsScaffold(stringResource(if (export) Res.string.profiles_backup_export else Res.string.profiles_backup_restore), onBack) {37 if (state.profiles.size > 1) item(key = "backup-profile-selector") {38 Column(Modifier.padding(16.dp)) {39 Text(stringResource(if (export) Res.string.profiles_backup_source else Res.string.profiles_backup_target), style = MaterialTheme.typography.titleMedium)40 state.profiles.forEach { profile -> key(profile.id.value) {41 Row { RadioButton(profile.id == state.target, onClick = { controls.select(profile.id) }, enabled = !state.busy); TextButton(onClick = { controls.select(profile.id) }, enabled = !state.busy) { Text(profile.name) } }42 } }43 }44 }45 item(key = "backup-guidance") { Text(stringResource(Res.string.profiles_backup_guidance), modifier = Modifier.padding(16.dp)) }46 item(key = "backup-password") { OutlinedTextField(state.password, controls.password, label = { Text(stringResource(Res.string.profiles_backup_password)) }, visualTransformation = PasswordVisualTransformation(), singleLine = true, enabled = !state.busy, modifier = Modifier.fillMaxWidth().padding(16.dp)) }47 if (export) {48 item(key = "backup-formats") { Row(Modifier.padding(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {49 BackupFormat.entries.forEach { format -> FilterChip(state.format == format, onClick = { controls.format(format) }, label = { Text(format.name) }, enabled = !state.busy && (format == BackupFormat.Yokan || state.password.isEmpty())) }50 } }51 item(key = "backup-export") { Button(onClick = controls.export, enabled = state.available && !state.busy && (state.format == BackupFormat.Yokan || state.password.isEmpty()), modifier = Modifier.padding(16.dp)) { Text(stringResource(Res.string.profiles_backup_export_named, targetName)) } }52 } else {53 item(key = "backup-open") { Row(Modifier.padding(16.dp)) {54 TextButton(onClick = controls.chooseFile, enabled = state.available && !state.busy) { Text(stringResource(Res.string.profiles_backup_choose)) }55 TextButton(onClick = controls.unlock, enabled = state.available && !state.busy && state.password.isNotEmpty()) { Text(stringResource(Res.string.profiles_backup_unlock)) }56 } }57 state.plan?.let { target ->58 item(key = "backup-preview") { Column(Modifier.padding(16.dp)) {59 Text(stringResource(Res.string.profiles_backup_preview, targetName, target.plan.preview.itemCount), style = MaterialTheme.typography.titleMedium)60 target.plan.preview.warnings.forEach { Text(it, style = MaterialTheme.typography.bodySmall) }61 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { RestoreMode.entries.forEach { mode -> FilterChip(state.mode == mode, onClick = { controls.mode(mode) }, label = { Text(mode.name) }, enabled = !state.busy) } }62 Button(onClick = controls.apply, enabled = state.available && !state.busy) { Text(stringResource(Res.string.profiles_backup_apply)) }63 } }64 }65 item(key = "backup-snapshots-refresh") { TextButton(onClick = controls.snapshots, enabled = state.available && !state.busy) { Text(stringResource(Res.string.profiles_backup_snapshots)) } }66 items(state.snapshots, key = { it.backupPath }, contentType = { "snapshot" }) { snapshot ->67 Column(Modifier.padding(16.dp)) {68 Text(snapshot.description)69 TextButton(onClick = { controls.rollback(state.target, snapshot) }, enabled = !state.busy) { Text(stringResource(Res.string.profiles_backup_rollback)) }70 }71 }72 }73 state.error?.let { error -> item(key = "backup-error") { Text(error, color = MaterialTheme.colorScheme.error, modifier = Modifier.padding(16.dp)) } }74 state.message?.let { message -> item(key = "backup-result") { Text(message, modifier = Modifier.padding(16.dp)) } }75 if (state.busy) item(key = "backup-busy") { LinearProgressIndicator(Modifier.fillMaxWidth()) }76 }77}78