refactor(backup/restore): Simplify restorer

This commit is contained in:
Ahmad Ansori Palembani 2024-06-27 14:39:56 +07:00
parent b2478844d7
commit 7f872e64f9
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 11 additions and 18 deletions

View file

@ -26,6 +26,7 @@ import yokai.util.lang.getString
class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) { class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
private val notifier = BackupNotifier(context.localeContext) private val notifier = BackupNotifier(context.localeContext)
private val restorer = BackupRestorer(context, notifier)
override suspend fun getForegroundInfo(): ForegroundInfo { override suspend fun getForegroundInfo(): ForegroundInfo {
val notification = notifier.showRestoreProgress(progress = -1).build() val notification = notifier.showRestoreProgress(progress = -1).build()
@ -38,18 +39,14 @@ class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : C
} }
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
if (isRunning(context)) return Result.failure()
val uriPath = inputData.getString(BackupConst.EXTRA_URI) ?: return Result.failure() val uriPath = inputData.getString(BackupConst.EXTRA_URI) ?: return Result.failure()
val uri = Uri.parse(uriPath) ?: return Result.failure() val uri = Uri.parse(uriPath) ?: return Result.failure()
tryToSetForeground() tryToSetForeground()
return withIOContext { return withIOContext {
try { try {
if (!BackupRestorer(context, notifier).restoreBackup(uri)) restorer.restore(uri)
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
Result.success() Result.success()
} catch (e: Exception) { } catch (e: Exception) {
if (e is CancellationException) { if (e is CancellationException) {

View file

@ -10,7 +10,7 @@ import eu.kanade.tachiyomi.data.backup.restore.restorers.PreferenceBackupRestore
import eu.kanade.tachiyomi.util.BackupUtil import eu.kanade.tachiyomi.util.BackupUtil
import eu.kanade.tachiyomi.util.system.createFileInCacheDir import eu.kanade.tachiyomi.util.system.createFileInCacheDir
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.isActive import kotlinx.coroutines.ensureActive
import yokai.i18n.MR import yokai.i18n.MR
import yokai.util.lang.getString import yokai.util.lang.getString
import java.io.File import java.io.File
@ -34,14 +34,12 @@ class BackupRestorer(
private val errors = mutableListOf<Pair<Date, String>>() private val errors = mutableListOf<Pair<Date, String>>()
suspend fun restoreBackup(uri: Uri): Boolean { suspend fun restore(uri: Uri) {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
restoreProgress = 0 restoreProgress = 0
errors.clear() errors.clear()
if (!performRestore(uri)) { performRestore(uri)
return false
}
val endTime = System.currentTimeMillis() val endTime = System.currentTimeMillis()
val time = endTime - startTime val time = endTime - startTime
@ -49,10 +47,9 @@ class BackupRestorer(
val logFile = writeErrorLog() val logFile = writeErrorLog()
notifier.showRestoreComplete(time, errors.size, logFile.parent, logFile.name) notifier.showRestoreComplete(time, errors.size, logFile.parent, logFile.name)
return true
} }
private suspend fun performRestore(uri: Uri): Boolean { private suspend fun performRestore(uri: Uri) {
val backup = BackupUtil.decodeBackup(context, uri) val backup = BackupUtil.decodeBackup(context, uri)
restoreAmount = backup.backupManga.size + 3 // +3 for categories, app prefs, source prefs restoreAmount = backup.backupManga.size + 3 // +3 for categories, app prefs, source prefs
@ -61,19 +58,22 @@ class BackupRestorer(
val backupMaps = backup.backupBrokenSources.map { BackupSource(it.name, it.sourceId) } + backup.backupSources val backupMaps = backup.backupBrokenSources.map { BackupSource(it.name, it.sourceId) } + backup.backupSources
sourceMapping = backupMaps.associate { it.sourceId to it.name } sourceMapping = backupMaps.associate { it.sourceId to it.name }
return coroutineScope { coroutineScope {
// Restore categories // Restore categories
if (backup.backupCategories.isNotEmpty()) { if (backup.backupCategories.isNotEmpty()) {
ensureActive()
categoriesBackupRestorer.restoreCategories(backup.backupCategories) { categoriesBackupRestorer.restoreCategories(backup.backupCategories) {
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.categories)) showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.categories))
} }
} }
ensureActive()
preferenceBackupRestorer.restoreAppPreferences(backup.backupPreferences) { preferenceBackupRestorer.restoreAppPreferences(backup.backupPreferences) {
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.app_settings)) showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.app_settings))
} }
ensureActive()
preferenceBackupRestorer.restoreSourcePreferences(backup.backupSourcePreferences) { preferenceBackupRestorer.restoreSourcePreferences(backup.backupSourcePreferences) {
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.source_settings)) showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.source_settings))
@ -81,10 +81,7 @@ class BackupRestorer(
// Restore individual manga // Restore individual manga
backup.backupManga.forEach { backup.backupManga.forEach {
if (!isActive) { ensureActive()
return@coroutineScope false
}
mangaBackupRestorer.restoreManga( mangaBackupRestorer.restoreManga(
it, it,
backup.backupCategories, backup.backupCategories,
@ -98,7 +95,6 @@ class BackupRestorer(
}, },
) )
} }
true
} }
// TODO: optionally trigger online library + tracker update // TODO: optionally trigger online library + tracker update
} }