2026-06-10 13:48:39 +02:00
|
|
|
package no.naiv.meddetsamme.alarm
|
|
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import java.time.LocalDate
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import no.naiv.meddetsamme.data.MedDatabase
|
|
|
|
|
import no.naiv.meddetsamme.domain.ScheduleEngine
|
|
|
|
|
import no.naiv.meddetsamme.notify.Notifications
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Daily derived-state check: low stock (inventory ÷ scheduled rate) and
|
|
|
|
|
* prescription renewal (rx expiry, independent of stock — decision #9).
|
|
|
|
|
* One digest notification, not one per med.
|
|
|
|
|
*/
|
|
|
|
|
class SupplyCheckReceiver : BroadcastReceiver() {
|
|
|
|
|
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
|
val pending = goAsync()
|
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
|
try {
|
|
|
|
|
check(context)
|
|
|
|
|
} finally {
|
|
|
|
|
AlarmScheduler(context).armSupplyCheck() // chain the next daily check
|
|
|
|
|
pending.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
private companion object {
|
|
|
|
|
/** "About a week in advance" for prescription renewal. */
|
|
|
|
|
const val RX_LEAD_DAYS = 7
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 13:48:39 +02:00
|
|
|
private suspend fun check(context: Context) {
|
|
|
|
|
val db = MedDatabase.get(context)
|
|
|
|
|
val today = LocalDate.now()
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
val lowStock = mutableListOf<String>()
|
|
|
|
|
val rxRenewal = mutableListOf<String>()
|
2026-06-10 13:48:39 +02:00
|
|
|
|
2026-06-10 15:13:28 +02:00
|
|
|
// Per inventory item: consumption is the SUM across all active regimens
|
|
|
|
|
// drawing from it — one shared box drains faster than either alone.
|
|
|
|
|
val byItem = db.medicationDao().getActive().groupBy { it.item.id }
|
|
|
|
|
for ((_, meds) in byItem) {
|
|
|
|
|
val item = meds.first().item
|
|
|
|
|
var rate = 0.0
|
2026-06-11 22:09:51 +02:00
|
|
|
for (med in meds) rate += ScheduleEngine.dailyConsumption(db.doseTimeDao().getForMed(med.med.id), today)
|
2026-06-10 15:13:28 +02:00
|
|
|
if (ScheduleEngine.needsRefill(item.stockUnits, rate, item.lowStockLeadDays)) {
|
|
|
|
|
val days = ScheduleEngine.daysOfSupply(item.stockUnits, rate)
|
2026-06-11 17:09:07 +02:00
|
|
|
lowStock += "${item.name}: lager for ${no.naiv.meddetsamme.domain.ScheduleText.daysCount(days?.toInt() ?: 0)}"
|
2026-06-10 13:48:39 +02:00
|
|
|
}
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
if (ScheduleEngine.needsRenewal(item.rxExpiryEpochDay, today, RX_LEAD_DAYS)) {
|
2026-06-10 15:13:28 +02:00
|
|
|
val expired = item.rxExpiryEpochDay!! < today.toEpochDay()
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
rxRenewal += if (expired) "${item.name}: resept utløpt" else "${item.name}: resept må fornyes"
|
2026-06-10 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
// Post-or-cancel: cancelling when the condition clears is what lets the
|
|
|
|
|
// ongoing rx notification disappear by itself after a renewal.
|
|
|
|
|
if (lowStock.isNotEmpty()) {
|
2026-06-10 13:48:39 +02:00
|
|
|
Notifications.post(
|
|
|
|
|
context,
|
|
|
|
|
Notifications.SUPPLY_NOTIFICATION_ID,
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
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),
|
2026-06-10 13:48:39 +02:00
|
|
|
)
|
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>
2026-06-11 16:21:25 +02:00
|
|
|
} else {
|
|
|
|
|
Notifications.cancel(context, Notifications.RX_NOTIFICATION_ID)
|
2026-06-10 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|