diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/TaskAlarmManager.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/TaskAlarmManager.kt index e5aa79806..a4572bee3 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/TaskAlarmManager.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/TaskAlarmManager.kt @@ -75,10 +75,9 @@ class TaskAlarmManager( } private fun setTimeForDailyReminder(remindersItem: RemindersItem?, task: Task): RemindersItem? { - val oldTime = remindersItem?.time - val newTime = (task.getNextReminderOccurence(oldTime) ?: return null) + val newTime = (remindersItem?.let { task.getNextReminderOccurrence(it) } ?: return null) - remindersItem?.time = newTime.withZoneSameLocal(ZoneId.systemDefault()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + remindersItem.time = newTime.withZoneSameLocal(ZoneId.systemDefault()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) return remindersItem } diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/models/tasks/Task.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/models/tasks/Task.kt index 0a2dbe487..8481ceb65 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/models/tasks/Task.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/models/tasks/Task.kt @@ -202,24 +202,26 @@ open class Task : RealmObject, BaseMainObject, Parcelable, BaseTask { fun checkIfDue(): Boolean = isDue == true - fun getNextReminderOccurence(oldTime: String?): ZonedDateTime? { - if (oldTime == null) { - return null - } - val nextDate = nextDue?.firstOrNull() + fun getNextReminderOccurrence(remindersItem: RemindersItem): ZonedDateTime? { + remindersItem.time?.let { + val oldTime = it + val now = ZonedDateTime.now().withZoneSameLocal(ZoneId.systemDefault())?.toInstant() + val nextDate = nextDue?.firstOrNull() - return if (nextDate != null && !isDisplayedActive) { - val nextDueCalendar = GregorianCalendar() - nextDueCalendar.time = nextDate - parse(oldTime) - ?.withYear(nextDueCalendar.get(Calendar.YEAR)) - ?.withMonth(nextDueCalendar.get(Calendar.MONTH)) - ?.withDayOfMonth(nextDueCalendar.get(Calendar.DAY_OF_MONTH)) - } else if (isDisplayedActive) { - parse(oldTime) - } else { - null + //If task !isDisplayedActive or if isDisplayedActive but reminder passed, + //set a updated reminder with nextDate + return if (nextDate != null && (!isDisplayedActive || remindersItem.getLocalZonedDateTimeInstant()?.isBefore(now) == true)) { + val nextDueCalendar = GregorianCalendar() + nextDueCalendar.time = nextDate + parse(oldTime) + ?.withYear(nextDueCalendar.get(Calendar.YEAR)) + ?.withMonth(nextDueCalendar.get(Calendar.MONTH) + 1) //+1 to handle Gregorian Calendar month range from 0-11 + ?.withDayOfMonth(nextDueCalendar.get(Calendar.DAY_OF_MONTH)) + } else { + return parse(oldTime) + } } + return null } fun formatter(): DateTimeFormatter =