From 0f48563e291fdff2a7844d7b34e4787ecd8f99b4 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Apr 2020 16:00:08 -0400 Subject: [PATCH] Some restore code refactoring --- .../data/backup/BackupRestoreService.kt | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index 6e3406beb8..3fb80a1089 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -9,6 +9,7 @@ import android.os.IBinder import android.os.PowerManager import com.github.salomonbrys.kotson.fromJson import com.google.gson.JsonArray +import com.google.gson.JsonElement import com.google.gson.JsonParser import com.google.gson.stream.JsonReader import eu.kanade.tachiyomi.R @@ -221,45 +222,13 @@ class BackupRestoreService : Service() { errors.clear() // Restore categories - json.get(CATEGORIES)?.let { - backupManager.restoreCategories(it.asJsonArray) - restoreProgress += 1 - showRestoreProgress(restoreProgress, restoreAmount, getString(R.string.categories)) - } + restoreCategories(json.get(CATEGORIES)) mangasJson } .flatMap { Observable.from(it) } .concatMap { - val obj = it.asJsonObject - val manga = backupManager.parser.fromJson(obj.get(MANGA)) - val chapters = backupManager.parser.fromJson>( - obj.get(CHAPTERS) - ?: JsonArray() - ) - val categories = backupManager.parser.fromJson>( - obj.get(CATEGORIES) - ?: JsonArray() - ) - val history = backupManager.parser.fromJson>( - obj.get(HISTORY) - ?: JsonArray() - ) - val tracks = backupManager.parser.fromJson>( - obj.get(TRACK) - ?: JsonArray() - ) - - val observable = getMangaRestoreObservable(manga, chapters, categories, history, tracks) - if (observable != null) { - observable - } else { - errors.add(Date() to "${manga.title} - ${getString(R.string.source_not_found)}") - restoreProgress += 1 - val content = getString(R.string.dialog_restoring_source_not_found, manga.title.chop(15)) - showRestoreProgress(restoreProgress, restoreAmount, manga.title, content) - Observable.just(manga) - } + restoreManga(it) } .toList() .doOnNext { @@ -287,6 +256,46 @@ class BackupRestoreService : Service() { .onErrorReturn { emptyList() } } + private fun restoreCategories(categoriesJson: JsonElement) { + backupManager.restoreCategories(categoriesJson.asJsonArray) + restoreProgress += 1 + showRestoreProgress(restoreProgress, restoreAmount, getString(R.string.categories)) + } + + private fun restoreManga(mangaJson: JsonElement): Observable? { + val obj = mangaJson.asJsonObject + + val manga = backupManager.parser.fromJson(obj.get(MANGA)) + val chapters = backupManager.parser.fromJson>( + obj.get(CHAPTERS) + ?: JsonArray() + ) + val categories = backupManager.parser.fromJson>( + obj.get(CATEGORIES) + ?: JsonArray() + ) + val history = backupManager.parser.fromJson>( + obj.get(HISTORY) + ?: JsonArray() + ) + val tracks = backupManager.parser.fromJson>( + obj.get(TRACK) + ?: JsonArray() + ) + + val observable = getMangaRestoreObservable(manga, chapters, categories, history, tracks) + return if (observable != null) { + observable + } else { + errors.add(Date() to "${manga.title} - ${getString(R.string.source_not_found)}") + restoreProgress += 1 + val content = + getString(R.string.dialog_restoring_source_not_found, manga.title.chop(15)) + showRestoreProgress(restoreProgress, restoreAmount, manga.title, content) + Observable.just(manga) + } + } + /** * Write errors to error log */