import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { renderIcon } from "../../graphics/icons"; import { wrapText } from "../../graphics/textwrap"; import { formatRelativeTime } from "../../util/date-util"; import { nightscoutBridge } from "../../native/nightscout-bridge"; import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import { createNotificationActionMenu, drawNotificationHeader, notificationDetailLayout, } from "../../ui/notifications"; import { type GlucoseRise } from "./missed-bolus-detector"; import { drawDirectionIndicator, drawNightscoutGraph, drawNightscoutValueStrikeThrough, formatDelta, isNightscoutPointStale, } from "./nightscout"; const ICON_SIZE = 24; const GRAPH_GAP = 6; const MIN_GRAPH_HEIGHT = 32; type ActionItem = { label: string; run: () => void }; /** * The missed-bolus alert, laid out like an Android notification's detail view * (header, text, right-hand action column), with the live glucose readout and * 2-hour graph below the text. */ export class MissedBolusNotificationLayer implements Layer { private readonly menu = createNotificationActionMenu(); constructor( private readonly rise: GlucoseRise, private readonly postedAtMs: number, private readonly close: () => void, openNightscout: () => void, ) { this.menu.setItems([ { label: "Dismiss", run: close }, { label: "Open Nightscout", run: () => { close(); openNightscout(); } }, ]); } paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); const step = lineStep(font); const viewport = ctx.stack.getBaseSize(); const image = new GrayImage(viewport.width, viewport.height, 0); // The same columns as a notification's detail view, inside the padding // suggested for the edges of whatever hosts this (the shell's modal). const layout = notificationDetailLayout(viewport); const { contentX, contentWidth } = layout; const nowMs = Date.now(); let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`, layout); image.drawText(font, contentX, y, "Missed bolus?", 230); y += step; const minutes = Math.round((this.rise.latestMs - this.rise.onsetMs) / 60_000); const body = `Glucose rose ${this.rise.riseMgdl} mg/dL in ${minutes} min with no bolus or carbs entered.`; for (const line of wrapText(font, body, contentWidth)) { image.drawText(font, contentX, y, line, 190); y += step; } // Live readout and graph: the modal repaints with each shell render, so // these follow new readings (and a bolus, once entered) while it is up. const nightscout = nightscoutBridge.snapshot(); const latest = nightscout.latest; if (latest) { const value = `${latest.sgv}`; image.drawText(font, contentX, y, value, 230); if (isNightscoutPointStale(latest, nowMs)) { drawNightscoutValueStrikeThrough(image, contentX, y + (font.lineHeight >> 1), font.measureText(value)); } let x = contentX + font.measureText(`${value} `); x += drawDirectionIndicator(image, font, x, y, nightscout.direction, 200); x += font.measureText(" "); const iob = nightscout.iob === null ? "" : ` IOB ${nightscout.iob.toFixed(2)}`; image.drawText(font, x, y, `${formatDelta(nightscout.delta)}${iob}`, 180); y += step; } const graphTop = y + GRAPH_GAP; const graphHeight = layout.bottom - graphTop; if (graphHeight >= MIN_GRAPH_HEIGHT) { drawNightscoutGraph(image, { x: contentX, y: graphTop, width: contentWidth, height: graphHeight }, nightscout, nowMs, font); } this.menu.paint(image, layout.menuBox, ctx.stack.isFocused()); return image; } async handleInput(event: InputEvent): Promise { if (event.type === "double-click") { this.close(); } else if (event.type === "click") { this.menu.selectedItem?.run(); } else if (event.type === "scroll-up" || event.type === "scroll-down") { await this.menu.handleInput(event); } } }