From b418169c20d7fe1b615c2af9a9850b9c8d2d1320 Mon Sep 17 00:00:00 2001 From: len Date: Sun, 6 Nov 2016 13:31:01 +0100 Subject: [PATCH] Exclude backup empty fields --- .../tachiyomi/data/backup/BackupManager.kt | 6 +++++- .../data/backup/serializer/BooleanSerializer.kt | 16 ++++++++++++++++ .../data/backup/serializer/IdExclusion.kt | 16 ++++++++-------- .../data/backup/serializer/LongSerializer.kt | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/BooleanSerializer.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/LongSerializer.kt 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 b775f595b4..3676144874 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 @@ -3,8 +3,10 @@ package eu.kanade.tachiyomi.data.backup import com.github.salomonbrys.kotson.fromJson import com.google.gson.* import com.google.gson.stream.JsonReader +import eu.kanade.tachiyomi.data.backup.serializer.BooleanSerializer import eu.kanade.tachiyomi.data.backup.serializer.IdExclusion import eu.kanade.tachiyomi.data.backup.serializer.IntegerSerializer +import eu.kanade.tachiyomi.data.backup.serializer.LongSerializer import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.* import java.io.* @@ -42,7 +44,9 @@ class BackupManager(private val db: DatabaseHelper) { @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") private val gson = GsonBuilder() - .registerTypeAdapter(Integer::class.java, IntegerSerializer()) + .registerTypeAdapter(java.lang.Integer::class.java, IntegerSerializer()) + .registerTypeAdapter(java.lang.Boolean::class.java, BooleanSerializer()) + .registerTypeAdapter(java.lang.Long::class.java, LongSerializer()) .setExclusionStrategies(IdExclusion()) .create() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/BooleanSerializer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/BooleanSerializer.kt new file mode 100644 index 0000000000..14d5a4236f --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/BooleanSerializer.kt @@ -0,0 +1,16 @@ +package eu.kanade.tachiyomi.data.backup.serializer + +import com.google.gson.JsonElement +import com.google.gson.JsonPrimitive +import com.google.gson.JsonSerializationContext +import com.google.gson.JsonSerializer +import java.lang.reflect.Type + +class BooleanSerializer : JsonSerializer { + + override fun serialize(value: Boolean?, type: Type, context: JsonSerializationContext): JsonElement? { + if (value != null && value != false) + return JsonPrimitive(value) + return null + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/IdExclusion.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/IdExclusion.kt index 867cb81b31..92c95cb032 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/IdExclusion.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/IdExclusion.kt @@ -2,10 +2,10 @@ package eu.kanade.tachiyomi.data.backup.serializer import com.google.gson.ExclusionStrategy import com.google.gson.FieldAttributes -import eu.kanade.tachiyomi.data.database.models.Category -import eu.kanade.tachiyomi.data.database.models.Chapter -import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.database.models.MangaSync +import eu.kanade.tachiyomi.data.database.models.CategoryImpl +import eu.kanade.tachiyomi.data.database.models.ChapterImpl +import eu.kanade.tachiyomi.data.database.models.MangaImpl +import eu.kanade.tachiyomi.data.database.models.MangaSyncImpl class IdExclusion : ExclusionStrategy { @@ -15,10 +15,10 @@ class IdExclusion : ExclusionStrategy { private val syncExclusions = listOf("id", "manga_id", "update") override fun shouldSkipField(f: FieldAttributes) = when (f.declaringClass) { - Manga::class.java -> mangaExclusions.contains(f.name) - Chapter::class.java -> chapterExclusions.contains(f.name) - MangaSync::class.java -> syncExclusions.contains(f.name) - Category::class.java -> categoryExclusions.contains(f.name) + MangaImpl::class.java -> mangaExclusions.contains(f.name) + ChapterImpl::class.java -> chapterExclusions.contains(f.name) + MangaSyncImpl::class.java -> syncExclusions.contains(f.name) + CategoryImpl::class.java -> categoryExclusions.contains(f.name) else -> false } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/LongSerializer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/LongSerializer.kt new file mode 100644 index 0000000000..be62e3fe1d --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/serializer/LongSerializer.kt @@ -0,0 +1,16 @@ +package eu.kanade.tachiyomi.data.backup.serializer + +import com.google.gson.JsonElement +import com.google.gson.JsonPrimitive +import com.google.gson.JsonSerializationContext +import com.google.gson.JsonSerializer +import java.lang.reflect.Type + +class LongSerializer : JsonSerializer { + + override fun serialize(value: Long?, type: Type, context: JsonSerializationContext): JsonElement? { + if (value != null && value !== 0L) + return JsonPrimitive(value) + return null + } +}