I built Upvas, an intermittent fasting app for iPhone aimed at Indian schedules: late dinners, and presets for Ekadashi and Navratri next to the usual 16:8 style windows. The part I want to write about is the timer, because the obvious way to build it is wrong on a phone.
Why not count seconds
The first idea is a setInterval that adds one second to an elapsed counter. A fast lasts many hours. In that time the phone locks, the OS suspends your JavaScript, and it may kill the app entirely. A counter only advances while your code runs, so it drifts or resets.
Store the facts, derive the rest
An active fast in Upvas is a small record:
export interface ActiveFast {
id: string;
startedAt: number; // Unix ms
plannedEndAt: number; // Unix ms
targetHours: number;
protocolName: string;
}
Everything on screen comes from a pure function of that record and the current time:
const elapsedMs = Math.max(0, now - fast.startedAt);
const totalMs = fast.plannedEndAt - fast.startedAt;
const progress = Math.min(1, elapsedMs / Math.max(totalMs, 1));
const remainingMs = Math.max(0, fast.plannedEndAt - now);
now is a parameter that defaults to Date.now(), so a test can pass any moment it likes without fake timers.
Persist only the source of truth
The timer lives in a Zustand store with the persist middleware on AsyncStorage. The important line is partialize:
partialize: (state) => ({ activeFast: state.activeFast }),
Progress, hours remaining and labels are never written to disk. After a cold start they are recalculated from the two timestamps, so there is no stale value to reconcile.
There is still a one second interval, but its only job is to repaint. If it stops for six hours, the next tick shows the correct number.
Backdating comes free
Upvas is built around dinner: you tell it when you finished eating, often a while after the fact. Because the state is just a start time, backdating is one subtraction:
const startedAt = now - backdateMinutes * 60000;
const plannedEndAt = startedAt + targetHours * 3600000;
What this does not solve
- It trusts the device clock. Change the clock by hand and the elapsed time changes with it. Time zones are a smaller issue, since the stored values are epoch milliseconds and only the labels are local.
- Ending a fast has to write the history entry explicitly. The timer state is cleared at that moment, so anything not copied into history is gone.
- Ekadashi in the app is a 24 hour preset. It does not calculate which calendar day Ekadashi falls on; that needs real lunar calendar work, and an average cycle length is not good enough.
None of this is medical advice. Long fasts are not for everyone, so talk to a doctor first.
If you have shipped something with long running timers, I'd like to hear how you handled clock changes.
Links (I'm the developer)
- iOS: https://apps.apple.com/app/id6772062089?utm_source=devto&utm_medium=article&utm_campaign=upvas_timer
- Site: https://lumenlabs.works/upvas?utm_source=devto&utm_medium=article&utm_campaign=upvas_timer
Disclosure: I used an AI assistant to help draft and edit this post; the technical details come from the app's source code.
Top comments (0)