Standalone mobile app: Capacitor shells for iOS and Android

A second flavor of openGym (VITE_MOBILE=1) that runs entirely on the
phone: no account, no server, no sync. State is mirrored into a file in
the app's private storage (survives WebView storage eviction), the
workout-day reminder is a native local notification scheduled on planned
weekdays, and backups export through the OS share sheet. Exercise media
loads from the same CDN as the demo build. npm run build:mobile, then
Android Studio / Xcode — see docs/MOBILE.md. NOTICE.md gains an AGPL §7
app-store exception.
This commit is contained in:
Duarte Santos committed 2026-07-23 15:14:36 +02:00
1 parent 389315625c
commit ee2be743b0
92 files changed
+3117 -29

No files matched your search

+58 -11
View File
@@ -7,6 +7,7 @@ import { webauthnOK, passkeyLogin, passkeyRegister, IS_ANDROID } from '../lib/ap
import { pushSupported, enablePush, disablePush, sendTestPush } from '../lib/push.js'
import { t, LANGS, INSTR_LANGS } from '../lib/i18n.js'
import { DEMO, REPO } from '../lib/demo.js'
import { MOBILE, shareExport, syncReminder } from '../lib/mobile.js'
import { loadStarterPlan, confirmSheet, importFromApp } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
import { Section, Row, SelectRow, Switch, Segmented, Button, TextField } from '../components/ui.jsx'
@@ -20,9 +21,16 @@ export default function Settings() {
const fileRef = useRef(null)
const importRef = useRef(null)
const doExport = () => {
const blob = new Blob([JSON.stringify(S, null, 2)], { type: 'application/json' })
const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'opengym-backup-' + todayISO() + '.json'; a.click(); URL.revokeObjectURL(a.href)
const doExport = async () => {
const json = JSON.stringify(S, null, 2)
const name = 'opengym-backup-' + todayISO() + '.json'
// WKWebView can't download blob URLs — the native build hands the file to the share sheet.
if (MOBILE) {
try { await shareExport(json, name); toast(t('Backup exported')) } catch (e) { /* share sheet dismissed */ }
return
}
const blob = new Blob([json], { type: 'application/json' })
const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); URL.revokeObjectURL(a.href)
toast(t('Backup exported'))
}
const doImport = ev => {
@@ -49,9 +57,13 @@ export default function Settings() {
<div style={{ flex: 1, marginLeft: 10 }}><h1>{t('Settings')}</h1></div>
</div>
{/* ---------- account (demo builds have nothing to sign in to) ---------- */}
<Section title={DEMO ? t('Demo') : t('Account')}>
{DEMO ? <>
{/* ---------- account (demo and mobile builds have nothing to sign in to) ---------- */}
<Section title={MOBILE ? t('Your data') : DEMO ? t('Demo') : t('Account')}>
{MOBILE ? <>
<Row icon="lock" iconTint="var(--acc)" title={t('All data stays on this phone')} subtitle={t('No account, no cloud — back it up anytime with Export below.')} />
<Row icon="rocket" iconTint="var(--indigo)" title={t('Self-host openGym')} subtitle={t('Passkey sign-in, sync across your devices, your own data.')} accessory="chevron"
onClick={() => window.open(REPO, '_blank', 'noopener')} />
</> : DEMO ? <>
<Row icon="sparkles" iconTint="var(--acc)" title={t('You’re in the demo')} subtitle={t('Example data, stored only in this browser — change anything you like.')} />
<Row icon="reset" iconTint="var(--blue)" title={t('Reset demo data')} accessory="chevron"
onClick={() => confirmSheet({ title: t('Reset demo data?'), message: t('Puts the example plan, workouts and weigh-ins back the way they started.'), confirmText: t('Reset'), onConfirm: () => { resetDemo(); nav('/home'); toast(t('Demo data reset')) } })} />
@@ -68,10 +80,10 @@ export default function Settings() {
<Row icon="lock" iconTint="var(--grey)" title={t('Passkeys not supported in this browser.')} />
)}
</Section>
{!user && !DEMO && <p className="sect-f" style={{ marginTop: -18, marginBottom: 22 }}>{t('Guest mode — data lives only in this browser.')}</p>}
{!user && !DEMO && !MOBILE && <p className="sect-f" style={{ marginTop: -18, marginBottom: 22 }}>{t('Guest mode — data lives only in this browser.')}</p>}
{/* ---------- appearance ---------- */}
<Section title={t('Appearance')} footer={DEMO ? undefined : t('synced with your profile')}>
<Section title={t('Appearance')} footer={DEMO || MOBILE ? undefined : t('synced with your profile')}>
<SelectRow
icon="globe" iconTint="var(--blue)" title={t('Language')}
value={S.lang || 'en'} onChange={v => update(s => { s.lang = v })}
@@ -123,7 +135,7 @@ export default function Settings() {
</Row>
</Section>
{user && <NotificationsCard S={S} update={update} toast={toast} />}
{(user || MOBILE) && <NotificationsCard S={S} update={update} toast={toast} />}
{/* ---------- data ---------- */}
<Section title={t('Data')}>
@@ -140,11 +152,12 @@ export default function Settings() {
<input ref={importRef} type="file" accept=".csv,.xml,text/csv,text/xml" style={{ display: 'none' }}
onChange={ev => { const f = ev.target.files[0]; if (f) importFromApp(f); ev.target.value = '' }} />
<Section title={t('Tip')}>
{/* "Add to Home screen" makes no sense inside the native app */}
{!MOBILE && <Section title={t('Tip')}>
<Row icon="lightbulb" iconTint="var(--yellow)"
title={IS_ANDROID ? t('In Chrome: ⋮ menu → Add to Home screen') : t('In Safari: Share → Add to Home Screen')}
subtitle={t('to install openGym as a full-screen app.') + ' ' + (user ? t('Your data syncs with your profile — sign in anywhere to see it.') : t('Guest data stays on this device — export a backup now and then!'))} />
</Section>
</Section>}
<div className="dim small" style={{ textAlign: 'center', marginTop: 4, lineHeight: 1.6 }}>
openGym · {t('free & open source (AGPL v3)')}<br />
@@ -154,6 +167,40 @@ export default function Settings() {
}
function NotificationsCard({ S, update, toast }) {
if (MOBILE) return <MobileReminderCard S={S} update={update} toast={toast} />
return <PushCard S={S} update={update} toast={toast} />
}
// Mobile build: the reminder is a native local notification scheduled on planned weekdays —
// no push server involved. The schedule itself is (re)synced by the store on every persist;
// this card only owns the OS permission prompt when the switch turns on.
function MobileReminderCard({ S, update, toast }) {
const setReminder = patch => update(s => { s.reminder = { ...(s.reminder || DEF.reminder), ...patch, tz: localTZ() } })
const toggle = async () => {
const on = !S.reminder?.on
if (on) {
const ok = await syncReminder({ ...S, reminder: { ...(S.reminder || DEF.reminder), on: true } }, true)
if (!ok) { toast(t('Could not change notification settings')); return }
}
setReminder({ on })
}
return (
<Section title={t('Notifications')}
footer={S.reminder?.on ? t('Reminds you at this time on days that have a routine planned.') : null}>
<Row icon="calendar" iconTint="var(--orange)" title={t('Workout day reminder')}>
<Switch checked={!!S.reminder?.on} onChange={toggle} />
</Row>
{S.reminder?.on && (
<Row icon="clock" iconTint="var(--purple)" title={t('Reminder time')}>
<input type="time" className="timef" value={S.reminder?.time || DEF.reminder.time}
onChange={e => setReminder({ time: e.target.value })} />
</Row>
)}
</Section>
)
}
function PushCard({ S, update, toast }) {
const [on, setOn] = useState(false)
const [busy, setBusy] = useState(false)
const supported = pushSupported()