Season end nudge: post once per close, not daily for the lead window

SupplyCheckReceiver re-posted the auto-cancel "Forleng" card on every
daily check for the whole 5-day window, so a dismissed nudge came back
each morning. Remember the season-end epoch day nudged per med (plaintext
pref) and skip re-posting for the same close; a new end date (next year,
or after Forleng) still earns one fresh nudge.
This commit is contained in:
Ole-Morten Duesund 2026-08-17 12:35:20 +02:00
commit 4bbb06d5d0
2 changed files with 26 additions and 9 deletions

View file

@ -12,6 +12,7 @@ import no.naiv.meddetsamme.data.MedDatabase
import no.naiv.meddetsamme.domain.ScheduleEngine
import no.naiv.meddetsamme.domain.ScheduleText
import no.naiv.meddetsamme.notify.Notifications
import no.naiv.meddetsamme.settings.SettingsStore
/**
* Daily derived-state check: low stock (inventory ÷ scheduled rate) and
@ -96,7 +97,7 @@ class SupplyCheckReceiver : BroadcastReceiver() {
/**
* Per-med seasonal nudge, post-or-cancel like the digests above:
* - resting and within [SEASON_START_LEAD_DAYS] of the next window "Start nå";
* - in season and within [SEASON_END_LEAD_DAYS] of its close "Forleng".
* - in season and within [SEASON_END_LEAD_DAYS] of its close "Forleng" (once per close).
* Acting on either (or the season simply passing) clears it the next day.
*/
private fun checkSeason(
@ -130,14 +131,20 @@ class SupplyCheckReceiver : BroadcastReceiver() {
val end = ScheduleEngine.currentSeasonEnd(doseTimes, today)
val days = end?.let { ChronoUnit.DAYS.between(today, it) }
if (end != null && days != null && days in 0..SEASON_END_LEAD_DAYS) {
Notifications.post(
context, notifId,
Notifications.buildSeasonNotification(
context, medId, name,
"Sesongen er snart over (${ScheduleText.dateLabel(end)}). Fortsatt plaget? Forleng to uker.",
starting = false,
),
)
// Once per season close, not daily: a dismissed card stays dismissed.
// Keyed on the end date, so "Forleng" (new end) earns one fresh nudge.
val settings = SettingsStore(context)
if (settings.seasonEndNudgedEpochDay(medId) != end.toEpochDay()) {
Notifications.post(
context, notifId,
Notifications.buildSeasonNotification(
context, medId, name,
"Sesongen er snart over (${ScheduleText.dateLabel(end)}). Fortsatt plaget? Forleng to uker.",
starting = false,
),
)
settings.setSeasonEndNudgedEpochDay(medId, end.toEpochDay())
}
} else {
Notifications.cancel(context, notifId)
}