mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-21 14:10:51 +01:00
simplify repetition exception handling, CalDAV synced too
This commit is contained in:
parent
5b0076302d
commit
3b6f06db8b
@ -680,7 +680,7 @@ class EventActivity : SimpleActivity() {
|
||||
DeleteEventDialog(this, arrayListOf(mEvent.id!!), mEvent.repeatInterval > 0) {
|
||||
Thread {
|
||||
when (it) {
|
||||
DELETE_SELECTED_OCCURRENCE -> eventsHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
|
||||
DELETE_SELECTED_OCCURRENCE -> eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true)
|
||||
DELETE_FUTURE_OCCURRENCES -> eventsHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS)
|
||||
DELETE_ALL_OCCURRENCES -> eventsHelper.deleteEvent(mEvent.id!!, true)
|
||||
}
|
||||
@ -817,7 +817,7 @@ class EventActivity : SimpleActivity() {
|
||||
}.start()
|
||||
} else {
|
||||
Thread {
|
||||
eventsHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
|
||||
eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true)
|
||||
mEvent.apply {
|
||||
parentId = id!!.toLong()
|
||||
id = null
|
||||
|
@ -440,7 +440,7 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Long>, ac
|
||||
when (action) {
|
||||
DELETE_SELECTED_OCCURRENCE -> {
|
||||
eventIds.forEachIndexed { index, value ->
|
||||
eventsHelper.addEventRepeatException(value, timestamps[index], true)
|
||||
eventsHelper.addEventRepetitionException(value, timestamps[index], true)
|
||||
}
|
||||
}
|
||||
DELETE_FUTURE_OCCURRENCES -> {
|
||||
|
@ -213,6 +213,18 @@ class CalDAVHelper(val context: Context) {
|
||||
}
|
||||
|
||||
fetchedEventIds.add(importId)
|
||||
|
||||
// if the event is an exception from another events repeat rule, find the original parent event
|
||||
if (originalInstanceTime != 0L) {
|
||||
val parentImportId = "$source-$originalId"
|
||||
val parentEventId = context.eventsDB.getEventIdWithImportId(parentImportId)
|
||||
if (parentEventId != null) {
|
||||
event.parentId = parentEventId
|
||||
eventsHelper.addEventRepetitionException(parentEventId, originalInstanceTime / 1000L, false)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if (importIdsMap.containsKey(event.importId)) {
|
||||
val existingEvent = importIdsMap[importId]
|
||||
val originalEventId = existingEvent!!.id
|
||||
@ -221,6 +233,7 @@ class CalDAVHelper(val context: Context) {
|
||||
this.id = null
|
||||
color = 0
|
||||
lastUpdated = 0L
|
||||
repetitionExceptions = ArrayList()
|
||||
}
|
||||
|
||||
if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) {
|
||||
@ -232,16 +245,6 @@ class CalDAVHelper(val context: Context) {
|
||||
importIdsMap[event.importId] = event
|
||||
eventsHelper.insertEvent(null, event, false)
|
||||
}
|
||||
|
||||
// if the event is an exception from another events repeat rule, find the original parent event
|
||||
if (originalInstanceTime != 0L) {
|
||||
val parentImportId = "$source-$originalId"
|
||||
val parentEventId = context.eventsDB.getEventIdWithImportId(parentImportId)
|
||||
if (parentEventId != null) {
|
||||
event.parentId = parentEventId
|
||||
eventsHelper.addEventRepeatException(parentEventId, originalInstanceTime / 1000L, false, event.importId)
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
|
@ -186,39 +186,18 @@ class EventsHelper(val context: Context) {
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun addEventRepeatException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String? = null) {
|
||||
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId)
|
||||
}
|
||||
fun addEventRepetitionException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean) {
|
||||
Thread {
|
||||
val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread
|
||||
val parentEventRepetitionExceptions = parentEvent.repetitionExceptions
|
||||
parentEventRepetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS))
|
||||
eventsDB.updateEventRepetitionExceptions(parentEventRepetitionExceptions, parentEventId)
|
||||
context.scheduleNextEventReminder(parentEvent)
|
||||
|
||||
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String?) {
|
||||
val childEvent = eventsDB.getEventWithId(parentEventId) ?: return
|
||||
|
||||
childEvent.apply {
|
||||
id = null
|
||||
parentId = parentEventId
|
||||
startTS = 0
|
||||
endTS = 0
|
||||
if (childImportId != null) {
|
||||
importId = childImportId
|
||||
if (addToCalDAV && config.caldavSync) {
|
||||
context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS)
|
||||
}
|
||||
}
|
||||
|
||||
insertEvent(null, childEvent, false) {
|
||||
val childEventId = it
|
||||
Thread {
|
||||
val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread
|
||||
val parentEventRepetitionExceptions = parentEvent.repetitionExceptions
|
||||
parentEventRepetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS))
|
||||
eventsDB.updateEventRepetitionExceptions(parentEventRepetitionExceptions, parentEventId)
|
||||
context.scheduleNextEventReminder(parentEvent)
|
||||
|
||||
if (addToCalDAV && config.caldavSync) {
|
||||
val newId = context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS)
|
||||
val newImportId = "${parentEvent.source}-$newId"
|
||||
eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId)
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun getEvents(fromTS: Long, toTS: Long, eventId: Long = -1L, applyTypeFilter: Boolean = true, callback: (events: ArrayList<Event>) -> Unit) {
|
||||
|
@ -162,7 +162,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
} else {
|
||||
eventsHelper.insertEvent(activity, event, true) {
|
||||
for (exceptionTS in curRepeatExceptions) {
|
||||
eventsHelper.addEventRepeatException(it, exceptionTS, true)
|
||||
eventsHelper.addEventRepetitionException(it, exceptionTS, true)
|
||||
}
|
||||
existingEvents.add(event)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user