Added BackupType with BackupSerializer

This commit is contained in:
merkost 2023-07-18 16:21:44 +10:00
parent 5363af1071
commit 5a8cc0f14d
4 changed files with 43 additions and 3 deletions

View File

@ -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,
}

View File

@ -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>(MessagesBackup::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out MessagesBackup> {
return when (element.jsonObject["backupType"]?.jsonPrimitive?.content) {
"sms" -> SmsBackup.serializer()
"mms" -> MmsBackup.serializer()
else -> throw Exception("ERROR: No Serializer found. Serialization failed.")
}
}
}

View File

@ -46,7 +46,9 @@ data class MmsBackup(
val addresses: List<MmsAddress>, val addresses: List<MmsAddress>,
@SerializedName("parts") @SerializedName("parts")
val parts: List<MmsPart>, val parts: List<MmsPart>,
) {
override val backupType: BackupType = BackupType.MMS,
): MessagesBackup() {
fun toContentValues(): ContentValues { fun toContentValues(): ContentValues {
return contentValuesOf( return contentValuesOf(

View File

@ -30,8 +30,10 @@ data class SmsBackup(
@SerializedName("type") @SerializedName("type")
val type: Int, val type: Int,
@SerializedName("service_center") @SerializedName("service_center")
val serviceCenter: String? val serviceCenter: String?,
) {
override val backupType: BackupType = BackupType.SMS,
): MessagesBackup() {
fun toContentValues(): ContentValues { fun toContentValues(): ContentValues {
return contentValuesOf( return contentValuesOf(