avoid inserting the same caldav event twice
This commit is contained in:
parent
016b27e762
commit
5b134a63e2
2 changed files with 22 additions and 19 deletions
|
@ -267,7 +267,14 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
|
|||
return calendars
|
||||
}
|
||||
|
||||
fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
|
||||
fun Context.fetchCalDAVCalendarEvents(calendarId: Long, eventTypeId: Int) {
|
||||
val importIdsMap = HashMap<String, Int>()
|
||||
val existingEvents = dbHelper.getEventsFromCalDAVCalendar(calendarId)
|
||||
existingEvents.forEach {
|
||||
it.id = 0
|
||||
importIdsMap.put(it.importId, it.hashCode())
|
||||
}
|
||||
|
||||
val uri = CalendarContract.Events.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
CalendarContract.Events._ID,
|
||||
|
@ -278,7 +285,7 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
|
|||
CalendarContract.Events.DURATION,
|
||||
CalendarContract.Events.ALL_DAY,
|
||||
CalendarContract.Events.RRULE)
|
||||
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarID"
|
||||
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarId"
|
||||
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
|
@ -299,20 +306,20 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
|
|||
endTS = startTS + Parser().parseDuration(duration)
|
||||
}
|
||||
|
||||
val importId = getCalDAVEventImportId(calendarID, id)
|
||||
val importId = getCalDAVEventImportId(calendarId, id)
|
||||
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
|
||||
val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }),
|
||||
reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval,
|
||||
importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, lastUpdated = System.currentTimeMillis(),
|
||||
source = "$CALDAV-$calendarID")
|
||||
|
||||
importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, source = "$CALDAV-$calendarId")
|
||||
|
||||
if (event.getIsAllDay() && endTS > startTS) {
|
||||
event.endTS -= DAY
|
||||
}
|
||||
|
||||
dbHelper.insert(event) {
|
||||
if (importIdsMap[importId] != event.hashCode()) {
|
||||
dbHelper.insert(event) {
|
||||
|
||||
}
|
||||
}
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
|
|
|
@ -436,17 +436,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
return ids.filter { it.trim().isNotEmpty() } as ArrayList<String>
|
||||
}
|
||||
|
||||
fun getEventWithImportId(importId: String): Event? {
|
||||
val selection = "$MAIN_TABLE_NAME.$COL_IMPORT_ID = ?"
|
||||
val selectionArgs = arrayOf(importId.toString())
|
||||
val cursor = getEventsCursor(selection, selectionArgs)
|
||||
val events = fillEvents(cursor)
|
||||
return if (!events.isEmpty())
|
||||
events[0]
|
||||
else
|
||||
null
|
||||
}
|
||||
|
||||
fun getEventWithId(id: Int): Event? {
|
||||
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
|
@ -656,6 +645,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
return events
|
||||
}
|
||||
|
||||
fun getEventsFromCalDAVCalendar(calendarId: Long): List<Event> {
|
||||
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = ?"
|
||||
val selectionArgs = arrayOf("$CALDAV-$calendarId")
|
||||
val cursor = getEventsCursor(selection, selectionArgs)
|
||||
return fillEvents(cursor)
|
||||
}
|
||||
|
||||
private fun getEventsCursor(selection: String = "", selectionArgs: Array<String>? = null): Cursor? {
|
||||
val builder = SQLiteQueryBuilder()
|
||||
builder.tables = "$MAIN_TABLE_NAME LEFT OUTER JOIN $META_TABLE_NAME ON $COL_EVENT_ID = $MAIN_TABLE_NAME.$COL_ID"
|
||||
|
@ -698,7 +694,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
ArrayList<Int>()
|
||||
}
|
||||
|
||||
if (repeatInterval % MONTH == 0 && repeatRule == 0) {
|
||||
if (repeatInterval > 0 && repeatInterval % MONTH == 0 && repeatRule == 0) {
|
||||
repeatRule = REPEAT_MONTH_SAME_DAY
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue