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

@ -26,6 +26,7 @@ object Notifications {
fun doseNotificationId(logId: Long): Int = (logId % Int.MAX_VALUE).toInt()
const val SUPPLY_NOTIFICATION_ID = 1_000_000_007
const val RX_NOTIFICATION_ID = 1_000_000_009
private val timeFmt = DateTimeFormatter.ofPattern("HH:mm")
@ -98,22 +99,41 @@ object Notifications {
.build()
}
fun buildSupplyNotification(context: Context, lines: List<String>): Notification {
/** Morning low-stock digest: plainly dismissable, re-posted daily while low. */
fun buildLowStockNotification(context: Context, lines: List<String>): Notification =
supplyDigestBuilder(context, SUPPLY_NOTIFICATION_ID, lines)
.setContentTitle(if (lines.size == 1) lines.first() else "Lite på lager (${lines.size})")
.setAutoCancel(true)
.build()
/**
* Rx-renewal digest: setOngoing makes it survive "Clear all" the
* "persistent but dismissable" contract (deliberate swipe still works on
* Android 14+, and the daily check re-posts it until the rx is renewed).
*/
fun buildRxNotification(context: Context, lines: List<String>): Notification =
supplyDigestBuilder(context, RX_NOTIFICATION_ID, lines)
.setContentTitle(if (lines.size == 1) lines.first() else "Resepter må fornyes (${lines.size})")
.setOngoing(true)
.build()
private fun supplyDigestBuilder(
context: Context,
requestCode: Int,
lines: List<String>,
): NotificationCompat.Builder {
val contentIntent = PendingIntent.getActivity(
context,
SUPPLY_NOTIFICATION_ID,
requestCode,
Intent(context, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
return NotificationCompat.Builder(context, CHANNEL_SUPPLY)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(if (lines.size == 1) lines.first() else "Apotek-ærend (${lines.size})")
.setContentText(lines.joinToString(" · "))
.setStyle(NotificationCompat.InboxStyle().also { s -> lines.forEach(s::addLine) })
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build()
}
/** Post if allowed; silently skipping is fine — channel-level blocks are user intent. */