Skip to content

Commit

Permalink
Handle weekly same day reminder that already passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hafizzle committed Jan 1, 2024
1 parent 7ee9b60 commit 1fec8ef
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ open class Task : RealmObject, BaseMainObject, Parcelable, BaseTask {
if (remindersItem == null) return null

val reminderTime = remindersItem.time?.parseToZonedDateTime() ?: return null
val now = ZonedDateTime.now().withZoneSameInstant(ZoneId.systemDefault())
var startDate = this.startDate?.toInstant()?.atZone(ZoneId.systemDefault()) ?: return null
val frequency = this.frequency ?: return null
val everyX = this.everyX ?: 1
Expand Down Expand Up @@ -380,6 +381,16 @@ open class Task : RealmObject, BaseMainObject, Parcelable, BaseTask {
dateTimeOccurenceToSchedule = startDate
} else {
var nextDueDate = dateTimeOccurenceToSchedule.withHour(reminderTime.hour).withMinute(reminderTime.minute)
// If the next due date already happened for today, increment it by one day. Otherwise, it will be scheduled for today.
if (nextDueDate.isBefore(now) && occurrencesList.size == 0) {
nextDueDate = nextDueDate.plusDays(1)
}

// If the reminder being scheduled is not the first iteration of the reminder, increment it by one day
if (occurrencesList.size > 0) {
nextDueDate = nextDueDate.plusDays(1)
}

while (!nextDueDate.matchesRepeatDays(repeatDays)) {
nextDueDate = nextDueDate.plusDays(1).withHour(reminderTime.hour).withMinute(reminderTime.minute)
}
Expand All @@ -394,16 +405,6 @@ open class Task : RealmObject, BaseMainObject, Parcelable, BaseTask {
}
}

// Ensure the next due date is in the future
val now = ZonedDateTime.now().withZoneSameInstant(ZoneId.systemDefault())
if (nextDueDate.isBefore(now)) {
nextDueDate = nextDueDate.plusWeeks(everyX.toLong())
// Find the next due day in the future
while (!nextDueDate.matchesRepeatDays(repeatDays)) {
nextDueDate = nextDueDate.plusDays(1).withHour(reminderTime.hour).withMinute(reminderTime.minute)
}
}

dateTimeOccurenceToSchedule = nextDueDate
}
// Set time to the reminder time
Expand Down

0 comments on commit 1fec8ef

Please sign in to comment.