forked from Blendman974/faceclaw
Add health metrics widget to Glanceboard
This commit is contained in:
1 parent
27a5fe47ef
commit
3a61ad3842
9 files changed
+161
-3
No files matched your search
@@ -5,7 +5,7 @@ import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export type GlanceSlotChoice = GlanceWidgetId | "none";
|
||||
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "nightscout", "compass", "music"];
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "nightscout", "compass", "music", "health"];
|
||||
|
||||
const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||
none: "Empty",
|
||||
@@ -15,6 +15,7 @@ const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||
nightscout: "Nightscout",
|
||||
compass: "Compass",
|
||||
music: "Music",
|
||||
health: "Health",
|
||||
};
|
||||
|
||||
export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface GlanceWidget {
|
||||
paint(image: GrayImage): void;
|
||||
}
|
||||
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal";
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal" | "health";
|
||||
|
||||
export type GlanceWidgetDefinition = {
|
||||
id: GlanceWidgetId;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { dailySummary, nextBucketStart } from "../../../health/health-derive";
|
||||
import { drawHealthCard, type HealthCardData } from "../../../health/health-card";
|
||||
import { syncLiveRecords } from "../../../health/health-live";
|
||||
import { isFixtureData } from "../../../health/health-seed";
|
||||
import { healthStore } from "../../../health/health-store-files";
|
||||
import { startOfLocalDay } from "../../../health/health-types";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
/** Saved daily metrics, refreshed while visible without extra BLE polling. */
|
||||
export class HealthWidget implements GlanceWidget {
|
||||
private data: HealthCardData = { kind: "empty" };
|
||||
private timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.stop();
|
||||
const refresh = (): void => {
|
||||
this.refresh();
|
||||
requestRender();
|
||||
};
|
||||
refresh();
|
||||
this.timer = setInterval(refresh, 60_000);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.timer !== null) clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
private refresh(): void {
|
||||
try {
|
||||
syncLiveRecords();
|
||||
if (isFixtureData()) {
|
||||
this.data = { kind: "fixture" };
|
||||
return;
|
||||
}
|
||||
const today = startOfLocalDay(Date.now());
|
||||
const store = healthStore();
|
||||
const samples = store.samplesInRange(today, nextBucketStart(today, "day"));
|
||||
const summary = dailySummary(samples, store.sleepSessions(), today);
|
||||
this.data = samples.length || summary.sleep
|
||||
? { kind: "live", summary,
|
||||
hasSteps: samples.some((sample) => sample.metric === "steps"),
|
||||
hasCalories: samples.some((sample) => sample.metric === "calories") }
|
||||
: { kind: "empty" };
|
||||
} catch (error) {
|
||||
console.warn("health widget refresh failed", error);
|
||||
this.data = { kind: "error" };
|
||||
}
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
drawHealthCard(image, this.data, getDefaultSmallFont(), getDefaultMediumFont());
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { MusicWidget } from "./music-widget";
|
||||
import { NightscoutWidget } from "./nightscout-widget";
|
||||
import { SystemCardWidget } from "./system-card";
|
||||
import { TerminalWidget } from "./terminal-widget";
|
||||
import { HealthWidget } from "./health-widget";
|
||||
|
||||
/** Every widget a board slot can hold. */
|
||||
export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
@@ -14,6 +15,7 @@ export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||
{ id: "health", label: "Health", create: () => new HealthWidget() },
|
||||
];
|
||||
|
||||
export function findGlanceWidget(id: GlanceWidgetId | string): GlanceWidgetDefinition | null {
|
||||
|
||||
Reference in new issue
Block a user