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
iOS: keep the pairing token out of iCloud and computer backups
Duarte Santos92cb1b3ff7ee
diff --git a/frontend/src/lib/mobile.js b/frontend/src/lib/mobile.jsindex c77b93f0f..e9092a61f 100644--- a/frontend/src/lib/mobile.js+++ b/frontend/src/lib/mobile.js@@ -104,18 +104,45 @@ export async function writeJsonFile(name, data) {} catch (e) { /* not a Capacitor build, or the write failed — the caller's in-memory copy stands */ }}+// Where the pairing lives. It holds the session token, so on iOS it stays out of iCloud and+// computer backups: Library/NoCloud (Directory.LibraryNoCloud) is excluded from them, where+// Documents (Directory.Data on iOS) is not. Android keeps it in the app's files and excludes it+// by name in backup_rules.xml / data_extraction_rules.xml. A restored iPhone is paired again,+// the same as an Android phone moved through a cloud backup.+async function remoteDir() {+ const { Directory } = await import('@capacitor/filesystem')+ return (await nativePlatform()) === 'ios' ? Directory.LibraryNoCloud : Directory.Data+}+export async function loadRemoteFile() {try {const { Filesystem, Directory, Encoding } = await import('@capacitor/filesystem')+ const directory = await remoteDir()+ try {+ const r = await Filesystem.readFile({ path: REMOTE_FILE, directory, encoding: Encoding.UTF8 })+ return JSON.parse(r.data)+ } catch (e) {+ if (directory === Directory.Data) throw e+ }+ // An iPhone paired before the move: its pairing is still in Documents. Moved once, then the+ // backed-up copy is gone.const r = await Filesystem.readFile({ path: REMOTE_FILE, directory: Directory.Data, encoding: Encoding.UTF8 })- return JSON.parse(r.data)+ const data = JSON.parse(r.data)+ await Filesystem.writeFile({ path: REMOTE_FILE, directory, data: JSON.stringify(data), encoding: Encoding.UTF8, recursive: true })+ try { await Filesystem.deleteFile({ path: REMOTE_FILE, directory: Directory.Data }) } catch { /* moved; the old copy is only stale */ }+ return data} catch (e) { return null } // never decided yet}export async function saveRemoteFile(data) {try {- const { Filesystem, Directory, Encoding } = await import('@capacitor/filesystem')- await Filesystem.writeFile({ path: REMOTE_FILE, directory: Directory.Data, data: JSON.stringify(data), encoding: Encoding.UTF8 })+ const { Filesystem, Encoding } = await import('@capacitor/filesystem')+ const directory = await remoteDir()+ await Filesystem.writeFile({ path: REMOTE_FILE, directory, data: JSON.stringify(data), encoding: Encoding.UTF8, recursive: true })+ // The plugin marks Library/NoCloud as excluded from backup each time it looks the folder up,+ // which does nothing while the folder does not exist yet: the first write creates it, so+ // one more lookup marks it now rather than on the next launch.+ if (directory !== (await import('@capacitor/filesystem')).Directory.Data) await Filesystem.stat({ path: REMOTE_FILE, directory })} catch (e) { /* worst case: onboarding asks again next launch */ }}diff --git a/frontend/src/lib/mobile.remote-file.test.js b/frontend/src/lib/mobile.remote-file.test.jsnew file mode 100644index 000000000..7605cadce--- /dev/null+++ b/frontend/src/lib/mobile.remote-file.test.js@@ -0,0 +1,61 @@+// The pairing file (opengym-remote.json) holds the phone's session token. On iOS it lives in+// Library/NoCloud, which the Filesystem plugin excludes from iCloud and computer backups, and an+// iPhone paired before that has its file moved there out of Documents once. Android keeps it in+// the app's files, excluded by name in its backup rules (android-backup.test.js).+import { beforeEach, describe, expect, it, vi } from 'vitest'++const h = vi.hoisted(() => ({ files: new Map(), platform: 'ios', stats: [] }))+vi.mock('@capacitor/core', () => ({ Capacitor: { getPlatform: () => h.platform } }))+vi.mock('@capacitor/filesystem', () => ({+ Directory: { Data: 'DATA', LibraryNoCloud: 'LIBRARY_NO_CLOUD' },+ Encoding: { UTF8: 'utf8' },+ Filesystem: {+ readFile: async ({ path, directory }) => {+ const k = directory + '/' + path+ if (!h.files.has(k)) throw new Error('File does not exist')+ return { data: h.files.get(k) }+ },+ writeFile: async ({ path, directory, data }) => { h.files.set(directory + '/' + path, data) },+ deleteFile: async ({ path, directory }) => { h.files.delete(directory + '/' + path) },+ stat: async ({ path, directory }) => { h.stats.push(directory + '/' + path); return {} },+ },+}))++let M+beforeEach(async () => {+ h.files.clear(); h.stats.length = 0; h.platform = 'ios'+ vi.resetModules()+ vi.stubEnv('VITE_MOBILE', '1')+ M = await import('./mobile.js')+})++const PAIRED = { mode: 'remote', base: 'https://gym.example', token: 'tok', user: { id: 'u1' } }++describe('the pairing file', () => {+ it('on iOS is written to Library/NoCloud, never to Documents, and the folder is marked right away', async () => {+ expect(M.MOBILE).toBe(true)+ await M.saveRemoteFile(PAIRED)+ expect([...h.files.keys()]).toEqual(['LIBRARY_NO_CLOUD/opengym-remote.json'])+ expect(h.stats).toEqual(['LIBRARY_NO_CLOUD/opengym-remote.json'])+ expect(await M.loadRemoteFile()).toEqual(PAIRED)+ })++ it('on iOS moves a pairing made before out of Documents, once', async () => {+ h.files.set('DATA/opengym-remote.json', JSON.stringify(PAIRED))+ expect(await M.loadRemoteFile()).toEqual(PAIRED)+ expect([...h.files.keys()]).toEqual(['LIBRARY_NO_CLOUD/opengym-remote.json'])+ expect(await M.loadRemoteFile()).toEqual(PAIRED)+ })++ it('on iOS with nothing anywhere is still "never decided"', async () => {+ expect(await M.loadRemoteFile()).toBeNull()+ })++ it('on Android stays where its backup rules exclude it', async () => {+ h.platform = 'android'+ await M.saveRemoteFile(PAIRED)+ expect([...h.files.keys()]).toEqual(['DATA/opengym-remote.json'])+ expect(h.stats).toEqual([])+ expect(await M.loadRemoteFile()).toEqual(PAIRED)+ })+})