From 05084ea3307a53ecedaaf74d6d50fd08af8c896a Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 24 Jun 2023 18:49:56 +0530 Subject: [PATCH] Properly handle repeating tasks --- .../calendar/pro/activities/EventActivity.kt | 9 +-------- .../calendar/pro/activities/TaskActivity.kt | 16 +++++++++------- .../calendar/pro/extensions/Event.kt | 10 ++++++++++ .../calendar/pro/helpers/EventsHelper.kt | 2 +- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 286600d38..9c3d9d8ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -1378,14 +1378,7 @@ class EventActivity : SimpleActivity() { ensureBackgroundThread { val eventId = mEvent.id!! val originalEvent = eventsDB.getEventWithId(eventId) ?: return@ensureBackgroundThread - val hasFixedRepeatCount = originalEvent.repeatLimit < 0 && mEvent.repeatLimit < 0 - 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.maybeAdjustRepeatLimitCount(originalEvent, mEventOccurrenceTS) mEvent.id = null eventsHelper.apply { addEventRepeatLimit(eventId, mEventOccurrenceTS) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt index ead2286d4..cf1e94685 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt @@ -489,13 +489,15 @@ class TaskActivity : SimpleActivity() { } EDIT_FUTURE_OCCURRENCES -> { ensureBackgroundThread { - eventsHelper.addEventRepeatLimit(mTask.id!!, mTaskOccurrenceTS) - mTask.apply { - id = null - } - - eventsHelper.insertTask(mTask, showToasts = true) { - finish() + val taskId = mTask.id!! + val originalTask = eventsDB.getTaskWithId(taskId) ?: return@ensureBackgroundThread + mTask.maybeAdjustRepeatLimitCount(originalTask, mTaskOccurrenceTS) + mTask.id = null + eventsHelper.apply { + addEventRepeatLimit(taskId, mTaskOccurrenceTS) + insertTask(mTask, showToasts = true) { + finish() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Event.kt index e9d2c8847..51f7f10bb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Event.kt @@ -29,3 +29,13 @@ fun Event.toUtcAllDayEvent() { startTS = Formatter.getShiftedUtcTS(startTS) 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 + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index d09fac49c..92567695f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -226,7 +226,7 @@ class EventsHelper(val context: Context) { } 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 repeatLimitDateTime = Formatter.getDateTimeFromTS(previousOccurrenceTS).withTimeAtStartOfDay() val repeatLimitTS = if (event.getIsAllDay()) {