diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt index 53d8660b8..a190777ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt @@ -208,6 +208,8 @@ class CalDAVHandler(val context: Context) { CalendarContract.Events.DURATION, CalendarContract.Events.ALL_DAY, CalendarContract.Events.RRULE, + CalendarContract.Events.ORIGINAL_ID, + CalendarContract.Events.ORIGINAL_INSTANCE_TIME, CalendarContract.Events.EVENT_LOCATION) val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarId" @@ -224,6 +226,8 @@ class CalDAVHandler(val context: Context) { val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY) val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: "" val location = cursor.getStringValue(CalendarContract.Events.EVENT_LOCATION) ?: "" + val originalId = cursor.getStringValue(CalendarContract.Events.ORIGINAL_ID) + val originalInstanceTime = cursor.getLongValue(CalendarContract.Events.ORIGINAL_INSTANCE_TIME) val reminders = getCalDAVEventReminders(id) if (endTS == 0) { @@ -232,10 +236,11 @@ class CalDAVHandler(val context: Context) { } val importId = getCalDAVEventImportId(calendarId, id) + val source = "$CALDAV-$calendarId" val repeatRule = Parser().parseRepeatInterval(rrule, startTS) val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }), reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval, - importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, source = "$CALDAV-$calendarId", + importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, source = source, location = location) if (event.getIsAllDay() && endTS > startTS) { @@ -255,6 +260,15 @@ class CalDAVHandler(val context: Context) { } else { context.dbHelper.insert(event, false) { importIdsMap.put(event.importId, event) + + // if the event is an exception from another events repeat rule, find the original parent event + if (originalInstanceTime != 0L) { + val parentImportId = "$source-$originalId" + val parentEventId = context.dbHelper.getEventIdWithImportId(parentImportId) + if (parentEventId != 0) { + context.dbHelper.addEventRepeatException(parentEventId, (originalInstanceTime / 1000).toInt()) + } + } } } } while (cursor.moveToNext()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 043801379..458964a83 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -565,12 +565,24 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val cursor = getEventsCursor(selection, selectionArgs) val events = fillEvents(cursor) return if (events.isNotEmpty()) { - events[0] + events.first() } else { null } } + fun getEventIdWithImportId(id: String): Int { + val selection = "$MAIN_TABLE_NAME.$COL_IMPORT_ID = ?" + val selectionArgs = arrayOf(id) + val cursor = getEventsCursor(selection, selectionArgs) + val events = fillEvents(cursor) + return if (events.isNotEmpty()) { + events.first().id + } else { + 0 + } + } + fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList) -> Unit) { Thread { getEventsInBackground(fromTS, toTS, eventId, callback)