From f4c28d26f0cf5e75cb979d746180c801eb8287a0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 15 Aug 2017 23:02:17 +0200 Subject: [PATCH] add a helper function for adding new events in caldav --- .../calendar/extensions/Context.kt | 23 +++++++++++++++++++ .../calendar/helpers/IcsExporter.kt | 17 +------------- .../calendar/helpers/Parser.kt | 16 +++++++++++++ 3 files changed, 40 insertions(+), 16 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 e00cd5954..2d6e326ab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -8,6 +8,7 @@ import android.app.NotificationManager import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.ComponentName +import android.content.ContentValues import android.content.Context import android.content.Intent import android.content.pm.PackageManager @@ -336,6 +337,28 @@ fun Context.fetchCalDAVCalendarEvents(calendarId: Long, eventTypeId: Int) { } } +fun Context.addCalDAVEvent(event: Event, calendarId: Int) { + val durationMinutes = (event.endTS - event.startTS) / 1000 / 60 + val uri = CalendarContract.Events.CONTENT_URI + val values = ContentValues().apply { + put(CalendarContract.Events.CALENDAR_ID, calendarId) + put(CalendarContract.Events.TITLE, event.title) + put(CalendarContract.Events.DESCRIPTION, event.description) + put(CalendarContract.Events.DTSTART, event.startTS * 1000L) + put(CalendarContract.Events.ALL_DAY, if (event.getIsAllDay()) 1 else 0) + put(CalendarContract.Events.RRULE, Parser().getShortRepeatInterval(event)) + put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString()) + + if (event.repeatInterval > 0) { + put(CalendarContract.Events.DURATION, Parser().getDurationString(durationMinutes)) + } else { + put(CalendarContract.Events.DTEND, event.endTS * 1000L) + } + } + + contentResolver.insert(uri, values) +} + fun Context.getCalDAVEventReminders(eventId: Long): List { val reminders = ArrayList() val uri = CalendarContract.Reminders.CONTENT_URI diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt index ada4ce253..68a0baf2a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt @@ -69,26 +69,11 @@ class IcsExporter { if (minutes != -1) { out.writeLn(BEGIN_ALARM) out.writeLn("$ACTION$DISPLAY") - out.writeLn("$TRIGGER${getReminderString(minutes)}") + out.writeLn("$TRIGGER${Parser().getDurationString(minutes)}") out.writeLn(END_ALARM) } } - private fun getReminderString(minutes: Int): String { - var days = 0 - var hours = 0 - var remainder = minutes - if (remainder >= DAY_MINUTES) { - days = Math.floor(((remainder / DAY_MINUTES).toDouble())).toInt() - remainder -= days * DAY_MINUTES - } - if (remainder >= 60) { - hours = Math.floor(((remainder / 60).toDouble())).toInt() - remainder -= hours * 60 - } - return "-P${days}DT${hours}H${remainder}M0S" - } - private fun fillIgnoredOccurrences(event: Event, out: BufferedWriter) { event.ignoreEventOccurrences.forEach { out.writeLn("$EXDATE:$it}") 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 b1bea1490..7aa275c18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Parser.kt @@ -208,4 +208,20 @@ class Parser { } private fun getDurationValue(duration: String, char: String) = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0 + + // from 65 to P0DT1H5M0S + fun getDurationString(minutes: Int): String { + var days = 0 + var hours = 0 + var remainder = minutes + if (remainder >= DAY_MINUTES) { + days = Math.floor((remainder / DAY_MINUTES).toDouble()).toInt() + remainder -= days * DAY_MINUTES + } + if (remainder >= 60) { + hours = Math.floor((remainder / 60).toDouble()).toInt() + remainder -= hours * 60 + } + return "-P${days}DT${hours}H${remainder}M0S" + } }