From 297db04f46a00bf679b987afe5e4f6654fd6df4a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 14 Aug 2017 22:08:17 +0200 Subject: [PATCH] if caldav event endTS is missing, use start + duration --- .../calendar/extensions/Context.kt | 8 +++++-- .../calendar/helpers/IcsImporter.kt | 22 ++----------------- .../calendar/helpers/Parser.kt | 18 +++++++++++++++ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index 1a458c3e9..a1c63ea38 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -289,12 +289,16 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) { val title = cursor.getStringValue(CalendarContract.Events.TITLE) val description = cursor.getStringValue(CalendarContract.Events.DESCRIPTION) val startTS = (cursor.getLongValue(CalendarContract.Events.DTSTART) / 1000).toInt() - val endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt() - val duration = cursor.getStringValue(CalendarContract.Events.DURATION) + var endTS = (cursor.getLongValue(CalendarContract.Events.DTEND) / 1000).toInt() val allDay = cursor.getIntValue(CalendarContract.Events.ALL_DAY) val rrule = cursor.getStringValue(CalendarContract.Events.RRULE) ?: "" val reminders = getCalDAVEventReminders(id) + if (endTS == 0) { + val duration = cursor.getStringValue(CalendarContract.Events.DURATION) + endTS = startTS + Parser().parseDuration(duration) + } + val importId = getCalDAVEventImportId(calendarID, id) val repeatRule = Parser().parseRepeatInterval(rrule, startTS) val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }), diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt index 863e75e52..09f86006f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt @@ -63,7 +63,7 @@ class IcsImporter { curEnd = getTimestamp(line.substring(DTEND.length)) } else if (line.startsWith(DURATION)) { val duration = line.substring(DURATION.length) - curEnd = curStart + decodeTime(duration) + curEnd = curStart + Parser().parseDuration(duration) } else if (line.startsWith(SUMMARY) && !isNotificationDescription) { curTitle = line.substring(SUMMARY.length) curTitle = getTitle(curTitle).replace("\\n", "\n") @@ -81,7 +81,7 @@ class IcsImporter { lastReminderAction = line.substring(ACTION.length) } else if (line.startsWith(TRIGGER)) { if (lastReminderAction == DISPLAY) - curReminderMinutes.add(decodeTime(line.substring(TRIGGER.length)) / 60) + curReminderMinutes.add(Parser().parseDuration(line.substring(TRIGGER.length)) / 60) } else if (line.startsWith(CATEGORIES)) { val categories = line.substring(CATEGORIES.length) tryAddCategories(categories, context) @@ -170,24 +170,6 @@ class IcsImporter { } } - // P0DT1H0M0S - private fun decodeTime(duration: String): Int { - val weeks = getDurationValue(duration, "W") - val days = getDurationValue(duration, "DT") - val hours = getDurationValue(duration, "H") - val minutes = getDurationValue(duration, "M") - val seconds = getDurationValue(duration, "S") - - val minSecs = 60 - val hourSecs = minSecs * 60 - val daySecs = hourSecs * 24 - val weekSecs = daySecs * 7 - - return seconds + (minutes * minSecs) + (hours * hourSecs) + (days * daySecs) + (weeks * weekSecs) - } - - private fun getDurationValue(duration: String, char: String): Int = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0 - private fun resetValues() { curStart = -1 curEnd = -1 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt index 4e6e87cf6..b1bea1490 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt @@ -190,4 +190,22 @@ class Parser { else -> SU } } + + // from P0DT1H5M0S to 3900 (seconds) + fun parseDuration(duration: String): Int { + val weeks = getDurationValue(duration, "W") + val days = getDurationValue(duration, "DT") + val hours = getDurationValue(duration, "H") + val minutes = getDurationValue(duration, "M") + val seconds = getDurationValue(duration, "S") + + val minSecs = 60 + val hourSecs = minSecs * 60 + val daySecs = hourSecs * 24 + val weekSecs = daySecs * 7 + + return seconds + (minutes * minSecs) + (hours * hourSecs) + (days * daySecs) + (weeks * weekSecs) + } + + private fun getDurationValue(duration: String, char: String) = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0 }