add a helper function for adding new events in caldav

This commit is contained in:
tibbi 2017-08-15 23:02:17 +02:00
parent 3ed2c4e472
commit f4c28d26f0
3 changed files with 40 additions and 16 deletions

View File

@ -8,6 +8,7 @@ import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.content.ComponentName import android.content.ComponentName
import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager 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<Int> { fun Context.getCalDAVEventReminders(eventId: Long): List<Int> {
val reminders = ArrayList<Int>() val reminders = ArrayList<Int>()
val uri = CalendarContract.Reminders.CONTENT_URI val uri = CalendarContract.Reminders.CONTENT_URI

View File

@ -69,26 +69,11 @@ class IcsExporter {
if (minutes != -1) { if (minutes != -1) {
out.writeLn(BEGIN_ALARM) out.writeLn(BEGIN_ALARM)
out.writeLn("$ACTION$DISPLAY") out.writeLn("$ACTION$DISPLAY")
out.writeLn("$TRIGGER${getReminderString(minutes)}") out.writeLn("$TRIGGER${Parser().getDurationString(minutes)}")
out.writeLn(END_ALARM) 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) { private fun fillIgnoredOccurrences(event: Event, out: BufferedWriter) {
event.ignoreEventOccurrences.forEach { event.ignoreEventOccurrences.forEach {
out.writeLn("$EXDATE:$it}") out.writeLn("$EXDATE:$it}")

View File

@ -208,4 +208,20 @@ class Parser {
} }
private fun getDurationValue(duration: String, char: String) = Regex("[0-9]+(?=$char)").find(duration)?.value?.toInt() ?: 0 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"
}
} }