med-det-samme/app/src/main/java/no/naiv/meddetsamme/alarm/SupplyCheckReceiver.kt

82 lines
3.2 KiB
Kotlin
Raw Normal View History

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()
}
}
}
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 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.
val byItem = db.medicationDao().getActive().groupBy { it.item.id }
for ((_, meds) in byItem) {
val item = meds.first().item
var rate = 0.0
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)
lowStock += "${item.name}: lager for ${days?.toInt() ?: 0} dager"
}
if (ScheduleEngine.needsRenewal(item.rxExpiryEpochDay, today, RX_LEAD_DAYS)) {
val expired = item.rxExpiryEpochDay!! < today.toEpochDay()
rxRenewal += if (expired) "${item.name}: resept utløpt" else "${item.name}: resept må fornyes"
}
}
// 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.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)
}
}
}