diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreValidator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreValidator.kt index f5f924ac9b..81bff9382d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreValidator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreValidator.kt @@ -7,16 +7,22 @@ import com.google.gson.JsonParser import com.google.gson.stream.JsonReader import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.models.Backup +import eu.kanade.tachiyomi.data.track.TrackManager +import eu.kanade.tachiyomi.source.SourceManager +import uy.kohesive.injekt.injectLazy object BackupRestoreValidator { + private val sourceManager: SourceManager by injectLazy() + private val trackManager: TrackManager by injectLazy() + /** * Checks for critical backup file data. * * @throws Exception if version or manga cannot be found. - * @return List of required sources. + * @return List of missing sources or missing trackers. */ - fun validate(context: Context, uri: Uri): Map { + fun validate(context: Context, uri: Uri): Results { val reader = JsonReader(context.contentResolver.openInputStream(uri)!!.bufferedReader()) val json = JsonParser.parseReader(reader).asJsonObject @@ -26,11 +32,29 @@ object BackupRestoreValidator { throw Exception(context.getString(R.string.invalid_backup_file_missing_data)) } - if (mangasJson.asJsonArray.size() == 0) { + val mangas = mangasJson.asJsonArray + if (mangas.size() == 0) { throw Exception(context.getString(R.string.invalid_backup_file_missing_manga)) } - return getSourceMapping(json) + val sources = getSourceMapping(json) + val missingSources = sources + .filter { sourceManager.get(it.key) == null } + .values + .sorted() + + val trackers = mangas + .filter { it.asJsonObject.has("track") } + .flatMap { it.asJsonObject["track"].asJsonArray } + .map { it.asJsonObject["s"].asInt } + .distinct() + val missingTrackers = trackers + .mapNotNull { trackManager.getService(it) } + .filter { !it.isLogged } + .map { it.name } + .sorted() + + return Results(missingSources, missingTrackers) } fun getSourceMapping(json: JsonObject): Map { @@ -43,4 +67,6 @@ object BackupRestoreValidator { } .toMap() } + + data class Results(val missingSources: List, val missingTrackers: List) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 6e13a08377..f030d36803 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -21,7 +21,6 @@ import eu.kanade.tachiyomi.data.backup.BackupRestoreValidator import eu.kanade.tachiyomi.data.backup.models.Backup import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.asImmediateFlow -import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.util.preference.defaultValue @@ -37,8 +36,6 @@ import eu.kanade.tachiyomi.util.system.getFilePicker import eu.kanade.tachiyomi.util.system.toast import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get class SettingsBackupController : SettingsController() { @@ -258,16 +255,12 @@ class SettingsBackupController : SettingsController() { return try { var message = activity.getString(R.string.backup_restore_content) - val sources = BackupRestoreValidator.validate(activity, uri) - if (sources.isNotEmpty()) { - val sourceManager = Injekt.get() - val missingSources = sources - .filter { sourceManager.get(it.key) == null } - .values - .sorted() - if (missingSources.isNotEmpty()) { - message += "\n\n${activity.getString(R.string.backup_restore_missing_sources)}\n${missingSources.joinToString("\n") { "- $it" }}" - } + val results = BackupRestoreValidator.validate(activity, uri) + if (results.missingSources.isNotEmpty()) { + message += "\n\n${activity.getString(R.string.backup_restore_missing_sources)}\n${results.missingSources.joinToString("\n") { "- $it" }}" + } + if (results.missingTrackers.isNotEmpty()) { + message += "\n\n${activity.getString(R.string.backup_restore_missing_trackers)}\n${results.missingTrackers.joinToString("\n") { "- $it" }}" } MaterialDialog(activity) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d1fd1f279f..173524ae5e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -349,6 +349,7 @@ File is missing data. Backup does not contain any manga. Missing sources: + Trackers not logged into: Restore uses sources to fetch data, carrier costs may apply.\n\nMake sure you have installed all necessary extensions and are logged in to sources and tracking services before restoring. Restore completed %02d min, %02d sec