diff --git a/android/src/main/java/com/timesafari/dailynotification/NotifyReceiver.kt b/android/src/main/java/com/timesafari/dailynotification/NotifyReceiver.kt index 3b8c738..429bdbf 100644 --- a/android/src/main/java/com/timesafari/dailynotification/NotifyReceiver.kt +++ b/android/src/main/java/com/timesafari/dailynotification/NotifyReceiver.kt @@ -399,10 +399,21 @@ class NotifyReceiver : BroadcastReceiver() { // Update database schedule with new nextRunAt so getNotificationStatus() returns correct value // This is critical for rollover scenarios where the UI needs to show the updated time + // Strategy: Find existing enabled notify schedule and update it (there should only be one) + // This ensures getNotificationStatus() finds the updated schedule, not a stale one try { runBlocking { val db = DailyNotificationDatabase.getDatabase(context) - val existingSchedule = db.scheduleDao().getById(stableScheduleId) + + // First, try to find schedule by the provided stableScheduleId + var scheduleToUpdate = db.scheduleDao().getById(stableScheduleId) + + // If not found by ID, find the existing enabled notify schedule (for rollover scenarios) + // getNotificationStatus() finds schedules with kind="notify" && enabled=true + if (scheduleToUpdate == null) { + val allSchedules = db.scheduleDao().getAll() + scheduleToUpdate = allSchedules.firstOrNull { it.kind == "notify" && it.enabled } + } // Calculate cron expression from trigger time (HH:mm format) val calendar = java.util.Calendar.getInstance().apply { @@ -413,14 +424,13 @@ class NotifyReceiver : BroadcastReceiver() { val cronExpression = "${minute} ${hour} * * *" val clockTime = String.format("%02d:%02d", hour, minute) - if (existingSchedule != null) { + if (scheduleToUpdate != null) { // Update existing schedule with new nextRunAt - val currentTime = System.currentTimeMillis() - db.scheduleDao().updateRunTimes(stableScheduleId, existingSchedule.lastRunAt, triggerAtMillis) - Log.d(SCHEDULE_TAG, "Updated schedule in database: id=$stableScheduleId, nextRunAt=$triggerAtMillis") + // Use the existing schedule's ID (not stableScheduleId) to ensure we update the right one + db.scheduleDao().updateRunTimes(scheduleToUpdate.id, scheduleToUpdate.lastRunAt, triggerAtMillis) + Log.d(SCHEDULE_TAG, "Updated schedule in database: id=${scheduleToUpdate.id}, nextRunAt=$triggerAtMillis (rollover)") } else { - // Create new schedule entry for rollover scenarios - // This ensures getNotificationStatus() can find the schedule + // No existing schedule found - create new one (shouldn't happen in normal flow) val newSchedule = Schedule( id = stableScheduleId, kind = "notify", @@ -434,7 +444,7 @@ class NotifyReceiver : BroadcastReceiver() { stateJson = null ) db.scheduleDao().upsert(newSchedule) - Log.d(SCHEDULE_TAG, "Created schedule in database: id=$stableScheduleId, nextRunAt=$triggerAtMillis") + Log.d(SCHEDULE_TAG, "Created new schedule in database: id=$stableScheduleId, nextRunAt=$triggerAtMillis") } } } catch (e: Exception) {