mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-14 10:50:50 +01:00
avoid inserting the same caldav event twice
This commit is contained in:
parent
016b27e762
commit
5b134a63e2
@ -267,7 +267,14 @@ fun Context.getCalDAVCalendars(ids: String = ""): List<CalDAVCalendar> {
|
|||||||
return calendars
|
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 uri = CalendarContract.Events.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
CalendarContract.Events._ID,
|
CalendarContract.Events._ID,
|
||||||
@ -278,7 +285,7 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
|
|||||||
CalendarContract.Events.DURATION,
|
CalendarContract.Events.DURATION,
|
||||||
CalendarContract.Events.ALL_DAY,
|
CalendarContract.Events.ALL_DAY,
|
||||||
CalendarContract.Events.RRULE)
|
CalendarContract.Events.RRULE)
|
||||||
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarID"
|
val selection = "${CalendarContract.Events.CALENDAR_ID} = $calendarId"
|
||||||
|
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
@ -299,21 +306,21 @@ fun Context.fetchCalDAVCalendarEvents(calendarID: Long, eventTypeId: Int) {
|
|||||||
endTS = startTS + Parser().parseDuration(duration)
|
endTS = startTS + Parser().parseDuration(duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
val importId = getCalDAVEventImportId(calendarID, id)
|
val importId = getCalDAVEventImportId(calendarId, id)
|
||||||
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
|
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
|
||||||
val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }),
|
val event = Event(0, startTS, endTS, title, description, reminders.getOrElse(0, { -1 }),
|
||||||
reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval,
|
reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval,
|
||||||
importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, lastUpdated = System.currentTimeMillis(),
|
importId, allDay, repeatRule.repeatLimit, repeatRule.repeatRule, eventTypeId, source = "$CALDAV-$calendarId")
|
||||||
source = "$CALDAV-$calendarID")
|
|
||||||
|
|
||||||
|
|
||||||
if (event.getIsAllDay() && endTS > startTS) {
|
if (event.getIsAllDay() && endTS > startTS) {
|
||||||
event.endTS -= DAY
|
event.endTS -= DAY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (importIdsMap[importId] != event.hashCode()) {
|
||||||
dbHelper.insert(event) {
|
dbHelper.insert(event) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -436,17 +436,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
return ids.filter { it.trim().isNotEmpty() } as ArrayList<String>
|
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? {
|
fun getEventWithId(id: Int): Event? {
|
||||||
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"
|
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(id.toString())
|
||||||
@ -656,6 +645,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
return events
|
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? {
|
private fun getEventsCursor(selection: String = "", selectionArgs: Array<String>? = null): Cursor? {
|
||||||
val builder = SQLiteQueryBuilder()
|
val builder = SQLiteQueryBuilder()
|
||||||
builder.tables = "$MAIN_TABLE_NAME LEFT OUTER JOIN $META_TABLE_NAME ON $COL_EVENT_ID = $MAIN_TABLE_NAME.$COL_ID"
|
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>()
|
ArrayList<Int>()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (repeatInterval % MONTH == 0 && repeatRule == 0) {
|
if (repeatInterval > 0 && repeatInterval % MONTH == 0 && repeatRule == 0) {
|
||||||
repeatRule = REPEAT_MONTH_SAME_DAY
|
repeatRule = REPEAT_MONTH_SAME_DAY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user