Merge pull request #1486 from KryptKode/fix/caldav-colors

fix updating event type colors for caldav events
This commit is contained in:
Tibor Kaputa
2021-10-08 19:00:00 +02:00
committed by GitHub
2 changed files with 43 additions and 41 deletions

View File

@ -14,10 +14,10 @@ import com.simplemobiletools.calendar.pro.models.*
import com.simplemobiletools.calendar.pro.objects.States.isUpdatingCalDAV import com.simplemobiletools.calendar.pro.objects.States.isUpdatingCalDAV
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
class CalDAVHelper(val context: Context) { class CalDAVHelper(val context: Context) {
@ -37,6 +37,7 @@ class CalDAVHelper(val context: Context) {
title = calendar.displayName title = calendar.displayName
caldavDisplayName = calendar.displayName caldavDisplayName = calendar.displayName
caldavEmail = calendar.accountName caldavEmail = calendar.accountName
color = calendar.color
eventsHelper.insertOrUpdateEventTypeSync(this) eventsHelper.insertOrUpdateEventTypeSync(this)
} }
@ -65,7 +66,8 @@ class CalDAVHelper(val context: Context) {
Calendars.ACCOUNT_TYPE, Calendars.ACCOUNT_TYPE,
Calendars.OWNER_ACCOUNT, Calendars.OWNER_ACCOUNT,
Calendars.CALENDAR_COLOR, Calendars.CALENDAR_COLOR,
Calendars.CALENDAR_ACCESS_LEVEL) Calendars.CALENDAR_ACCESS_LEVEL
)
val selection = if (ids.trim().isNotEmpty()) "${Calendars._ID} IN ($ids)" else null val selection = if (ids.trim().isNotEmpty()) "${Calendars._ID} IN ($ids)" else null
context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor -> context.queryCursor(uri, projection, selection, showErrors = showToasts) { cursor ->
@ -83,37 +85,34 @@ class CalDAVHelper(val context: Context) {
return calendars return calendars
} }
// check if the calendars color or title has changed
fun updateCalDAVCalendar(eventType: EventType) { fun updateCalDAVCalendar(eventType: EventType) {
val uri = Calendars.CONTENT_URI val uri = ContentUris.withAppendedId(Calendars.CONTENT_URI, eventType.caldavCalendarId.toLong())
val newUri = ContentUris.withAppendedId(uri, eventType.caldavCalendarId.toLong()) val values = ContentValues().apply {
val projection = arrayOf( val colorKey = getCalDAVColorKey(eventType)
Calendars.CALENDAR_COLOR_KEY, if (colorKey != null) {
Calendars.CALENDAR_COLOR, put(Calendars.CALENDAR_COLOR_KEY, getCalDAVColorKey(eventType))
Calendars.CALENDAR_DISPLAY_NAME } else {
) put(Calendars.CALENDAR_COLOR, eventType.color)
put(Calendars.CALENDAR_COLOR_KEY, "")
}
put(Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
}
context.queryCursor(newUri, projection) { cursor ->
val properColorKey = cursor.getIntValue(Calendars.CALENDAR_COLOR_KEY)
val properColor = cursor.getIntValue(Calendars.CALENDAR_COLOR)
val displayName = cursor.getStringValue(Calendars.CALENDAR_DISPLAY_NAME)
if (eventType.color != properColor || displayName != eventType.title) {
val values = fillCalendarContentValues(properColorKey, displayName)
try { try {
context.contentResolver.update(newUri, values, null, null) context.contentResolver.update(uri, values, null, null)
eventType.color = properColor
eventType.title = displayName
context.eventTypesDB.insertOrUpdate(eventType) context.eventTypesDB.insertOrUpdate(eventType)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
} e.printStackTrace()
}
} }
} }
private fun fillCalendarContentValues(colorKey: Int, title: String): ContentValues { private fun getCalDAVColorKey(eventType: EventType): String? {
return ContentValues().apply { val colors = getAvailableCalDAVCalendarColors(eventType)
put(Calendars.CALENDAR_COLOR_KEY, colorKey) val colorKey = colors.indexOf(eventType.color)
put(Calendars.CALENDAR_DISPLAY_NAME, title) return if (colorKey > 0) {
colorKey.toString()
} else {
null
} }
} }
@ -209,11 +208,13 @@ class CalDAVHelper(val context: Context) {
val source = "$CALDAV-$calendarId" val source = "$CALDAV-$calendarId"
val repeatRule = Parser().parseRepeatInterval(rrule, startTS) val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
val event = Event(null, startTS, endTS, title, location, description, reminder1?.minutes ?: REMINDER_OFF, val event = Event(
null, startTS, endTS, title, location, description, reminder1?.minutes ?: REMINDER_OFF,
reminder2?.minutes ?: REMINDER_OFF, reminder3?.minutes ?: REMINDER_OFF, reminder1?.type reminder2?.minutes ?: REMINDER_OFF, reminder3?.minutes ?: REMINDER_OFF, reminder1?.type
?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type ?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type
?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule, ?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule,
repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, source = source, availability = availability) repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, source = source, availability = availability
)
if (event.getIsAllDay()) { if (event.getIsAllDay()) {
event.startTS = Formatter.getShiftedImportTimestamp(event.startTS) event.startTS = Formatter.getShiftedImportTimestamp(event.startTS)
@ -471,7 +472,8 @@ class CalDAVHelper(val context: Context) {
val uri = Reminders.CONTENT_URI val uri = Reminders.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
Reminders.MINUTES, Reminders.MINUTES,
Reminders.METHOD) Reminders.METHOD
)
val selection = "${Reminders.EVENT_ID} = $eventId" val selection = "${Reminders.EVENT_ID} = $eventId"
context.queryCursor(uri, projection, selection) { cursor -> context.queryCursor(uri, projection, selection) { cursor ->
@ -494,7 +496,8 @@ class CalDAVHelper(val context: Context) {
Attendees.ATTENDEE_NAME, Attendees.ATTENDEE_NAME,
Attendees.ATTENDEE_EMAIL, Attendees.ATTENDEE_EMAIL,
Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS,
Attendees.ATTENDEE_RELATIONSHIP) Attendees.ATTENDEE_RELATIONSHIP
)
val selection = "${Attendees.EVENT_ID} = $eventId" val selection = "${Attendees.EVENT_ID} = $eventId"
context.queryCursor(uri, projection, selection) { cursor -> context.queryCursor(uri, projection, selection) { cursor ->
val name = cursor.getStringValue(Attendees.ATTENDEE_NAME) ?: "" val name = cursor.getStringValue(Attendees.ATTENDEE_NAME) ?: ""

View File

@ -8,9 +8,9 @@ import androidx.room.PrimaryKey
import com.simplemobiletools.calendar.pro.extensions.seconds import com.simplemobiletools.calendar.pro.extensions.seconds
import com.simplemobiletools.calendar.pro.helpers.* import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.commons.extensions.addBitIf import com.simplemobiletools.commons.extensions.addBitIf
import java.io.Serializable
import org.joda.time.DateTime import org.joda.time.DateTime
import org.joda.time.DateTimeZone import org.joda.time.DateTimeZone
import java.io.Serializable
@Entity(tableName = "events", indices = [(Index(value = ["id"], unique = true))]) @Entity(tableName = "events", indices = [(Index(value = ["id"], unique = true))])
data class Event( data class Event(
@ -38,8 +38,9 @@ data class Event(
@ColumnInfo(name = "parent_id") var parentId: Long = 0, @ColumnInfo(name = "parent_id") var parentId: Long = 0,
@ColumnInfo(name = "last_updated") var lastUpdated: Long = 0L, @ColumnInfo(name = "last_updated") var lastUpdated: Long = 0L,
@ColumnInfo(name = "source") var source: String = SOURCE_SIMPLE_CALENDAR, @ColumnInfo(name = "source") var source: String = SOURCE_SIMPLE_CALENDAR,
@ColumnInfo(name = "availability") var availability: Int = 0) @ColumnInfo(name = "availability") var availability: Int = 0,
: Serializable { var color: Int = 0,
) : Serializable {
companion object { companion object {
private const val serialVersionUID = -32456795132345616L private const val serialVersionUID = -32456795132345616L
@ -196,8 +197,6 @@ data class Event(
flags = flags.addBitIf(isPastEvent, FLAG_IS_PAST_EVENT) flags = flags.addBitIf(isPastEvent, FLAG_IS_PAST_EVENT)
} }
var color: Int = 0
fun getTimeZoneString(): String { fun getTimeZoneString(): String {
return if (timeZone.isNotEmpty() && getAllTimeZones().map { it.zoneName }.contains(timeZone)) { return if (timeZone.isNotEmpty() && getAllTimeZones().map { it.zoneName }.contains(timeZone)) {
timeZone timeZone