Add a prototype nightscout "missed bolus" detector

This commit is contained in:
James Babcock committed 2026-10-12 17:06:38 -07:00
1 parent f6201d8e41
commit 00c36602d6
13 files changed
+997 -61

No files matched your search

+17
View File
@@ -1163,6 +1163,23 @@ export const nightscoutAlwaysShowInTopBarSetting = new ConfigSettingBoolean({
defaultValue: false,
description: "Keep the Nightscout glucose graph and warnings in the top bar even when all Nightscout windows are closed.",
});
export const nightscoutMissedBolusAlertsSetting = new ConfigSettingBoolean({
id: "nightscout-missed-bolus-alerts",
label: "Missed-bolus alerts",
storageKey: "integrations.nightscout.missedBolusAlerts",
defaultValue: false,
description: "Show a notification on the glasses when glucose rises like an unannounced meal with no bolus or carbs entered. Experimental.",
});
export const nightscoutMissedBolusCooldownSetting = new ConfigSettingString({
id: "nightscout-missed-bolus-cooldown-minutes",
label: "Missed-bolus alert cooldown",
storageKey: "integrations.nightscout.missedBolusCooldownMinutes",
defaultValue: "120",
editorTitle: "Missed-bolus alert cooldown (min)",
normalize: normalizeNightscoutThreshold,
formatValue: (value) => `${value} min`,
description: "After a missed-bolus alert, wait at least this many minutes before showing another.",
});
export function loadNightscoutThresholds(): NightscoutThresholds {
return {
+61 -39
View File
@@ -190,22 +190,7 @@ export class NotificationsListLayer implements Layer {
*/
export class SingleNotificationLayer implements Layer {
/** The action column; its items are rebuilt from the live notification on every paint and input. */
private readonly detailMenu = new Menu<DetailMenuItem>({
wrap: false,
rowGap: DETAIL_MENU_ROW_GAP,
highlight: { radius: 6 },
getHeight: (item, width) => {
const font = getDefaultSmallFont();
return detailMenuRowLines(font, item, width).length * lineStep(font) + 8;
},
draw: ({ image, item, x, y, width, selected }) => {
const font = getDefaultSmallFont();
const lines = detailMenuRowLines(font, item, width);
for (let line = 0; line < lines.length; line++) {
image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185);
}
},
});
private readonly detailMenu = createNotificationActionMenu<DetailMenuItem>((item) => item.kind === "disable-source");
private confirmation: { source: AndroidNotification; menu: Menu<ConfirmationItem> } | null = null;
// The last paint, which a tray-hosted layer repeats once its notification
// is gone, until the tray swaps in a neighbour's (see closeUnavailableNotification).
@@ -229,7 +214,7 @@ export class SingleNotificationLayer implements Layer {
this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin));
drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height);
this.detailMenu.paint(image, detailMenuBox(width, height), ctx.stack.isFocused());
this.detailMenu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused());
this.lastImage = image;
return image;
}
@@ -427,20 +412,9 @@ function drawDetailContent(
width: number,
height: number,
): void {
const contentX = DETAIL_CONTENT_X;
const menuX = width - DETAIL_MENU_WIDTH - 24;
const contentWidth = menuX - contentX - 20;
image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220);
// The header row (icon + app name) and the body stack below the title's
// line box, so a large UI font pushes them down instead of overlapping.
const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP;
const headerHeight = Math.max(ICON_SIZE, font.lineHeight);
let appLineX = contentX;
if (icon) {
image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true });
appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP;
}
image.drawText(font, appLineX, centeredTextY(font, headerTop, headerHeight), `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150);
const contentX = NOTIFICATION_CONTENT_X;
const contentWidth = notificationContentWidth(width);
const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`);
const lines: string[] = [];
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
@@ -456,7 +430,6 @@ function drawDetailContent(
}
const step = lineStep(font);
const bodyTop = headerTop + headerHeight + DETAIL_BODY_GAP;
const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0);
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
const line = lines[index]!;
@@ -467,8 +440,63 @@ function drawDetailContent(
}
}
/** Left edge of a notification detail view's content column. */
export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X;
/** Width of a notification detail view's content column, left of the action column. */
export function notificationContentWidth(width: number): number {
return width - DETAIL_MENU_WIDTH - 24 - NOTIFICATION_CONTENT_X - 20;
}
/**
* The top of a notification detail view: the "Notification" title, then the
* source's icon and label. Returns the y where the body starts.
*/
export function drawNotificationHeader(image: GrayImage, font: UiFont, icon: GrayImage | null, sourceLabel: string): number {
const contentX = NOTIFICATION_CONTENT_X;
image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220);
// The header row (icon + app name) and the body stack below the title's
// line box, so a large UI font pushes them down instead of overlapping.
const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP;
const headerHeight = Math.max(ICON_SIZE, font.lineHeight);
let sourceX = contentX;
if (icon) {
image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true });
sourceX = contentX + ICON_SIZE + ICON_TEXT_GAP;
}
image.drawText(font, sourceX, centeredTextY(font, headerTop, headerHeight), sourceLabel, 150);
return headerTop + headerHeight + DETAIL_BODY_GAP;
}
/**
* A notification detail view's right-hand action column. Rows truncate their
* label to one line, except those `wraps` picks, which wrap it.
*/
export function createNotificationActionMenu<T extends { label: string }>(wraps: (item: T) => boolean = () => false): Menu<T> {
const rowLines = (font: UiFont, item: T, rowWidth: number): string[] => {
const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X;
return wraps(item) ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)];
};
return new Menu<T>({
wrap: false,
rowGap: DETAIL_MENU_ROW_GAP,
highlight: { radius: 6 },
getHeight: (item, width) => {
const font = getDefaultSmallFont();
return rowLines(font, item, width).length * lineStep(font) + 8;
},
draw: ({ image, item, x, y, width, selected }) => {
const font = getDefaultSmallFont();
const lines = rowLines(font, item, width);
for (let line = 0; line < lines.length; line++) {
image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185);
}
},
});
}
/** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */
function detailMenuBox(width: number, height: number): MenuBox {
export function notificationActionMenuBox(width: number, height: number): MenuBox {
return {
x: width - DETAIL_MENU_WIDTH - 32,
y: DETAIL_MENU_TOP,
@@ -477,12 +505,6 @@ function detailMenuBox(width: number, height: number): MenuBox {
};
}
/** An action row's text lines: the disable-source row wraps, the rest truncate to one line. */
function detailMenuRowLines(font: UiFont, item: DetailMenuItem, rowWidth: number): string[] {
const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X;
return item.kind === "disable-source" ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)];
}
function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] {
return [
{ kind: "back", label: "Back" },
+36 -6
View File
@@ -248,6 +248,18 @@ export type AssistantActivityEvent = {
export type FocusKind = "sidebar" | "window";
/**
* A notification an app posts itself, rather than one Android posted. It
* opens in the same modal (and waits in the same queue) as Android
* notifications, but is not listed in the Notifications app.
*/
export type AppNotification = {
/** Unique per notification; must not collide with Android notification keys. */
key: string;
/** The modal's content; `close` takes the modal down. */
createLayer: (close: () => void) => Layer;
};
const noopActions: LayerActions = noopLayerActions;
/**
@@ -403,9 +415,12 @@ class Shell {
private readonly trayIcons = new Map<string, GrayImage>();
private activeVoiceLayer: VoiceInputLayer | null = null;
private activeKeyboardLayer: KeyboardInputLayer | null = null;
// App-posted notifications that are open or queued, by key.
private readonly appNotifications = new Map<string, AppNotification>();
private readonly notificationModals = new NotificationModalQueue<ShellModalLayer>({
open: (notificationKey) => this.pushNotificationModal(notificationKey),
isAvailable: (notificationKey) => {
if (this.appNotifications.has(notificationKey)) return true;
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
return !!notification && shouldShowNotificationOnGlasses(notification.packageName);
},
@@ -797,20 +812,35 @@ class Shell {
this.config.requestShellRender();
}
/**
* Show an app's own notification the way new Android notifications are
* shown: in the notification modal, waking the screen for it if it was off.
*/
postAppNotification(notification: AppNotification): void {
this.appNotifications.set(notification.key, notification);
const wokeScreen = this.screenOn ? false : this.wake("sidebar");
this.openNotificationModal(notification.key, wokeScreen);
}
private pushNotificationModal(notificationKey: string): ShellModalLayer {
const appNotification = this.appNotifications.get(notificationKey);
const close = () => this.closeNotificationModal(modal, notificationKey);
const modal: ShellModalLayer = new ShellModalLayer(
new SingleNotificationLayer(notificationKey, {
origin: "new-notification-modal",
onClose: () => this.closeNotificationModal(modal),
}),
appNotification
? appNotification.createLayer(close)
: new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", onClose: close }),
this.config.actions,
() => this.notificationModals.removed(modal, this.screenOn),
() => {
this.appNotifications.delete(notificationKey);
this.notificationModals.removed(modal, this.screenOn);
},
);
this.stack.push(modal);
return modal;
}
private closeNotificationModal(modal: ShellModalLayer): void {
private closeNotificationModal(modal: ShellModalLayer, notificationKey: string): void {
this.appNotifications.delete(notificationKey);
// The queue first: it opens the next notification or puts the screen back
// to sleep, and then ignores the removal below. remove() rather than a
// pop, so a menu opened over the modal doesn't leave it stuck.