Properly handle repeating tasks

This commit is contained in:
Naveen
2023-06-24 18:49:56 +05:30
parent 04cc2b5371
commit 05084ea330
4 changed files with 21 additions and 16 deletions

View File

@@ -1378,14 +1378,7 @@ class EventActivity : SimpleActivity() {
ensureBackgroundThread { ensureBackgroundThread {
val eventId = mEvent.id!! val eventId = mEvent.id!!
val originalEvent = eventsDB.getEventWithId(eventId) ?: return@ensureBackgroundThread val originalEvent = eventsDB.getEventWithId(eventId) ?: return@ensureBackgroundThread
val hasFixedRepeatCount = originalEvent.repeatLimit < 0 && mEvent.repeatLimit < 0 mEvent.maybeAdjustRepeatLimitCount(originalEvent, mEventOccurrenceTS)
val repeatLimitUnchanged = originalEvent.repeatLimit == mEvent.repeatLimit
if (hasFixedRepeatCount && repeatLimitUnchanged) {
val occurrencesSinceStart = (mEventOccurrenceTS - originalEvent.startTS) / originalEvent.repeatInterval
val newRepeatLimit = mEvent.repeatLimit + occurrencesSinceStart
mEvent.repeatLimit = newRepeatLimit
}
mEvent.id = null mEvent.id = null
eventsHelper.apply { eventsHelper.apply {
addEventRepeatLimit(eventId, mEventOccurrenceTS) addEventRepeatLimit(eventId, mEventOccurrenceTS)

View File

@@ -489,16 +489,18 @@ class TaskActivity : SimpleActivity() {
} }
EDIT_FUTURE_OCCURRENCES -> { EDIT_FUTURE_OCCURRENCES -> {
ensureBackgroundThread { ensureBackgroundThread {
eventsHelper.addEventRepeatLimit(mTask.id!!, mTaskOccurrenceTS) val taskId = mTask.id!!
mTask.apply { val originalTask = eventsDB.getTaskWithId(taskId) ?: return@ensureBackgroundThread
id = null mTask.maybeAdjustRepeatLimitCount(originalTask, mTaskOccurrenceTS)
} mTask.id = null
eventsHelper.apply {
eventsHelper.insertTask(mTask, showToasts = true) { addEventRepeatLimit(taskId, mTaskOccurrenceTS)
insertTask(mTask, showToasts = true) {
finish() finish()
} }
} }
} }
}
EDIT_ALL_OCCURRENCES -> { EDIT_ALL_OCCURRENCES -> {
ensureBackgroundThread { ensureBackgroundThread {
// Shift the start and end times of the first (original) event based on the changes made // Shift the start and end times of the first (original) event based on the changes made

View File

@@ -29,3 +29,13 @@ fun Event.toUtcAllDayEvent() {
startTS = Formatter.getShiftedUtcTS(startTS) startTS = Formatter.getShiftedUtcTS(startTS)
endTS = Formatter.getShiftedUtcTS(endTS) endTS = Formatter.getShiftedUtcTS(endTS)
} }
fun Event.maybeAdjustRepeatLimitCount(original: Event, occurrenceTS: Long) {
val hasFixedRepeatCount = original.repeatLimit < 0 && repeatLimit < 0
val repeatLimitUnchanged = original.repeatLimit == repeatLimit
if (hasFixedRepeatCount && repeatLimitUnchanged) {
val occurrencesSinceStart = (occurrenceTS - original.startTS) / original.repeatInterval
val newRepeatLimit = repeatLimit + occurrencesSinceStart
this.repeatLimit = newRepeatLimit
}
}

View File

@@ -226,7 +226,7 @@ class EventsHelper(val context: Context) {
} }
fun addEventRepeatLimit(eventId: Long, occurrenceTS: Long) { fun addEventRepeatLimit(eventId: Long, occurrenceTS: Long) {
val event = eventsDB.getEventWithId(eventId) ?: return val event = eventsDB.getEventOrTaskWithId(eventId) ?: return
val previousOccurrenceTS = occurrenceTS - event.repeatInterval // always update repeat limit of the occurrence preceding the one being edited val previousOccurrenceTS = occurrenceTS - event.repeatInterval // always update repeat limit of the occurrence preceding the one being edited
val repeatLimitDateTime = Formatter.getDateTimeFromTS(previousOccurrenceTS).withTimeAtStartOfDay() val repeatLimitDateTime = Formatter.getDateTimeFromTS(previousOccurrenceTS).withTimeAtStartOfDay()
val repeatLimitTS = if (event.getIsAllDay()) { val repeatLimitTS = if (event.getIsAllDay()) {