From 3529681f90c5488637bd2e9163f56f5a09b711c1 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 6 May 2020 23:07:55 -0400 Subject: [PATCH] If backup is created with extensions mapped, restores will now list the source name instead of numbers Restore complete notification will also list the first 5 sources missing --- .../tachiyomi/data/backup/BackupManager.kt | 15 +++++ .../data/backup/BackupRestoreService.kt | 60 +++++++++++++++---- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt index 43aad96b4b..461abbb5d7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt @@ -326,6 +326,21 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) { } } + /** + * Restore extensions list from json and map them + */ + internal fun getExtensionsMap(extensions: JsonArray): Map { + val extensionsList = parser.fromJson>(extensions) + return extensionsList.mapNotNull { + val split = it.split(":") + if (split.size == 2) { + split.first() to split.last() + } else { + null + } + }.toMap() + } + /** * Restore the categories from Json * 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 84603eee6f..ee8ad9634a 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 @@ -18,6 +18,7 @@ import com.google.gson.stream.JsonReader import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.models.Backup.CATEGORIES import eu.kanade.tachiyomi.data.backup.models.Backup.CHAPTERS +import eu.kanade.tachiyomi.data.backup.models.Backup.EXTENSIONS import eu.kanade.tachiyomi.data.backup.models.Backup.HISTORY import eu.kanade.tachiyomi.data.backup.models.Backup.MANGA import eu.kanade.tachiyomi.data.backup.models.Backup.MANGAS @@ -89,7 +90,9 @@ class BackupRestoreService : Service() { /** * List containing missing sources */ - private val sourcesMissing = mutableListOf() + private val sourcesMissing = mutableListOf() + + var extensionsMap: Map = emptyMap() /** * List containing missing sources @@ -197,6 +200,7 @@ class BackupRestoreService : Service() { cancelled = 0 // Restore categories restoreCategories(json, backupManager) + extensionsMap = getExtensionsList(json, backupManager) mangasJson.forEach { restoreManga(it.asJsonObject, backupManager) @@ -213,8 +217,19 @@ class BackupRestoreService : Service() { showResultNotification(logFile.parent, logFile.name) } - /**Restore categories if they were backed up - * + /** + * Restore extension names if they were backed up + */ + private fun getExtensionsList(json: JsonObject, backupManager: BackupManager): Map { + json.get(EXTENSIONS)?.let { element -> + return backupManager.getExtensionsMap(element.asJsonArray) + } + return emptyMap() + } + + /** + * Restore categories if they were backed up */ private fun restoreCategories(json: JsonObject, backupManager: BackupManager) { val element = json.get(CATEGORIES) @@ -275,11 +290,18 @@ class BackupRestoreService : Service() { if (e is RuntimeException) { val cause = e.cause if (cause is SourceNotFoundException) { - sourcesMissing.add(cause.id) - } else if (e.message?.contains("licensed", true) == true) { - lincensedManga++ + val sourceName = extensionsMap[cause.id.toString()] ?: cause.id.toString() + sourcesMissing.add( + extensionsMap[cause.id.toString()] ?: cause.id.toString() + ) + val errorMessage = getString(R.string.source_not_installed_, sourceName) + errors.add("${manga.title} - $errorMessage") + } else { + if (e.message?.contains("licensed", true) == true) { + lincensedManga++ + } + errors.add("${manga.title} - ${cause?.message ?: e.message}") } - errors.add("${manga.title} - ${cause?.message ?: e.message}") return } errors.add("${manga.title} - ${e.message}") @@ -376,9 +398,27 @@ class BackupRestoreService : Service() { val content = mutableListOf(getString(R.string.restore_completed_content, restoreProgress .toString(), errors.size.toString())) val sourceMissingCount = sourcesMissing.distinct().size - if (sourceMissingCount > 0) - content.add(resources.getQuantityString(R.plurals.sources_missing, - sourceMissingCount, sourceMissingCount)) + if (sourceMissingCount > 0) { + val sources = sourcesMissing.distinct().filter { it.toLongOrNull() == null } + val missingSourcesString = if (sources.size > 5) { + sources.take(5).joinToString(", ") + "..." + } else { + sources.joinToString(", ") + } + if (sources.isEmpty()) { + content.add( + resources.getQuantityString( + R.plurals.sources_missing, sourceMissingCount, sourceMissingCount + ) + ) + } else { + content.add( + resources.getQuantityString( + R.plurals.sources_missing, sourceMissingCount, sourceMissingCount + ) + ": " + missingSourcesString + ) + } + } if (lincensedManga > 0) content.add(resources.getQuantityString(R.plurals.licensed_manga, lincensedManga, lincensedManga))