Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Animation quality: offer only the packs the build has
Duarte Santos58dab637f695
diff --git a/frontend/android/app/src/play/java/ch/duartesantos/opengym/AnimationPacksPlugin.java b/frontend/android/app/src/play/java/ch/duartesantos/opengym/AnimationPacksPlugin.javaindex 0c66e042c..bff46dd22 100644--- a/frontend/android/app/src/play/java/ch/duartesantos/opengym/AnimationPacksPlugin.java+++ b/frontend/android/app/src/play/java/ch/duartesantos/opengym/AnimationPacksPlugin.java@@ -169,6 +169,10 @@ public class AnimationPacksPlugin extends Plugin {JSObject ret = new JSObject();ret.put("available", !parts.isEmpty());ret.put("testflight", BuildConfig.DEBUG);+ // The packs this build has: the page offers only those (a quality may not be encoded yet).+ JSArray ids = new JSArray();+ for (String id : parts.keySet()) ids.put(id);+ ret.put("packs", ids);call.resolve(ret);}diff --git a/frontend/src/components/Animations.jsx b/frontend/src/components/Animations.jsxindex a5fb31211..f607969e4 100644--- a/frontend/src/components/Animations.jsx+++ b/frontend/src/components/Animations.jsx@@ -7,8 +7,8 @@ import { t } from '../lib/i18n.js'import { MOBILE } from '../lib/mobile.js'import { gifSrc, figureOf } from '../lib/exercises.js'import { fmtMB } from '../lib/media-limits.js'-import { BUILTIN_PX, packQualities, packId, wantedQuality, clipSources } from '../lib/anim-quality.js'-import { subscribe, getState, installedBases, deviceInfo, refresh, ensureWanted, writePrefs, download, remove, downloadBytes, diagnose, ALL_PACKS } from '../lib/anim-packs.js'+import { BUILTIN_PX, packId, parsePackId, wantedQuality, clipSources } from '../lib/anim-quality.js'+import { subscribe, getState, installedBases, deviceInfo, refresh, ensureWanted, writePrefs, download, remove, downloadBytes, diagnose, offeredQualities, targetPack, ALL_PACKS } from '../lib/anim-packs.js'import { Section, Row, Switch, Button } from './ui.jsx'import { useUI } from '../store/useUI.js'@@ -35,11 +35,13 @@ export function useAnimPacksBoot() {}// "Auto (360 px)", "Still only", "Built in (180 px)", "720 px".-export function choiceLabel(choice, packs = getState()) {+export function choiceLabel(choice, packs = getState(), figure = 'male') {if (choice === 'still') return t('Still only')if (choice === 'builtin' || !packs.available) return t('Built in ({0} px)', BUILTIN_PX)if (choice === 'auto') {- const q = wantedQuality('auto', deviceInfo())+ // A build that names its packs (Android) may not have the size Auto wants: the one it gets.+ const target = packs.offered ? targetPack('auto', figure, deviceInfo(), packs) : null+ const q = packs.offered ? (target ? parsePackId(target).px : 'builtin') : wantedQuality('auto', deviceInfo())return t('Auto ({0})', q === 'builtin' ? t('{0} px', BUILTIN_PX) : t('{0} px', q))}return t('{0} px', choice)@@ -47,8 +49,9 @@ export function choiceLabel(choice, packs = getState()) {export function AnimationQualityRow() {const packs = useAnimPacks()+ const figure = useStore(s => figureOf(s.S))if (!MOBILE) return null- return <Row icon="image" iconTint="var(--indigo)" title={t('Animation quality')} value={choiceLabel(packs.prefs.choice, packs)} accessory="chevron"+ return <Row icon="image" iconTint="var(--indigo)" title={t('Animation quality')} value={choiceLabel(packs.prefs.choice, packs, figure)} accessory="chevron"onClick={() => useUI.getState().openSheet(close => <AnimationQualitySheet close={close} />)} />}@@ -76,7 +79,7 @@ function PackDetails({ figure }) {const [rows, setRows] = useState(null)const run = async () => {setRows([])- const ids = ALL_PACKS.filter(id => id.endsWith('-' + figure))+ const ids = (getState().offered || ALL_PACKS).filter(id => id.endsWith('-' + figure))const out = []for (const id of ids) { out.push(await diagnose(id)); setRows([...out]) }}@@ -94,7 +97,7 @@ export function AnimationQualitySheet({ close }) {const { choice, wifiOnly } = packs.prefsconst pick = c => { writePrefs({ choice: c }); ensureWanted(figure) }const retry = id => () => download(id)- const qualities = packs.available ? packQualities() : []+ const qualities = packs.available ? offeredQualities(figure, packs) : []const ready = Object.entries(packs.packs).filter(([, p]) => p.state === 'ready')const taps = useRef(0)const [details, setDetails] = useState(false)@@ -104,9 +107,9 @@ export function AnimationQualitySheet({ close }) {<Section footer={packs.available? t('Sharper animations download once per figure. Until they’re here you see the built-in ones, so nothing ever goes blank.'): t('Sharper animations aren’t available on this device yet. The built-in ones work everywhere.')}>- {packs.available && <Row title={choiceLabel('auto', packs)} subtitle={t('A good size for this screen.')} accessory={choice === 'auto' ? 'check' : 'none'} onClick={() => pick('auto')} />}+ {packs.available && <Row title={choiceLabel('auto', packs, figure)} subtitle={t('A good size for this screen.')} accessory={choice === 'auto' ? 'check' : 'none'} onClick={() => pick('auto')} />}<Row title={t('Still only')} subtitle={t('No animation, just the picture. Easy on battery.')} accessory={choice === 'still' ? 'check' : 'none'} onClick={() => pick('still')} />- <Row title={choiceLabel('builtin', packs)} accessory={choice === 'builtin' || (!packs.available && choice !== 'still') ? 'check' : 'none'} onClick={() => pick('builtin')} />+ <Row title={choiceLabel('builtin', packs, figure)} accessory={choice === 'builtin' || (!packs.available && choice !== 'still') ? 'check' : 'none'} onClick={() => pick('builtin')} />{qualities.map(px => {const id = packId(px, figure)const p = packs.packs[id]diff --git a/frontend/src/lib/anim-packs.js b/frontend/src/lib/anim-packs.jsindex 375521534..cc68c5bb9 100644--- a/frontend/src/lib/anim-packs.js+++ b/frontend/src/lib/anim-packs.js@@ -2,7 +2,8 @@// and the person's choice of quality. The rules are lib/anim-quality.js; this is the state around// them and the bridge to the native AnimationPacks plugin://-// available() → { available, testflight } iOS 26+ with Apple-hosted packs+// available() → { available, testflight, packs? } iOS 26+ with Apple-hosted packs;+// Android (Play build): `packs`, the ids this build has// status({ packId, probe }) → { state: 'ready'|'none', bytes?, base? }// info({ packId }) → { downloadSize? } what the download costs, from Apple// diagnose({ packId, probe }) → what the native side knows, for the details row@@ -24,7 +25,7 @@// profile, its workouts and its history are not touched by any of this.import { MOBILE } from './mobile.js'import { EXIDX } from './exercises.js'-import { PACK_QUALITIES, FIGURES, packId, packDrawingId, readChoice, wantedPack, DEFAULT_CHOICE } from './anim-quality.js'+import { PACK_QUALITIES, FIGURES, packId, packDrawingId, readChoice, wantedPack, packQualities, parsePackId, DEFAULT_CHOICE } from './anim-quality.js'export const PREFS_KEY = 'gym_anim_v1'export const ALL_PACKS = PACK_QUALITIES.flatMap(px => FIGURES.map(f => packId(px, f)))@@ -57,7 +58,8 @@ export const deviceInfo = () => {/* ---------------------------------------------------------------- state */// packs: { [packId]: { state: 'none'|'ready'|'downloading'|'failed'|'needs-wifi', progress, bytes, base, downloadSize } }-let state = { available: false, testflight: false, checked: false, packs: {}, prefs: readPrefs() }+// offered: the pack ids this build has (Android's Play build says), or null for all of them (iOS)+let state = { available: false, testflight: false, offered: null, checked: false, packs: {}, prefs: readPrefs() }const listeners = new Set()const emit = () => { for (const fn of listeners) fn() }export const subscribe = fn => { listeners.add(fn); return () => listeners.delete(fn) }@@ -101,19 +103,41 @@ async function readInfo(p, id) {}/** What downloading a pack takes, in bytes: Apple's figure, else the known one, else null. */-export const downloadBytes = (id, s = state) => s.packs[id]?.downloadSize || KNOWN_DOWNLOAD_BYTES[id] || null+// (The known figures are Apple's packs: a build that names its own packs gets its sizes from its store.)+export const downloadBytes = (id, s = state) => s.packs[id]?.downloadSize || (s.offered ? null : KNOWN_DOWNLOAD_BYTES[id]) || null++/** The qualities this build has a pack of for a figure: all the sharper ones, or those it names. */+export const offeredQualities = (figure, s = state) =>+ packQualities().filter(px => !s.offered || s.offered.includes(packId(px, figure)))++/**+ * The pack a choice wants for a figure on this build: the wanted quality's when the build has+ * it, else the sharpest smaller one it has, else none (the built-in files).+ */+export function targetPack(choice, figure, device = deviceInfo(), s = state) {+ const id = wantedPack(choice, figure, device)+ if (!id || !s.offered || s.offered.includes(id)) return id+ const want = parsePackId(id)?.px || 0+ const px = offeredQualities(figure, s).filter(q => q <= want).pop()+ return px ? packId(px, figure) : null+}/** Asks the native side what it has. Once at start, and after every download or removal. */export async function refresh() {const p = await packsPlugin()if (!p) { state = { ...state, checked: true, available: false }; emit(); return state }- let available = false, testflight = false- try { const r = await p.Packs.available(); available = !!r?.available; testflight = !!r?.testflight } catch { available = false }- state = { ...state, checked: true, available, testflight }+ let available = false, testflight = false, offered = null+ try {+ const r = await p.Packs.available()+ available = !!r?.available; testflight = !!r?.testflight+ if (Array.isArray(r?.packs)) offered = r.packs.filter(id => ALL_PACKS.includes(id))+ } catch { available = false }+ state = { ...state, checked: true, available, testflight, offered }emit()if (available) {- await Promise.all(ALL_PACKS.map(id => readPack(p, id)))- ALL_PACKS.forEach(id => { readInfo(p, id) })+ const ids = offered || ALL_PACKS+ await Promise.all(ids.map(id => readPack(p, id)))+ ids.forEach(id => { readInfo(p, id) })}return state}@@ -163,7 +187,7 @@ export async function remove(id) {*/export function ensureWanted(figure, device = deviceInfo()) {if (!state.available) return null- const id = wantedPack(state.prefs.choice, figure, device)+ const id = targetPack(state.prefs.choice, figure, device)if (!id) return nullconst st = state.packs[id]?.stateif (st === 'ready' || st === 'downloading') return null@@ -198,5 +222,5 @@ function defaultLoadImage(src) {export function _resetAnimPacks(next = {}) {pluginP = null; progressHooked = false; downloads.clear()- state = { available: false, testflight: false, checked: false, packs: {}, prefs: readPrefs(), ...next }+ state = { available: false, testflight: false, offered: null, checked: false, packs: {}, prefs: readPrefs(), ...next }}diff --git a/frontend/src/lib/anim-packs.test.js b/frontend/src/lib/anim-packs.test.jsindex 03fd8fbc0..262b3bb31 100644--- a/frontend/src/lib/anim-packs.test.js+++ b/frontend/src/lib/anim-packs.test.js@@ -8,7 +8,7 @@ const mocks = vi.hoisted(() => ({ MOBILE: true, available: true, ready: new Set(vi.mock('./mobile.js', () => ({ get MOBILE() { return mocks.MOBILE } }))vi.mock('@capacitor/core', () => {const impl = {- available: async () => ({ available: mocks.available, testflight: mocks.testflight }),+ available: async () => ({ available: mocks.available, testflight: mocks.testflight, ...(mocks.offered ? { packs: mocks.offered } : {}) }),status: async ({ packId }) => (mocks.ready.has(packId) ? { state: 'ready', bytes: 5 * 1024 * 1024, ...(mocks.base ? { base: 'opengym-pack://' + packId } : {}) } : { state: 'none' }),info: async ({ packId }) => mocks.info[packId] || {},diagnose: async ({ packId, probe }) => ({ local: mocks.ready.has(packId), probe: packId + '/' + probe + '.webp', base: 'opengym-pack://' + packId }),@@ -36,7 +36,7 @@ import * as packs from './anim-packs.js'const phone = { shortSide: 390 }beforeEach(() => {localStorage.clear()- mocks.MOBILE = true; mocks.available = true; mocks.ready = new Set(); mocks.calls = []; mocks.listeners = {}; mocks.downloadFails = null; mocks.base = false; mocks.info = {}; mocks.testflight = false+ mocks.MOBILE = true; mocks.available = true; mocks.ready = new Set(); mocks.calls = []; mocks.listeners = {}; mocks.downloadFails = null; mocks.base = false; mocks.info = {}; mocks.testflight = false; mocks.offered = nullpacks._resetAnimPacks()})@@ -159,3 +159,28 @@ describe('reading a pack from where the app serves it', () => {expect(d.image).toBe('ok 480×480')})})++// Android's Play build names the packs it has: only those are offered, sized by the store, and+// Auto takes the sharpest one it has below what it wants.+describe('a build that names its packs', () => {+ it('offers only those, and Auto settles for the sharpest smaller one', async () => {+ mocks.offered = ['anim-240-male', 'anim-240-female', 'anim-480-male', 'anim-480-female']+ await packs.refresh()+ const s = packs.getState()+ expect(s.offered).toEqual(mocks.offered)+ expect(packs.offeredQualities('male')).toEqual([240, 480])+ expect(packs.targetPack('auto', 'male', { shortSide: 390 })).toBe('anim-240-male')+ expect(packs.targetPack('auto', 'male', { shortSide: 800 })).toBe('anim-480-male')+ expect(packs.targetPack(1080, 'female', {})).toBe('anim-480-female')+ expect(packs.downloadBytes('anim-480-male')).toBeNull()+ packs.ensureWanted('male', { shortSide: 390 })+ await new Promise(r => setTimeout(r, 0))+ expect(mocks.calls.find(c => c[0] === 'download')?.[1]).toBe('anim-240-male')+ })+ it('none it has under the wanted size: the built-in files, nothing downloaded', async () => {+ mocks.offered = ['anim-720-male']+ await packs.refresh()+ expect(packs.targetPack('auto', 'male', { shortSide: 390 })).toBeNull()+ expect(packs.ensureWanted('male', { shortSide: 390 })).toBeNull()+ })+})