feat: add update check in settings

Check GitLab releases API on settings open to determine if a newer
version is available. Shows current version (up to date) or the new
version number with download/install action.

- New lib/update.js: checkForUpdate(), downloadAndInstall()
- Settings Data section: update row with auto-check on mount
- Dialog for re-check confirmation when already up to date
- APK download + installer trigger on Android via Capacitor
- Falls back to opening releases page on web
- 16 unit tests for version check logic
This commit is contained in:
Sebastian G committed 2026-09-04 13:00:25 +02:00
1 parent d6f0db9b4b
commit 106a3abee0
3 files changed
+338

No files matched your search

+87
View File
@@ -10,6 +10,7 @@ import { wakeLockSupported } from '../lib/wakelock.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 { checkForUpdate, downloadAndInstall } from '../lib/update.js'
import { ConnectSheet } from './MobileOnboarding.jsx'
import { loadStarterPlan, confirmSheet, importFromApp, importFromHevy, equipmentProfileSheet } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
@@ -26,6 +27,89 @@ export default function Settings() {
const importRef = useRef(null)
const wakeOK = wakeLockSupported()
// --- update check state ---
const [updateInfo, setUpdateInfo] = useState(null) // { hasUpdate, latestVersion, apkUrl } | null while loading
const [updateLoading, setUpdateLoading] = useState(true)
const [updateError, setUpdateError] = useState(null)
const runUpdateCheck = async () => {
setUpdateLoading(true)
setUpdateError(null)
try {
const info = await checkForUpdate()
setUpdateInfo(info)
} catch (e) {
setUpdateError(e.message || 'Check failed')
} finally {
setUpdateLoading(false)
}
}
useEffect(() => { runUpdateCheck() }, [])
const onUpdateRowClick = () => {
if (updateLoading) return
if (updateInfo?.hasUpdate && updateInfo.apkUrl) {
// Start download & install
const version = updateInfo.latestVersion
confirmSheet({
title: t('Update to {0}?', version),
message: t('The latest version will be downloaded and the installer will open.'),
confirmText: t('Download & Install'),
onConfirm: async () => {
toast(t('Downloading update…'))
try {
await downloadAndInstall(updateInfo.apkUrl)
} catch (e) {
toast(t('Update failed: {0}', e.message))
}
},
})
} else if (updateInfo?.hasUpdate && !updateInfo.apkUrl) {
// Update available but no APK asset — open the releases page
window.open('https://gitlab.com/DuarteSantos8/opengym/-/releases', '_blank', 'noopener')
} else {
// Already up to date or error — re-check and show result in a dialog
setUpdateLoading(true)
setUpdateError(null)
checkForUpdate().then(info => {
setUpdateInfo(info)
setUpdateLoading(false)
useUI.getState().openSheet(close => (
<div style={{ textAlign: 'center', padding: '4px 0' }}>
<h3>{t('Update check')}</h3>
<div className="muted" style={{ marginBottom: 18, lineHeight: 1.5 }}>
{info.hasUpdate
? t('{0} is available!', info.latestVersion)
: t('openGym {0} — you\u2019re up to date.', __APP_VERSION__)}
</div>
<Button variant="ghost" className="dim" onClick={close}>{t('OK')}</Button>
</div>
), { kind: 'center' })
}).catch(e => {
setUpdateError(e.message || 'Check failed')
setUpdateLoading(false)
useUI.getState().openSheet(close => (
<div style={{ textAlign: 'center', padding: '4px 0' }}>
<h3>{t('Update check')}</h3>
<div className="muted" style={{ marginBottom: 18, lineHeight: 1.5 }}>
{t('Could not check for updates: {0}', e.message || 'unknown error')}
</div>
<Button variant="ghost" className="dim" onClick={close}>{t('OK')}</Button>
</div>
), { kind: 'center' })
})
}
}
const updateTitle = updateLoading
? t('Checking for updates…')
: updateError
? t('Update check failed')
: updateInfo?.hasUpdate
? t('{0} available', updateInfo.latestVersion)
: t('openGym {0} (up to date)', __APP_VERSION__)
const doExport = async () => {
const json = JSON.stringify(S, null, 2)
const name = 'opengym-backup-' + todayISO() + '.json'
@@ -226,6 +310,9 @@ export default function Settings() {
{/* ---------- data: fill it, bring things over, back it up, wipe it ---------- */}
<Section title={t('Data')}>
<Row icon="rocket" iconTint="var(--purple)" title={updateTitle}
accessory={updateInfo?.hasUpdate ? 'chevron' : 'none'}
onClick={onUpdateRowClick} />
<Row icon="sparkles" iconTint="var(--acc)" title={t('Load starter plan (PPL)')} accessory="chevron" onClick={loadStarterPlan} />
<Row icon="shuffle" iconTint="var(--teal)" title={t('Import from another app')}
subtitle={t('FitNotes, Strong, Hevy — or body weight from Apple Health')}