Reminder logic updates

Check for upcoming reminder and/or if reminder is happening today.
Also - error on the side of showing a reminder
This commit is contained in:
Hafizzle 2023-11-15 07:56:55 -05:00 committed by Phillip Thelen
parent cd8cfaefdd
commit da0938da0f
2 changed files with 43 additions and 36 deletions

View file

@ -83,7 +83,7 @@ class TaskAlarmManager(
private fun setTimeForDailyReminder(remindersItem: RemindersItem?, task: Task): RemindersItem? { private fun setTimeForDailyReminder(remindersItem: RemindersItem?, task: Task): RemindersItem? {
val newTime = task.getNextReminderOccurrence(remindersItem, context) val newTime = task.getNextReminderOccurrence(remindersItem, context)
remindersItem?.time = newTime?.withZoneSameLocal(ZoneId.systemDefault())?.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) remindersItem?.time = newTime?.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
return remindersItem return remindersItem
} }
@ -95,14 +95,13 @@ class TaskAlarmManager(
* which is indicated by first nextDue being null (As the alarm is created before the API returns nextDue times) * which is indicated by first nextDue being null (As the alarm is created before the API returns nextDue times)
*/ */
private fun setAlarmForRemindersItem(reminderItemTask: Task, remindersItem: RemindersItem?) { private fun setAlarmForRemindersItem(reminderItemTask: Task, remindersItem: RemindersItem?) {
if (remindersItem == null) return
val now = ZonedDateTime.now().withZoneSameLocal(ZoneId.systemDefault())?.toInstant() val now = ZonedDateTime.now().withZoneSameLocal(ZoneId.systemDefault())?.toInstant()
val zonedTime = remindersItem?.getLocalZonedDateTimeInstant() val reminderZonedTime = remindersItem.getLocalZonedDateTimeInstant()
if (remindersItem == null ||
(reminderItemTask.type == TaskType.DAILY && zonedTime?.isBefore(now) == true && reminderItemTask.nextDue?.firstOrNull() != null) || if (reminderZonedTime == null || reminderZonedTime.isBefore(now)) return
(reminderItemTask.type == TaskType.TODO && zonedTime?.isBefore(now) == true || zonedTime == null)
) {
return
}
val intent = Intent(context, TaskReceiver::class.java) val intent = Intent(context, TaskReceiver::class.java)
intent.action = remindersItem.id intent.action = remindersItem.id
@ -129,7 +128,7 @@ class TaskAlarmManager(
withImmutableFlag(PendingIntent.FLAG_CANCEL_CURRENT) withImmutableFlag(PendingIntent.FLAG_CANCEL_CURRENT)
) )
setAlarm(context, zonedTime.toEpochMilli(), sender) setAlarm(context, reminderZonedTime.toEpochMilli(), sender)
} }
private fun removeAlarmForRemindersItem(remindersItem: RemindersItem) { private fun removeAlarmForRemindersItem(remindersItem: RemindersItem) {

View file

@ -344,38 +344,46 @@ open class Task : RealmObject, BaseMainObject, Parcelable, BaseTask {
if (remindersItem == null) { if (remindersItem == null) {
return null return null
} }
val reminderTime = remindersItem.time?.parseToZonedDateTime() val reminderTime = remindersItem.time?.parseToZonedDateTime()
val zonedDateTimeNow = ZonedDateTime.now() val zonedDateTimeNow = ZonedDateTime.now()
return if (nextDue?.firstOrNull() != null && (!isDisplayedActive || reminderTime?.isBefore(zonedDateTimeNow) == true)
) { // Check if the reminder is scheduled to repeat today
val repeatingDays = repeat?.dayStrings(context) val repeatingDays = repeat?.dayStrings(context)
val isScheduledForToday = repeatingDays?.find { day -> day == zonedDateTimeNow.dayOfWeekString() } val isScheduledForToday = repeatingDays?.find { day -> day == zonedDateTimeNow.dayOfWeekString() }
if (isScheduledForToday != null) { // Check if reminder time already passed
val currentDateTime = LocalDateTime.now() val isReminderTimePassed = reminderTime?.isBefore(zonedDateTimeNow) == true
val updatedDateTime: LocalDateTime = LocalDateTime.of( if (isScheduledForToday != null && !isReminderTimePassed) {
currentDateTime.year, val currentDateTime = LocalDateTime.now()
currentDateTime.month, val updatedDateTime: LocalDateTime = LocalDateTime.of(
currentDateTime.dayOfMonth, currentDateTime.year,
reminderTime?.hour ?: 0, currentDateTime.month,
reminderTime?.minute ?: 0 currentDateTime.dayOfMonth,
) reminderTime?.hour ?: 0,
updatedDateTime.atZone(ZoneId.systemDefault()) reminderTime?.minute ?: 0
} else { )
val nextDate = LocalDateTime.ofInstant(nextDue?.firstOrNull()?.toInstant(), ZoneId.systemDefault()) return updatedDateTime.atZone(ZoneId.systemDefault())
val updatedDateTime: LocalDateTime = LocalDateTime.of(
nextDate.year,
nextDate.month,
nextDate.dayOfMonth,
reminderTime?.hour ?: 0,
reminderTime?.minute ?: 0
)
updatedDateTime.atZone(ZoneId.systemDefault())
}
} else {
return reminderTime
} }
// If the reminder is not scheduled to repeat today, use the first upcoming nextDue date
val today = LocalDate.now()
// Filter out the dates that are in the past
val futureNextDues = nextDue?.filter { nextDueDate -> nextDueDate.toInstant().atZone(ZonedDateTime.now().zone).toLocalDate().isAfter(today) }
val earliestFutureDate = futureNextDues?.minByOrNull { it }
if (earliestFutureDate != null) {
return earliestFutureDate.toInstant()
.atZone(ZonedDateTime.now().zone)
.withHour(reminderTime?.hour ?: 0)
.withMinute(reminderTime?.minute ?: 0)
}
// If there are no upcoming nextDue dates, use the first upcoming reminder time
return reminderTime
} }
fun parseMarkdown() { fun parseMarkdown() {
parsedText = MarkdownParser.parseMarkdown(text) parsedText = MarkdownParser.parseMarkdown(text)
parsedNotes = MarkdownParser.parseMarkdown(notes) parsedNotes = MarkdownParser.parseMarkdown(notes)