# Focus timer

Start a 25 or 50 minute focus session from the widget. The countdown ticks on the phone, and a real timer rings at the end.

**Sizes:** small (the headline or the countdown), medium and large (with buttons)
**Interaction:** buttons, answered by a listener; a phone `timer` action (needs `alarms` in `can`)
**Gotcha:** when the countdown reaches zero nothing tells your agent. The phone timer rings; update the widget at the end time yourself, or let style `timer` count up.

## Idle

```json
{
  "slug": "focus",
  "title": "Focus",
  "symbol": "timer",
  "accent": "#FF9F0A",
  "blocks": [
    { "type": "metric", "label": "Today", "value": "50m" },
    { "type": "text", "text": "Ready to focus? I'll set a timer and keep the widget quiet." },
    { "type": "buttons", "items": [
      { "label": "25 min", "id": "start-25", "symbol": "timer" },
      { "label": "50 min", "id": "start-50", "symbol": "clock.fill" }
    ] },
    { "type": "chart", "label": "Focus this week, min", "style": "bar", "values": [75, 50, 100, 25, 50] }
  ],
  "sizes": { "small": [0], "large": [0, 1, 3, 2] },
  "lock": { "circular": 0, "inline": "Focus: ready" }
}
```

## Running

Same four blocks; the first is now a countdown with a filling bar (it spans medium on its own), and the round Lock Screen widget shows it:

```json
{
  "slug": "focus",
  "title": "Focus",
  "blocks": [
    { "type": "countdown", "label": "Focus ends", "to": "2027-01-04T10:25:00+03:00", "from": "2027-01-04T10:00:00+03:00", "style": "timer" },
    { "type": "text", "text": "Focusing on the Q4 report. The timer rings at the end." },
    { "type": "buttons", "items": [
      { "label": "Done", "id": "done", "symbol": "checkmark" },
      { "label": "Stop", "id": "stop", "symbol": "stop.fill" }
    ] },
    { "type": "chart", "label": "Focus this week, min", "style": "bar", "values": [75, 50, 100, 25, 50] }
  ],
  "sizes": { "small": [0], "large": [0, 1, 3, 2] },
  "lock": { "circular": 0, "inline": "Focus until 10:25" }
}
```

## Handler

```javascript
// focus-handler.mjs, run by listen.mjs. Keeps the timer's alarm_id in a file so Stop can cancel it.
import { readFileSync, writeFileSync, existsSync } from "node:fs";

const API = "https://ujtuxanqlzmhnrzijfoo.supabase.co/functions/v1/agent-api";
const headers = { authorization: `Bearer ${process.env.DROPLET_TOKEN}`, "content-type": "application/json", "x-region": "us-east-1" };
const droplet = async (action, body = {}) => (await fetch(API, { method: "POST", headers, body: JSON.stringify({ action, ...body }) })).json();
const STATE = "/tmp/droplet-focus.json";

const event = JSON.parse(readFileSync(0, "utf8"));
const week = { type: "chart", label: "Focus this week, min", style: "bar", values: [75, 50, 100, 25, 50] };   // from your own log
const idle = {
  blocks: [
    { type: "metric", label: "Today", value: "50m" },
    { type: "text", text: "Ready to focus? I'll set a timer and keep the widget quiet." },
    { type: "buttons", items: [
      { label: "25 min", id: "start-25", symbol: "timer" },
      { label: "50 min", id: "start-50", symbol: "clock.fill" },
    ] },
    week,
  ],
  lock: { circular: 0, inline: "Focus: ready" },
};

if (event.target === "start-25" || event.target === "start-50") {
  const minutes = event.target === "start-25" ? 25 : 50;
  const from = new Date(), to = new Date(Date.now() + minutes * 60_000);
  console.log(JSON.stringify({
    blocks: [
      { type: "countdown", label: "Focus ends", to: to.toISOString(), from: from.toISOString(), style: "timer" },
      { type: "text", text: `Focusing for ${minutes} min. The timer rings at the end.` },
      { type: "buttons", items: [
        { label: "Done", id: "done", symbol: "checkmark" },
        { label: "Stop", id: "stop", symbol: "stop.fill" },
      ] },
      week,
    ],
    lock: { circular: 0, inline: `Focus: ${minutes} min` },
  }));
  const r = await droplet("phone_action", { kind: "timer", params: { seconds: minutes * 60, label: "Focus done" } });
  if (r.status === "done") writeFileSync(STATE, JSON.stringify({ alarm_id: r.result.alarm_id }));
}

if (event.target === "done" || event.target === "stop") {
  console.log(JSON.stringify(idle));
  if (existsSync(STATE)) {
    const { alarm_id } = JSON.parse(readFileSync(STATE, "utf8"));
    await droplet("phone_action", { kind: "cancel_alarm", params: { alarm_id } });   // timers cancel like alarms
  }
}
```

- The widget updates first, then the timer is set, so the tap feels instant.
- Idle and running have the same four blocks in the same order (a number or the countdown, a sentence, the buttons, the week's chart), so `sizes` and `lock` fit both.
- `toISOString()` is UTC with a `Z`, which is fine: it's an exact instant.
- If `can` has no `alarms`, skip the timer and tell the user; the countdown still works.
- Style `timer` counts up after the end, so a forgotten session shows how long ago it ended instead of "Now".
