From 3513b1f9c1f6b7f6fcadcb8672375db4d28a11e0 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 1 Jul 2022 12:51:19 -0400 Subject: [PATCH] Use interactors for backup data calls --- .../backup/interactor/ExportBackupFile.kt | 28 +++++++++++++++++ .../backup/interactor/ImportBackupFile.kt | 29 +++++++++++++++++ .../backup/interactor/ValidateBackupFile.kt | 31 +++++++++++++++++++ .../jui/ui/settings/SettingsBackupScreen.kt | 23 ++++++++------ 4 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ExportBackupFile.kt create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ImportBackupFile.kt create mode 100644 domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ValidateBackupFile.kt diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ExportBackupFile.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ExportBackupFile.kt new file mode 100644 index 00000000..e98085c8 --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ExportBackupFile.kt @@ -0,0 +1,28 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.backup.interactor + +import ca.gosyer.jui.domain.backup.service.BackupRepository +import io.ktor.client.request.HttpRequestBuilder +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import org.lighthousegames.logging.logging + +class ExportBackupFile @Inject constructor(private val backupRepository: BackupRepository) { + + suspend fun await(block: HttpRequestBuilder.() -> Unit = {}) = asFlow(block) + .catch { log.warn(it) { "Failed to export backup" } } + .singleOrNull() + + fun asFlow(block: HttpRequestBuilder.() -> Unit = {}) = + backupRepository.exportBackupFile(block) + + companion object { + private val log = logging() + } +} diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ImportBackupFile.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ImportBackupFile.kt new file mode 100644 index 00000000..316fe4bd --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ImportBackupFile.kt @@ -0,0 +1,29 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.backup.interactor + +import ca.gosyer.jui.domain.backup.service.BackupRepository +import io.ktor.client.request.HttpRequestBuilder +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import okio.Path +import org.lighthousegames.logging.logging + +class ImportBackupFile @Inject constructor(private val backupRepository: BackupRepository) { + + suspend fun await(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = asFlow(file, block) + .catch { log.warn(it) { "Failed to import backup ${file.name}" } } + .singleOrNull() + + fun asFlow(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = + backupRepository.importBackupFile(file, block) + + companion object { + private val log = logging() + } +} diff --git a/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ValidateBackupFile.kt b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ValidateBackupFile.kt new file mode 100644 index 00000000..c3f85ec7 --- /dev/null +++ b/domain/src/commonMain/kotlin/ca/gosyer/jui/domain/backup/interactor/ValidateBackupFile.kt @@ -0,0 +1,31 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.backup.interactor + +import ca.gosyer.jui.domain.backup.service.BackupRepository +import ca.gosyer.jui.domain.category.service.CategoryRepository +import io.ktor.client.request.HttpRequestBuilder +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.singleOrNull +import me.tatarka.inject.annotations.Inject +import okio.Path +import org.lighthousegames.logging.logging + +class ValidateBackupFile @Inject constructor(private val backupRepository: BackupRepository) { + + suspend fun await(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = asFlow(file, block) + .catch { log.warn(it) { "Failed to validate backup ${file.name}" } } + .singleOrNull() + + fun asFlow(file: Path, block: HttpRequestBuilder.() -> Unit = {}) = + backupRepository.validateBackupFile(file, block) + + companion object { + private val log = logging() + } +} diff --git a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/settings/SettingsBackupScreen.kt b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/settings/SettingsBackupScreen.kt index 614734ad..4f11c224 100644 --- a/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/settings/SettingsBackupScreen.kt +++ b/presentation/src/commonMain/kotlin/ca/gosyer/jui/ui/settings/SettingsBackupScreen.kt @@ -35,7 +35,9 @@ import ca.gosyer.jui.core.io.copyTo import ca.gosyer.jui.core.io.saveTo import ca.gosyer.jui.core.lang.IO import ca.gosyer.jui.core.lang.throwIfCancellation -import ca.gosyer.jui.data.backup.BackupRepositoryImpl +import ca.gosyer.jui.domain.backup.interactor.ExportBackupFile +import ca.gosyer.jui.domain.backup.interactor.ImportBackupFile +import ca.gosyer.jui.domain.backup.interactor.ValidateBackupFile import ca.gosyer.jui.i18n.MR import ca.gosyer.jui.ui.base.dialog.getMaterialDialogProperties import ca.gosyer.jui.ui.base.file.rememberFileChooser @@ -108,7 +110,9 @@ class SettingsBackupScreen : Screen { } class SettingsBackupViewModel @Inject constructor( - private val backupHandler: BackupRepositoryImpl, + private val validateBackupFile: ValidateBackupFile, + private val importBackupFile: ImportBackupFile, + private val exportBackupFile: ExportBackupFile, contextWrapper: ContextWrapper ) : ViewModel(contextWrapper) { private val _restoring = MutableStateFlow(false) @@ -144,7 +148,7 @@ class SettingsBackupViewModel @Inject constructor( } file ?: return@launch - backupHandler.validateBackupFile(file) + validateBackupFile.asFlow(file) .onEach { (missingSources) -> if (missingSources.isEmpty()) { restoreBackup(file) @@ -165,11 +169,12 @@ class SettingsBackupViewModel @Inject constructor( _restoreStatus.value = Status.Nothing _restoringProgress.value = null _restoring.value = true - backupHandler.importBackupFile(file) { - onUpload { bytesSentTotal, contentLength -> - _restoringProgress.value = (bytesSentTotal.toFloat() / contentLength).coerceAtMost(1.0F) + importBackupFile + .asFlow(file) { + onUpload { bytesSentTotal, contentLength -> + _restoringProgress.value = (bytesSentTotal.toFloat() / contentLength).coerceAtMost(1.0F) + } } - } .onEach { _restoreStatus.value = Status.Success } @@ -194,8 +199,8 @@ class SettingsBackupViewModel @Inject constructor( _creatingStatus.value = Status.Nothing _creatingProgress.value = null _creating.value = true - backupHandler - .exportBackupFile { + exportBackupFile + .asFlow { onDownload { bytesSentTotal, contentLength -> _creatingProgress.value = (bytesSentTotal.toFloat() / contentLength).coerceAtMost(0.99F) }