Yokan / rebuild
Course 1 8624561

CreateBackupViewModel.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/data/CreateBackupViewModel.kt

implementation62 lines
1package eu.friedcutlet.yokan.ui.screens.settings.data23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.services.backup.BackupFormat6import eu.friedcutlet.yokan.services.backup.BackupResult7import eu.friedcutlet.yokan.services.backup.BackupService8import eu.friedcutlet.yokan.services.catchingNonCancellation9import eu.friedcutlet.yokan.services.files.LocalFileService10import eu.friedcutlet.yokan.services.files.exportFile11import kotlinx.coroutines.flow.MutableStateFlow12import kotlinx.coroutines.flow.StateFlow13import kotlinx.coroutines.flow.asStateFlow14import kotlinx.coroutines.flow.update15import kotlinx.coroutines.launch1617internal data class CreateBackupState(18    val status: BackupResult? = null,19    val error: String? = null,20    val running: Boolean = false,21)2223/** Exports a backup and hands it to the platform save UI. */24internal class CreateBackupViewModel(25    private val backup: BackupService,26    private val files: LocalFileService,27) : ViewModel() {28    private val _state = MutableStateFlow(CreateBackupState())29    val state: StateFlow<CreateBackupState> = _state.asStateFlow()3031    /** Encrypts the export when [password] is set; a cancelled save leaves [CreateBackupState.status] null. */32    fun export(autoBackupLocation: String?, ids: List<Int>, compressionLevel: Int, format: BackupFormat, password: String?) {33        _state.update { it.copy(running = true, error = null) }34        viewModelScope.launch {35            catchingNonCancellation {36                val options = buildBackupOptionsFromIds(ids, compressionLevel, format)37                val export = if (password == null) {38                    backup.exportBackup(options)39                } else {40                    backup.exportEncryptedBackup(options, password)41                }42                val path = exportFile(43                    bytes = export.bytes,44                    suggestedName = export.fileName,45                    mimeType = export.contentType,46                    initial = autoBackupLocation47                        ?.takeIf { it.isNotBlank() }48                        ?: files.backupRoot,49                )50                path?.let {51                    BackupResult(52                        fileName = export.fileName,53                        bytesWritten = export.bytes.size.toLong(),54                        itemCount = export.itemCount,55                        path = it,56                    )57                }58            }.onSuccess { result -> _state.update { it.copy(status = result, running = false) } }59                .onFailure { error -> _state.update { it.copy(error = error.message ?: "Backup failed", running = false) } }60        }61    }62}63

Search chapters, reference and source filenames.