From e718c022756c8e2f14c7ed03ce3839001ed3dcbb Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 20 Aug 2017 18:38:33 +0200 Subject: [PATCH] delete invalid caldav events at sync --- .../calendar/helpers/CalDAVEventsHandler.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt index 71fb2bd87..55287c3a2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVEventsHandler.kt @@ -55,6 +55,7 @@ class CalDAVEventsHandler(val context: Context) { fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) { val importIdsMap = HashMap() + val fetchedEventIds = ArrayList() val existingEvents = context.dbHelper.getEventsFromCalDAVCalendar(calendarId) existingEvents.forEach { importIdsMap.put(it.importId, it) @@ -101,6 +102,7 @@ class CalDAVEventsHandler(val context: Context) { event.endTS -= DAY } + fetchedEventIds.add(importId) if (importIdsMap.containsKey(event.importId)) { val existingEvent = importIdsMap[importId] val originalEventId = existingEvent!!.id @@ -120,6 +122,20 @@ class CalDAVEventsHandler(val context: Context) { } finally { cursor?.close() } + + val eventIdsToDelete = ArrayList() + importIdsMap.keys.filter { !fetchedEventIds.contains(it) }.forEach { + val caldavEventId = it + existingEvents.forEach { + if (it.importId == caldavEventId) { + eventIdsToDelete.add(it.id.toString()) + } + } + } + + eventIdsToDelete.forEach { + context.dbHelper.deleteEvents(eventIdsToDelete.toTypedArray(), false) + } } fun addCalDAVEvent(event: Event) {