From 3b6f06db8bdfe875ff9b7c701b379da81b8a0185 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 16 Nov 2018 18:03:53 +0100 Subject: [PATCH] simplify repetition exception handling, CalDAV synced too --- .../calendar/pro/activities/EventActivity.kt | 4 +- .../calendar/pro/extensions/Context.kt | 2 +- .../calendar/pro/helpers/CalDAVHelper.kt | 23 ++++++----- .../calendar/pro/helpers/EventsHelper.kt | 41 +++++-------------- .../calendar/pro/helpers/IcsImporter.kt | 2 +- 5 files changed, 27 insertions(+), 45 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 8e7469a8d..f7d4ddaa6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -680,7 +680,7 @@ class EventActivity : SimpleActivity() { DeleteEventDialog(this, arrayListOf(mEvent.id!!), mEvent.repeatInterval > 0) { Thread { when (it) { - DELETE_SELECTED_OCCURRENCE -> eventsHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true) + DELETE_SELECTED_OCCURRENCE -> eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true) DELETE_FUTURE_OCCURRENCES -> eventsHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS) DELETE_ALL_OCCURRENCES -> eventsHelper.deleteEvent(mEvent.id!!, true) } @@ -817,7 +817,7 @@ class EventActivity : SimpleActivity() { }.start() } else { Thread { - eventsHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true) + eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true) mEvent.apply { parentId = id!!.toLong() id = null diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index cce64bea5..95cce17e1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -440,7 +440,7 @@ fun Context.handleEventDeleting(eventIds: List, timestamps: List, ac when (action) { DELETE_SELECTED_OCCURRENCE -> { eventIds.forEachIndexed { index, value -> - eventsHelper.addEventRepeatException(value, timestamps[index], true) + eventsHelper.addEventRepetitionException(value, timestamps[index], true) } } DELETE_FUTURE_OCCURRENCES -> { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index 165655cdc..5ff33b9a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -213,6 +213,18 @@ class CalDAVHelper(val context: Context) { } fetchedEventIds.add(importId) + + // 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.eventsDB.getEventIdWithImportId(parentImportId) + if (parentEventId != null) { + event.parentId = parentEventId + eventsHelper.addEventRepetitionException(parentEventId, originalInstanceTime / 1000L, false) + continue + } + } + if (importIdsMap.containsKey(event.importId)) { val existingEvent = importIdsMap[importId] val originalEventId = existingEvent!!.id @@ -221,6 +233,7 @@ class CalDAVHelper(val context: Context) { this.id = null color = 0 lastUpdated = 0L + repetitionExceptions = ArrayList() } if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) { @@ -232,16 +245,6 @@ class CalDAVHelper(val context: Context) { importIdsMap[event.importId] = event eventsHelper.insertEvent(null, event, false) } - - // 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.eventsDB.getEventIdWithImportId(parentImportId) - if (parentEventId != null) { - event.parentId = parentEventId - eventsHelper.addEventRepeatException(parentEventId, originalInstanceTime / 1000L, false, event.importId) - } - } } } while (cursor.moveToNext()) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index dd4999b4f..b811f6484 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -186,39 +186,18 @@ class EventsHelper(val context: Context) { }.start() } - fun addEventRepeatException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String? = null) { - fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) - } + fun addEventRepetitionException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean) { + Thread { + val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread + val parentEventRepetitionExceptions = parentEvent.repetitionExceptions + parentEventRepetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS)) + eventsDB.updateEventRepetitionExceptions(parentEventRepetitionExceptions, parentEventId) + context.scheduleNextEventReminder(parentEvent) - private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String?) { - val childEvent = eventsDB.getEventWithId(parentEventId) ?: return - - childEvent.apply { - id = null - parentId = parentEventId - startTS = 0 - endTS = 0 - if (childImportId != null) { - importId = childImportId + if (addToCalDAV && config.caldavSync) { + context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS) } - } - - insertEvent(null, childEvent, false) { - val childEventId = it - Thread { - val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread - val parentEventRepetitionExceptions = parentEvent.repetitionExceptions - parentEventRepetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS)) - eventsDB.updateEventRepetitionExceptions(parentEventRepetitionExceptions, parentEventId) - context.scheduleNextEventReminder(parentEvent) - - if (addToCalDAV && config.caldavSync) { - val newId = context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS) - val newImportId = "${parentEvent.source}-$newId" - eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId) - } - }.start() - } + }.start() } fun getEvents(fromTS: Long, toTS: Long, eventId: Long = -1L, applyTypeFilter: Boolean = true, callback: (events: ArrayList) -> Unit) { 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 8c7bdaace..41a784eaf 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 @@ -162,7 +162,7 @@ class IcsImporter(val activity: SimpleActivity) { } else { eventsHelper.insertEvent(activity, event, true) { for (exceptionTS in curRepeatExceptions) { - eventsHelper.addEventRepeatException(it, exceptionTS, true) + eventsHelper.addEventRepetitionException(it, exceptionTS, true) } existingEvents.add(event) }