package no.naiv.meddetsamme.alarm import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import no.naiv.meddetsamme.domain.DoseActions /** * Handles the notification actions. The dose card groups every med due at the * same wall-clock minute, so its buttons act on the whole occurrence (carried * as EXTRA_SCHEDULED_AT), not a single log — "Tatt alle" / "Utsett alle". * * Contract (CLAUDE.md, load-bearing), applied to each dose in the occurrence: * - Taken → TAKEN, decrement inventory by the *logged* amount, stop escalating. * - Snooze → SNOOZED, next nag in 15 min, keeps escalating (and counting) after. * - Mute → "Ikke forstyrr": stop escalating, dose stays unresolved. * (Skip stays a per-dose UI action; the notification never offers it.) */ class DoseActionReceiver : BroadcastReceiver() { companion object { const val ACTION_TAKEN = "no.naiv.meddetsamme.action.TAKEN" const val ACTION_SNOOZE = "no.naiv.meddetsamme.action.SNOOZE" const val ACTION_MUTE = "no.naiv.meddetsamme.action.MUTE" const val EXTRA_SCHEDULED_AT = "scheduledAt" } override fun onReceive(context: Context, intent: Intent) { val scheduledAt = intent.getLongExtra(EXTRA_SCHEDULED_AT, -1) if (scheduledAt < 0) return val action = intent.action ?: return val pending = goAsync() CoroutineScope(Dispatchers.IO).launch { try { val actions = DoseActions(context) when (action) { ACTION_TAKEN -> actions.takeAll(scheduledAt) ACTION_SNOOZE -> actions.snoozeAll(scheduledAt) ACTION_MUTE -> actions.muteAll(scheduledAt) } } finally { pending.finish() } } } }