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
This commit is contained in:
Jay 2020-05-06 23:07:55 -04:00
parent 49d8563658
commit 3529681f90
2 changed files with 65 additions and 10 deletions

View File

@ -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<String, String> {
val extensionsList = parser.fromJson<List<String>>(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 * Restore the categories from Json
* *

View File

@ -18,6 +18,7 @@ import com.google.gson.stream.JsonReader
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.models.Backup.CATEGORIES 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.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.HISTORY
import eu.kanade.tachiyomi.data.backup.models.Backup.MANGA import eu.kanade.tachiyomi.data.backup.models.Backup.MANGA
import eu.kanade.tachiyomi.data.backup.models.Backup.MANGAS import eu.kanade.tachiyomi.data.backup.models.Backup.MANGAS
@ -89,7 +90,9 @@ class BackupRestoreService : Service() {
/** /**
* List containing missing sources * List containing missing sources
*/ */
private val sourcesMissing = mutableListOf<Long>() private val sourcesMissing = mutableListOf<String>()
var extensionsMap: Map<String, String> = emptyMap()
/** /**
* List containing missing sources * List containing missing sources
@ -197,6 +200,7 @@ class BackupRestoreService : Service() {
cancelled = 0 cancelled = 0
// Restore categories // Restore categories
restoreCategories(json, backupManager) restoreCategories(json, backupManager)
extensionsMap = getExtensionsList(json, backupManager)
mangasJson.forEach { mangasJson.forEach {
restoreManga(it.asJsonObject, backupManager) restoreManga(it.asJsonObject, backupManager)
@ -213,8 +217,19 @@ class BackupRestoreService : Service() {
showResultNotification(logFile.parent, logFile.name) 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<String,
String> {
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) { private fun restoreCategories(json: JsonObject, backupManager: BackupManager) {
val element = json.get(CATEGORIES) val element = json.get(CATEGORIES)
@ -275,11 +290,18 @@ class BackupRestoreService : Service() {
if (e is RuntimeException) { if (e is RuntimeException) {
val cause = e.cause val cause = e.cause
if (cause is SourceNotFoundException) { if (cause is SourceNotFoundException) {
sourcesMissing.add(cause.id) val sourceName = extensionsMap[cause.id.toString()] ?: cause.id.toString()
} else if (e.message?.contains("licensed", true) == true) { sourcesMissing.add(
lincensedManga++ 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 return
} }
errors.add("${manga.title} - ${e.message}") errors.add("${manga.title} - ${e.message}")
@ -376,9 +398,27 @@ class BackupRestoreService : Service() {
val content = mutableListOf(getString(R.string.restore_completed_content, restoreProgress val content = mutableListOf(getString(R.string.restore_completed_content, restoreProgress
.toString(), errors.size.toString())) .toString(), errors.size.toString()))
val sourceMissingCount = sourcesMissing.distinct().size val sourceMissingCount = sourcesMissing.distinct().size
if (sourceMissingCount > 0) if (sourceMissingCount > 0) {
content.add(resources.getQuantityString(R.plurals.sources_missing, val sources = sourcesMissing.distinct().filter { it.toLongOrNull() == null }
sourceMissingCount, sourceMissingCount)) 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) if (lincensedManga > 0)
content.add(resources.getQuantityString(R.plurals.licensed_manga, lincensedManga, content.add(resources.getQuantityString(R.plurals.licensed_manga, lincensedManga,
lincensedManga)) lincensedManga))