Replace manual `attendees` serialization with type converter

This commit is contained in:
Naveen 2023-09-27 23:14:07 +05:30
parent 52c403a7b1
commit 0241e3beea
No known key found for this signature in database
GPG Key ID: 0E155DAD31671DA3
9 changed files with 51 additions and 29 deletions

View File

@ -6,6 +6,7 @@ plugins {
alias(libs.plugins.android)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.ksp)
alias(libs.plugins.parcelize)
base
}

View File

@ -24,8 +24,6 @@ import android.widget.RelativeLayout
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_CLOCK
import com.google.android.material.timepicker.TimeFormat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.adapters.AutoCompleteTextViewAdapter
import com.simplemobiletools.calendar.pro.databinding.ActivityEventBinding
@ -171,7 +169,7 @@ class EventActivity : SimpleActivity() {
putInt(REPEAT_RULE, mRepeatRule)
putLong(REPEAT_LIMIT, mRepeatLimit)
putString(ATTENDEES, getAllAttendees(false))
putParcelableArrayList(ATTENDEES, getAllAttendees(false))
putInt(AVAILABILITY, mAvailability)
putInt(EVENT_COLOR, mEventColor)
@ -213,8 +211,7 @@ class EventActivity : SimpleActivity() {
mRepeatRule = getInt(REPEAT_RULE)
mRepeatLimit = getLong(REPEAT_LIMIT)
val token = object : TypeToken<List<Attendee>>() {}.type
mAttendees = Gson().fromJson<ArrayList<Attendee>>(getString(ATTENDEES), token) ?: ArrayList()
mAttendees = getParcelableArrayList(ATTENDEES) ?: arrayListOf()
mEventTypeId = getLong(EVENT_TYPE_ID)
mEventCalendarId = getInt(EVENT_CALENDAR_ID)
@ -507,8 +504,7 @@ class EventActivity : SimpleActivity() {
mAvailability = mEvent.availability
mEventColor = mEvent.color
val token = object : TypeToken<List<Attendee>>() {}.type
mAttendees = Gson().fromJson<ArrayList<Attendee>>(mEvent.attendees, token) ?: ArrayList()
mAttendees = mEvent.attendees.toMutableList() as ArrayList<Attendee>
checkRepeatTexts(mRepeatInterval)
checkAttendees()
@ -1310,7 +1306,7 @@ class EventActivity : SimpleActivity() {
flags = mEvent.flags.addBitIf(binding.eventAllDay.isChecked, FLAG_ALL_DAY)
repeatLimit = if (repeatInterval == 0) 0 else mRepeatLimit
repeatRule = mRepeatRule
attendees = if (mEventCalendarId == STORED_LOCALLY_ONLY) "" else getAllAttendees(true)
attendees = if (mEventCalendarId == STORED_LOCALLY_ONLY) emptyList() else getAllAttendees(true)
eventType = newEventType
lastUpdated = System.currentTimeMillis()
source = newSource
@ -1817,7 +1813,7 @@ class EventActivity : SimpleActivity() {
}
}
private fun getAllAttendees(isSavingEvent: Boolean): String {
private fun getAllAttendees(isSavingEvent: Boolean): ArrayList<Attendee> {
var attendees = ArrayList<Attendee>()
mSelectedContacts.forEach {
attendees.add(it)
@ -1839,7 +1835,7 @@ class EventActivity : SimpleActivity() {
}
}
return Gson().toJson(attendees)
return attendees
}
private fun getNames(): List<Attendee> {

View File

@ -7,8 +7,6 @@ import android.content.Context
import android.graphics.Color
import android.provider.CalendarContract.*
import android.widget.Toast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.*
@ -213,7 +211,7 @@ class CalDAVHelper(val context: Context) {
val originalId = cursor.getStringValue(Events.ORIGINAL_ID)
val originalInstanceTime = cursor.getLongValue(Events.ORIGINAL_INSTANCE_TIME)
val reminders = getCalDAVEventReminders(id)
val attendees = Gson().toJson(getCalDAVEventAttendees(id))
val attendees = getCalDAVEventAttendees(id)
val availability = cursor.getIntValue(Events.AVAILABILITY)
val status = cursor.getIntValue(Events.STATUS)
val color = cursor.getIntValueOrNull(Events.EVENT_COLOR)
@ -394,8 +392,7 @@ class CalDAVHelper(val context: Context) {
private fun setupCalDAVEventAttendees(event: Event) {
clearEventAttendees(event)
val attendees = Gson().fromJson<ArrayList<Attendee>>(event.attendees, object : TypeToken<List<Attendee>>() {}.type) ?: ArrayList()
attendees.forEach {
event.attendees.forEach {
val contentValues = ContentValues().apply {
put(Attendees.ATTENDEE_NAME, it.name)
put(Attendees.ATTENDEE_EMAIL, it.email)

View File

@ -3,10 +3,12 @@ package com.simplemobiletools.calendar.pro.helpers
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.calendar.pro.models.Attendee
class Converters {
private val gson = Gson()
private val stringType = object : TypeToken<List<String>>() {}.type
private val attendeeType = object : TypeToken<List<Attendee>>() {}.type
@TypeConverter
fun jsonToStringList(value: String): List<String> {
@ -25,4 +27,16 @@ class Converters {
@TypeConverter
fun stringListToJson(list: List<String>) = gson.toJson(list)
@TypeConverter
fun attendeeListToJson(list: List<Attendee>) = gson.toJson(list)
@TypeConverter
fun jsonToAttendeeList(value: String): List<Attendee> {
return try {
gson.fromJson<ArrayList<Attendee>>(value, attendeeType) ?: ArrayList()
} catch (e: Exception) {
emptyList()
}
}
}

View File

@ -249,7 +249,7 @@ class IcsImporter(val activity: SimpleActivity) {
curRepeatRule,
curRepeatLimit,
curRepeatExceptions,
"",
emptyList(),
curImportId,
DateTimeZone.getDefault().id,
curFlags,

View File

@ -2,14 +2,25 @@ package com.simplemobiletools.calendar.pro.models
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Parcelable
import android.provider.CalendarContract
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import kotlinx.parcelize.Parcelize
data class Attendee(val contactId: Int, var name: String, val email: String, var status: Int, var photoUri: String, var isMe: Boolean, var relationship: Int) {
@Parcelize
data class Attendee(
val contactId: Int,
var name: String,
val email: String,
var status: Int,
var photoUri: String,
var isMe: Boolean,
var relationship: Int
) : Parcelable {
fun getPublicName() = name.ifEmpty { email }
fun updateImage(context: Context, imageView: ImageView, placeholder: Drawable) {

View File

@ -30,7 +30,7 @@ data class Event(
@ColumnInfo(name = "repeat_rule") var repeatRule: Int = 0,
@ColumnInfo(name = "repeat_limit") var repeatLimit: Long = 0L,
@ColumnInfo(name = "repetition_exceptions") var repetitionExceptions: List<String> = emptyList(),
@ColumnInfo(name = "attendees") var attendees: String = "",
@ColumnInfo(name = "attendees") var attendees: List<Attendee> = emptyList(),
@ColumnInfo(name = "import_id") var importId: String = "",
@ColumnInfo(name = "time_zone") var timeZone: String = "",
@ColumnInfo(name = "flags") var flags: Int = 0,

View File

@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android).apply(false)
alias(libs.plugins.kotlinAndroid).apply(false)
alias(libs.plugins.ksp).apply(false)
alias(libs.plugins.parcelize).apply(false)
}
tasks.register<Delete>("clean") {

View File

@ -45,3 +45,5 @@ room = [
android = { id = "com.android.application", version.ref = "gradlePlugins-agp" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }