From 9b4be2717e481adb4f4ce710fdc425578cd3ca52 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 3 Dec 2018 15:24:02 +0100 Subject: [PATCH] fix #711, handle some new cases at importing linked events via sequences --- .../calendar/pro/helpers/Constants.kt | 2 ++ .../calendar/pro/helpers/IcsImporter.kt | 32 ++++++++++++++++++- .../calendar/pro/interfaces/EventsDao.kt | 3 ++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 00869f839..6d0838779 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -104,6 +104,8 @@ const val BYDAY = "BYDAY" const val BYMONTHDAY = "BYMONTHDAY" const val BYMONTH = "BYMONTH" const val LOCATION = "LOCATION" +const val RECURRENCE_ID = "RECURRENCE-ID" +const val SEQUENCE = "SEQUENCE" // this tag isn't a standard ICS tag, but there's no official way of adding a category color in an ics file const val CATEGORY_COLOR = "CATEGORY_COLOR:" diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt index 7d146bcff..76ce0086b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt @@ -23,6 +23,7 @@ class IcsImporter(val activity: SimpleActivity) { private var curLocation = "" private var curDescription = "" private var curImportId = "" + private var curRecurrenceDayCode = "" private var curFlags = 0 private var curReminderMinutes = ArrayList() private var curRepeatExceptions = ArrayList() @@ -35,6 +36,7 @@ class IcsImporter(val activity: SimpleActivity) { private var isNotificationDescription = false private var isProperReminderAction = false private var isDescription = false + private var isSequence = false private var curReminderTriggerMinutes = -1 private val eventsHelper = activity.eventsHelper @@ -121,6 +123,11 @@ class IcsImporter(val activity: SimpleActivity) { curRepeatExceptions.add(Formatter.getDayCodeFromTS(getTimestamp(value))) } else if (line.startsWith(LOCATION)) { curLocation = getLocation(line.substring(LOCATION.length).replace("\\,", ",")) + } else if (line.startsWith(RECURRENCE_ID)) { + val timestamp = getTimestamp(line.substring(RECURRENCE_ID.length)) + curRecurrenceDayCode = Formatter.getDayCodeFromTS(timestamp) + } else if (line.startsWith(SEQUENCE)) { + isSequence = true } else if (line == END_ALARM) { if (isProperReminderAction && curReminderTriggerMinutes != -1) { curReminderMinutes.add(curReminderTriggerMinutes) @@ -157,7 +164,28 @@ class IcsImporter(val activity: SimpleActivity) { } if (eventToUpdate == null) { - eventsToInsert.add(event) + // if an event belongs to a sequence insert it immediately, to avoid some glitches with linked events + if (isSequence) { + if (curRecurrenceDayCode.isEmpty()) { + eventsHelper.insertEvent(null, event, true) + } else { + // if an event contains the RECURRENCE-ID field, it is an exception to a recurring event, so update its parent too + val parentEvent = activity.eventsDB.getEventWithImportId(event.importId) + if (parentEvent != null) { + if (parentEvent.repetitionExceptions.contains(curRecurrenceDayCode)) { + continue + } + + parentEvent.repetitionExceptions.add(curRecurrenceDayCode) + activity.eventsDB.insertOrUpdate(parentEvent) + + event.parentId = parentEvent.id!! + eventsToInsert.add(event) + } + } + } else { + eventsToInsert.add(event) + } } else { event.id = eventToUpdate.id eventsHelper.updateEvent(null, event, true) @@ -241,6 +269,7 @@ class IcsImporter(val activity: SimpleActivity) { curLocation = "" curDescription = "" curImportId = "" + curRecurrenceDayCode = "" curFlags = 0 curReminderMinutes = ArrayList() curRepeatExceptions = ArrayList() @@ -252,6 +281,7 @@ class IcsImporter(val activity: SimpleActivity) { curCategoryColor = -2 isNotificationDescription = false isProperReminderAction = false + isSequence = false curReminderTriggerMinutes = -1 } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index 5dc421f08..1926eb38f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -20,6 +20,9 @@ interface EventsDao { @Query("SELECT * FROM events WHERE id = :id") fun getEventWithId(id: Long): Event? + @Query("SELECT * FROM events WHERE import_id = :importId") + fun getEventWithImportId(importId: String): Event? + @Query("SELECT * FROM events WHERE start_ts <= :toTS AND end_ts >= :fromTS AND repeat_interval = 0") fun getOneTimeEventsFromTo(toTS: Long, fromTS: Long): List