properly handle migrating to no longer minified PhoneNumber

This commit is contained in:
tibbi 2022-06-25 23:04:51 +02:00
parent 82643fdafb
commit b4e420c443
2 changed files with 22 additions and 5 deletions

View file

@ -4,16 +4,14 @@ import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.contacts.pro.models.Address
import com.simplemobiletools.contacts.pro.models.Email
import com.simplemobiletools.contacts.pro.models.Event
import com.simplemobiletools.contacts.pro.models.IM
import com.simplemobiletools.contacts.pro.models.*
class Converters {
private val gson = Gson()
private val longType = object : TypeToken<List<Long>>() {}.type
private val stringType = object : TypeToken<List<String>>() {}.type
private val numberType = object : TypeToken<List<PhoneNumber>>() {}.type
private val numberConverterType = object : TypeToken<List<PhoneNumberConverter>>() {}.type
private val emailType = object : TypeToken<List<Email>>() {}.type
private val addressType = object : TypeToken<List<Address>>() {}.type
private val eventType = object : TypeToken<List<Event>>() {}.type
@ -31,8 +29,23 @@ class Converters {
@TypeConverter
fun longListToJson(list: ArrayList<Long>) = gson.toJson(list)
// some hacky converting is needed since PhoneNumber model has been added to proguard rules, but obfuscated json was stored in database
// convert [{"a":"678910","b":2,"c":"","d":"678910","e":false}] to PhoneNumber(value=678910, type=2, label=, normalizedNumber=678910, isPrimary=false)
@TypeConverter
fun jsonToPhoneNumberList(value: String) = gson.fromJson<ArrayList<PhoneNumber>>(value, numberType)
fun jsonToPhoneNumberList(value: String): ArrayList<PhoneNumber> {
val numbers = gson.fromJson<ArrayList<PhoneNumber>>(value, numberType)
return if (numbers.any { it.value == null }) {
val phoneNumbers = ArrayList<PhoneNumber>()
val numberConverters = gson.fromJson<ArrayList<PhoneNumberConverter>>(value, numberConverterType)
numberConverters.forEach { converter ->
val phoneNumber = PhoneNumber(converter.a, converter.b, converter.c, converter.d, converter.e)
phoneNumbers.add(phoneNumber)
}
phoneNumbers
} else {
numbers
}
}
@TypeConverter
fun phoneNumberListToJson(list: ArrayList<PhoneNumber>) = gson.toJson(list)

View file

@ -0,0 +1,4 @@
package com.simplemobiletools.contacts.pro.models
// need for hacky parsing of no longer minified PhoneNumber model in Converters.kt
data class PhoneNumberConverter(var a: String, var b: Int, var c: String, var d: String, var e: Boolean = false)