med-det-samme/app/src/main/java/no/naiv/meddetsamme/data/DoseLog.kt
Ole-Morten Duesund 46a5d7e98e Reminder subsystem: exact alarms, escalation, boot re-arm — verified on emulator
Milestone 4. AlarmScheduler is the only AlarmManager writer; one stable
PendingIntent requestCode per dose-time means re-arming replaces and can
never stack (the no-drift invariant is OS-enforced). DoseAlarmReceiver
handles both occurrence and escalation alarms idempotently and always
re-arms the next occurrence before anything else so the chain can't
break. nagCount lives in dose_log so the ~6-nag cap survives reboot.
SCHEDULE_EXACT_ALARM added maxSdk 32 (USE_EXACT_ALARM is 33+ only).

Emulator-verified (API 35): exact alarm armed (window=0,
policy_permission), fired on time, HIGH notification with Tatt/Utsett,
escalation armed +10 min, next day re-armed, Tatt → TAKEN + inventory
10→9 + escalation cancelled, reboot → BootReceiver re-armed everything.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 13:48:39 +02:00

62 lines
2.2 KiB
Kotlin

package no.naiv.meddetsamme.data
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
enum class DoseStatus { PENDING, TAKEN, SKIPPED, SNOOZED }
/**
* One scheduled occurrence of a dose and what happened to it. Created PENDING
* when the reminder fires (the escalation anchor), resolved by user action.
*
* SNOOZED is a *resting* state between nags — the alarm layer treats it like
* PENDING (keeps nagging after the snooze interval) but the UI can distinguish
* "I saw it and deferred" from "never reacted".
*
* [doseTimeId] is nullable and SET_NULL on delete: editing a schedule away must
* not erase adherence history. [medId] CASCADE instead — deleting a med (vs.
* deactivating) is an explicit "forget everything" action.
*/
@Entity(
tableName = "dose_log",
foreignKeys = [
ForeignKey(
entity = Medication::class,
parentColumns = ["id"],
childColumns = ["medId"],
onDelete = ForeignKey.CASCADE,
),
ForeignKey(
entity = DoseTime::class,
parentColumns = ["id"],
childColumns = ["doseTimeId"],
onDelete = ForeignKey.SET_NULL,
),
],
indices = [
Index("medId"),
Index("doseTimeId"),
// The reminder layer's hot path: "is there already a log for this occurrence?"
Index(value = ["doseTimeId", "scheduledAtMillis"]),
Index("status"),
],
)
data class DoseLog(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val medId: Long,
val doseTimeId: Long?,
/** The occurrence this log is for (epoch millis of the scheduled local time). */
val scheduledAtMillis: Long,
/** Dose size at the time of logging — schedule edits must not rewrite history. */
val amount: Double,
val status: DoseStatus = DoseStatus.PENDING,
/** When the user last acted on it (Taken/Skipped/Snoozed), null while untouched. */
val actionedAtMillis: Long? = null,
/**
* Escalation nags already sent for this occurrence. Lives in the DB, not in
* alarm extras, so the ~6-nag cap survives a reboot mid-escalation.
*/
val nagCount: Int = 0,
)