mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(backup/restore): Simplify restorer
This commit is contained in:
parent
b2478844d7
commit
7f872e64f9
2 changed files with 11 additions and 18 deletions
|
@ -26,6 +26,7 @@ import yokai.util.lang.getString
|
|||
class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
|
||||
|
||||
private val notifier = BackupNotifier(context.localeContext)
|
||||
private val restorer = BackupRestorer(context, notifier)
|
||||
|
||||
override suspend fun getForegroundInfo(): ForegroundInfo {
|
||||
val notification = notifier.showRestoreProgress(progress = -1).build()
|
||||
|
@ -38,18 +39,14 @@ class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : C
|
|||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
if (isRunning(context)) return Result.failure()
|
||||
|
||||
val uriPath = inputData.getString(BackupConst.EXTRA_URI) ?: return Result.failure()
|
||||
|
||||
val uri = Uri.parse(uriPath) ?: return Result.failure()
|
||||
|
||||
tryToSetForeground()
|
||||
|
||||
return withIOContext {
|
||||
try {
|
||||
if (!BackupRestorer(context, notifier).restoreBackup(uri))
|
||||
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
|
||||
restorer.restore(uri)
|
||||
Result.success()
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import eu.kanade.tachiyomi.data.backup.restore.restorers.PreferenceBackupRestore
|
|||
import eu.kanade.tachiyomi.util.BackupUtil
|
||||
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import yokai.i18n.MR
|
||||
import yokai.util.lang.getString
|
||||
import java.io.File
|
||||
|
@ -34,14 +34,12 @@ class BackupRestorer(
|
|||
|
||||
private val errors = mutableListOf<Pair<Date, String>>()
|
||||
|
||||
suspend fun restoreBackup(uri: Uri): Boolean {
|
||||
suspend fun restore(uri: Uri) {
|
||||
val startTime = System.currentTimeMillis()
|
||||
restoreProgress = 0
|
||||
errors.clear()
|
||||
|
||||
if (!performRestore(uri)) {
|
||||
return false
|
||||
}
|
||||
performRestore(uri)
|
||||
|
||||
val endTime = System.currentTimeMillis()
|
||||
val time = endTime - startTime
|
||||
|
@ -49,10 +47,9 @@ class BackupRestorer(
|
|||
val logFile = writeErrorLog()
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
sourceMapping = backupMaps.associate { it.sourceId to it.name }
|
||||
|
||||
return coroutineScope {
|
||||
coroutineScope {
|
||||
// Restore categories
|
||||
if (backup.backupCategories.isNotEmpty()) {
|
||||
ensureActive()
|
||||
categoriesBackupRestorer.restoreCategories(backup.backupCategories) {
|
||||
restoreProgress += 1
|
||||
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.categories))
|
||||
}
|
||||
}
|
||||
|
||||
ensureActive()
|
||||
preferenceBackupRestorer.restoreAppPreferences(backup.backupPreferences) {
|
||||
restoreProgress += 1
|
||||
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.app_settings))
|
||||
}
|
||||
ensureActive()
|
||||
preferenceBackupRestorer.restoreSourcePreferences(backup.backupSourcePreferences) {
|
||||
restoreProgress += 1
|
||||
showRestoreProgress(restoreProgress, restoreAmount, context.getString(MR.strings.source_settings))
|
||||
|
@ -81,10 +81,7 @@ class BackupRestorer(
|
|||
|
||||
// Restore individual manga
|
||||
backup.backupManga.forEach {
|
||||
if (!isActive) {
|
||||
return@coroutineScope false
|
||||
}
|
||||
|
||||
ensureActive()
|
||||
mangaBackupRestorer.restoreManga(
|
||||
it,
|
||||
backup.backupCategories,
|
||||
|
@ -98,7 +95,6 @@ class BackupRestorer(
|
|||
},
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
// TODO: optionally trigger online library + tracker update
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue