Rx-expiry ongoing alert (7-day lead), configurable morning check, Felleskatalogen links

The daily digest splits in two: low stock stays an auto-cancel
notification re-posted each morning while below the per-item limit;
prescription renewal becomes a separate ONGOING notification starting
7 days before rx expiry — survives 'Clear all', swipe-dismissable on
Android 14+, returns each morning until the rx date is updated, and
cancels itself once renewed (post-or-cancel in SupplyCheckReceiver).
Check time is now configurable (Innstillinger → Varsler, M3 TimePicker,
default 10:00); same PendingIntent so re-arming replaces. 'Slå opp i
Felleskatalogen' buttons on the item editor and the med editor's
summary card open the browser — decision #8 clarified: the ban covers
Felleskatalogen as a data source, not human-facing links.

Emulator-verified: both notifications post with correct flags
(ONGOING_EVENT vs AUTO_CANCEL), auto-clear after renewal/restock,
alarm follows the configured time (10:00 → 07:30), FK button opens
Chrome.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-06-11 16:21:25 +02:00
commit 608d84236b
9 changed files with 142 additions and 15 deletions

View file

@ -30,10 +30,16 @@ class SupplyCheckReceiver : BroadcastReceiver() {
}
}
private companion object {
/** "About a week in advance" for prescription renewal. */
const val RX_LEAD_DAYS = 7
}
private suspend fun check(context: Context) {
val db = MedDatabase.get(context)
val today = LocalDate.now()
val lines = mutableListOf<String>()
val lowStock = mutableListOf<String>()
val rxRenewal = mutableListOf<String>()
// Per inventory item: consumption is the SUM across all active regimens
// drawing from it — one shared box drains faster than either alone.
@ -44,20 +50,33 @@ class SupplyCheckReceiver : BroadcastReceiver() {
for (med in meds) rate += ScheduleEngine.dailyConsumption(db.doseTimeDao().getForMed(med.med.id))
if (ScheduleEngine.needsRefill(item.stockUnits, rate, item.lowStockLeadDays)) {
val days = ScheduleEngine.daysOfSupply(item.stockUnits, rate)
lines += "${item.name}: lager for ${days?.toInt() ?: 0} dager"
lowStock += "${item.name}: lager for ${days?.toInt() ?: 0} dager"
}
if (ScheduleEngine.needsRenewal(item.rxExpiryEpochDay, today)) {
if (ScheduleEngine.needsRenewal(item.rxExpiryEpochDay, today, RX_LEAD_DAYS)) {
val expired = item.rxExpiryEpochDay!! < today.toEpochDay()
lines += if (expired) "${item.name}: resept utløpt" else "${item.name}: resept må fornyes"
rxRenewal += if (expired) "${item.name}: resept utløpt" else "${item.name}: resept må fornyes"
}
}
if (lines.isNotEmpty()) {
// Post-or-cancel: cancelling when the condition clears is what lets the
// ongoing rx notification disappear by itself after a renewal.
if (lowStock.isNotEmpty()) {
Notifications.post(
context,
Notifications.SUPPLY_NOTIFICATION_ID,
Notifications.buildSupplyNotification(context, lines),
Notifications.buildLowStockNotification(context, lowStock),
)
} else {
Notifications.cancel(context, Notifications.SUPPLY_NOTIFICATION_ID)
}
if (rxRenewal.isNotEmpty()) {
Notifications.post(
context,
Notifications.RX_NOTIFICATION_ID,
Notifications.buildRxNotification(context, rxRenewal),
)
} else {
Notifications.cancel(context, Notifications.RX_NOTIFICATION_ID)
}
}
}