From 5a8cc0f14d33f15835fd9d8e32748c5d17a86298 Mon Sep 17 00:00:00 2001 From: merkost Date: Tue, 18 Jul 2023 16:21:44 +1000 Subject: [PATCH] Added BackupType with BackupSerializer --- .../smsmessenger/models/BackupType.kt | 13 +++++++++++ .../smsmessenger/models/MessagesBackup.kt | 23 +++++++++++++++++++ .../smsmessenger/models/MmsBackup.kt | 4 +++- .../smsmessenger/models/SmsBackup.kt | 6 +++-- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt new file mode 100644 index 00000000..c6132aee --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/BackupType.kt @@ -0,0 +1,13 @@ +package com.simplemobiletools.smsmessenger.models + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +enum class BackupType { + @SerialName("sms") + SMS, + + @SerialName("mms") + MMS, +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt new file mode 100644 index 00000000..9f97aac2 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MessagesBackup.kt @@ -0,0 +1,23 @@ +package com.simplemobiletools.smsmessenger.models + +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.* + +@Serializable(with = BackupSerializer::class) +sealed class MessagesBackup() { + @SerialName("backupType") + abstract val backupType: BackupType +} + +object BackupSerializer : + JsonContentPolymorphicSerializer(MessagesBackup::class) { + override fun selectDeserializer(element: JsonElement): DeserializationStrategy { + return when (element.jsonObject["backupType"]?.jsonPrimitive?.content) { + "sms" -> SmsBackup.serializer() + "mms" -> MmsBackup.serializer() + else -> throw Exception("ERROR: No Serializer found. Serialization failed.") + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt index c2c2c93c..3d0c9d25 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/MmsBackup.kt @@ -46,7 +46,9 @@ data class MmsBackup( val addresses: List, @SerializedName("parts") val parts: List, -) { + + override val backupType: BackupType = BackupType.MMS, +): MessagesBackup() { fun toContentValues(): ContentValues { return contentValuesOf( diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt index 41b128e1..ff3c9a39 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt @@ -30,8 +30,10 @@ data class SmsBackup( @SerializedName("type") val type: Int, @SerializedName("service_center") - val serviceCenter: String? -) { + val serviceCenter: String?, + + override val backupType: BackupType = BackupType.SMS, + ): MessagesBackup() { fun toContentValues(): ContentValues { return contentValuesOf(