Yokan / rebuild
Course 1 8624561

BrowserPersistenceWarning.kt

composeApp/src/wasmJsMain/kotlin/eu/friedcutlet/yokan/BrowserPersistenceWarning.kt

implementation69 lines
1package eu.friedcutlet.yokan23import androidx.compose.foundation.layout.Column4import androidx.compose.foundation.layout.fillMaxWidth5import androidx.compose.foundation.layout.padding6import androidx.compose.material3.MaterialTheme7import androidx.compose.material3.Surface8import androidx.compose.material3.Text9import androidx.compose.material3.TextButton10import androidx.compose.runtime.Composable11import androidx.compose.runtime.getValue12import androidx.compose.runtime.mutableStateOf13import androidx.compose.runtime.remember14import androidx.compose.runtime.rememberCoroutineScope15import androidx.compose.runtime.setValue16import androidx.compose.ui.Modifier17import androidx.compose.ui.unit.dp18import androidx.lifecycle.compose.collectAsStateWithLifecycle19import eu.friedcutlet.yokan.database.SnapshotYokanDatabase20import eu.friedcutlet.yokan.app.YokanRuntime21import eu.friedcutlet.yokan.services.backup.BackupOptions22import eu.friedcutlet.yokan.services.system.triggerBrowserDownload23import kotlinx.coroutines.CancellationException24import kotlinx.coroutines.launch25import org.jetbrains.compose.resources.stringResource26import yokan.composeapp.generated.resources.*272829private val browserDatabase: SnapshotYokanDatabase get() = YokanRuntime.database as SnapshotYokanDatabase3031@Composable32internal fun BrowserPersistenceWarning() {33    val failed by browserDatabase.saveFailed.collectAsStateWithLifecycle()34    if (!failed) return35    val scope = rememberCoroutineScope()36    var exporting by remember { mutableStateOf(false) }37    var exportFailed by remember { mutableStateOf(false) }38    Surface(color = MaterialTheme.colorScheme.errorContainer, modifier = Modifier.fillMaxWidth()) {39        Column(Modifier.padding(12.dp)) {40            Text(stringResource(Res.string.browser_save_failed))41            if (exportFailed) Text(stringResource(Res.string.browser_export_failed))42            if (exporting) Text(stringResource(Res.string.browser_exporting_backup))43            Column {44                TextButton(onClick = { browserDatabase.persist() }) {45                    Text(stringResource(Res.string.browser_retry_save))46                }47                TextButton(enabled = !exporting, onClick = {48                    exporting = true49                    exportFailed = false50                    scope.launch {51                        try {52                            val backup = YokanRuntime.services.backup53                            val settings = browserDatabase.settings()54                            val export = if (settings.backupEncryptionEnabled) {55                                backup.exportEncryptedBackup(BackupOptions(), requireNotNull(settings.backupEncryptionPassword))56                            } else backup.exportBackup(BackupOptions())57                            exportFailed = !triggerBrowserDownload(export.bytes, export.fileName, export.contentType)58                        } catch (error: Throwable) {59                            if (error is CancellationException) throw error60                            exportFailed = true61                        } finally {62                            exporting = false63                        }64                    }65                }) { Text(stringResource(Res.string.browser_export_backup)) }66            }67        }68    }69}70

Search chapters, reference and source filenames.