refactor(backup/restore): Return try catch

This commit is contained in:
Ahmad Ansori Palembani 2024-06-27 14:35:15 +07:00
parent 81a034db1a
commit b2478844d7
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -26,7 +26,6 @@ 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()
@ -41,27 +40,28 @@ class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : C
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
if (isRunning(context)) return Result.failure() if (isRunning(context)) return Result.failure()
tryToSetForeground()
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()
withIOContext { tryToSetForeground()
return withIOContext {
try { try {
if (!restorer.restoreBackup(uri)) { if (!BackupRestorer(context, notifier).restoreBackup(uri))
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled)) notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
} Result.success()
} catch (exception: Exception) { } catch (e: Exception) {
if (exception is CancellationException) { if (e is CancellationException) {
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled)) notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
Result.success()
} else { } else {
restorer.writeErrorLog() restorer.writeErrorLog()
notifier.showRestoreError(exception.message) notifier.showRestoreError(e.message)
Result.failure()
} }
} }
} }
return Result.success()
} }
companion object { companion object {