mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Use interactors for backup data calls
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user