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
Watch clips: what the phone shows, animated WebP included
Duarte Santos41a4f620c3ac
diff --git a/frontend/ios/App/App/WatchBridgePlugin.swift b/frontend/ios/App/App/WatchBridgePlugin.swiftindex d606f9297..b5ef9913e 100644--- a/frontend/ios/App/App/WatchBridgePlugin.swift+++ b/frontend/ios/App/App/WatchBridgePlugin.swift@@ -30,9 +30,11 @@ import WatchConnectivity* await WatchBridge.sendClips({ wid, clips: [{ exId, path }] }) // the workout's animations** Animations: only the running workout's, only as files sent with transferFile (the watch keeps- * them while that workout runs and deletes them after). Each comes from the app's own clip in the- * app package (public/<path>, an MP4 loop), made into a small looping GIF here, because the watch- * shows a frame sequence and plays no MP4 inline. Nothing is fetched from anywhere.+ * them while that workout runs and deletes them after). Each is what this phone shows for that+ * exercise: a file of a downloaded animation pack (`file`), or the app's built-in clip in the app+ * package (`path` under public/: the store build's animated WebP, an MP4 loop in other builds).+ * Made into a small looping GIF here (ImageIO reads animated WebP; AVFoundation the MP4), because+ * the watch shows a frame sequence. Nothing is fetched from anywhere.*/@objc(WatchBridgePlugin)public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {@@ -127,11 +129,10 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {var sent = self.clipsSent[wid] ?? []if self.clipsSent.count > 4 { self.clipsSent = self.clipsSent.filter { $0.key == wid } }for clip in clips {- guard let exId = clip["exId"] as? String, let path = clip["path"] as? String,- !sent.contains(exId), !path.contains(".."), !path.hasPrefix("/") else { continue }- let video = Bundle.main.bundleURL.appendingPathComponent("public").appendingPathComponent(path)- guard FileManager.default.fileExists(atPath: video.path),- let gif = WatchBridgePlugin.gif(for: video, named: exId) else { continue }+ guard let exId = clip["exId"] as? String, !sent.contains(exId),+ let source = WatchBridgePlugin.clipSource(clip),+ FileManager.default.isReadableFile(atPath: source.path),+ let gif = WatchBridgePlugin.gif(for: source, named: exId) else { continue }s.transferFile(gif, metadata: ["exId": exId, "wid": wid])sent.insert(exId)}@@ -139,14 +140,29 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {}}- /// A small looping GIF of an MP4 clip (about 2 to 3 seconds, 160 px), cached by exercise.- static func gif(for video: URL, named name: String) -> URL? {+ /// The clip's file: a pack file (an absolute .webp path the AnimationPacks plugin gave the page)+ /// or a file in the app package (public/<path>). Anything else is refused.+ static func clipSource(_ clip: [String: Any]) -> URL? {+ let ok = { (p: String) in !p.contains("..") && ["webp", "gif", "mp4"].contains((p as NSString).pathExtension.lowercased()) }+ if let file = clip["file"] as? String, file.hasPrefix("/"), ok(file), file.lowercased().hasSuffix(".webp") {+ return URL(fileURLWithPath: file)+ }+ if let path = clip["path"] as? String, !path.hasPrefix("/"), ok(path) {+ return Bundle.main.bundleURL.appendingPathComponent("public").appendingPathComponent(path)+ }+ return nil+ }++ /// A small looping GIF of a clip (160 px), cached by exercise: an animated WebP or GIF frame by+ /// frame through ImageIO, an MP4 sampled about ten times a second through AVFoundation.+ static func gif(for source: URL, named name: String) -> URL? {let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("watch-clips")try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)let safe = name.replacingOccurrences(of: "/", with: "_")let out = dir.appendingPathComponent(safe + ".gif")if FileManager.default.fileExists(atPath: out.path) { return out }- let asset = AVURLAsset(url: video)+ if source.pathExtension.lowercased() != "mp4" { return gifFromImage(source, to: out) }+ let asset = AVURLAsset(url: source)let seconds = CMTimeGetSeconds(asset.duration)guard seconds.isFinite, seconds > 0 else { return nil }let gen = AVAssetImageGenerator(asset: asset)@@ -174,6 +190,38 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {return out}+ private static func gifFromImage(_ source: URL, to out: URL) -> URL? {+ guard let src = CGImageSourceCreateWithURL(source as CFURL, nil) else { return nil }+ let count = CGImageSourceGetCount(src)+ guard count > 0,+ let dest = CGImageDestinationCreateWithURL(out as CFURL, UTType.gif.identifier as CFString, count, nil) else { return nil }+ CGImageDestinationSetProperties(dest, [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary)+ let thumb = [+ kCGImageSourceCreateThumbnailFromImageAlways as String: true,+ kCGImageSourceThumbnailMaxPixelSize as String: 160,+ kCGImageSourceCreateThumbnailWithTransform as String: true,+ ] as CFDictionary+ var added = 0+ for i in 0..<count {+ guard let img = CGImageSourceCreateThumbnailAtIndex(src, i, thumb) else { continue }+ let props = CGImageSourceCopyPropertiesAtIndex(src, i, nil) as? [String: Any]+ let webp = props?[kCGImagePropertyWebPDictionary as String] as? [String: Any]+ let gifp = props?[kCGImagePropertyGIFDictionary as String] as? [String: Any]+ let delay = (webp?[kCGImagePropertyWebPUnclampedDelayTime as String] as? Double)+ ?? (webp?[kCGImagePropertyWebPDelayTime as String] as? Double)+ ?? (gifp?[kCGImagePropertyGIFUnclampedDelayTime as String] as? Double)+ ?? (gifp?[kCGImagePropertyGIFDelayTime as String] as? Double) ?? 0.1+ let frame = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: delay > 0.01 ? delay : 0.1]] as CFDictionary+ CGImageDestinationAddImage(dest, img, frame)+ added += 1+ }+ guard added > 0, CGImageDestinationFinalize(dest) else {+ try? FileManager.default.removeItem(at: out)+ return nil+ }+ return out+ }+/* ---------- the watch's side ---------- */public func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {diff --git a/frontend/src/lib/anim-packs.js b/frontend/src/lib/anim-packs.jsindex 08bff0a11..0f1f92fd3 100644--- a/frontend/src/lib/anim-packs.js+++ b/frontend/src/lib/anim-packs.js@@ -48,7 +48,8 @@ export const deviceInfo = () => {}/* ---------------------------------------------------------------- state */-// packs: { [packId]: { state: 'none'|'ready'|'downloading'|'failed'|'needs-wifi', progress, bytes, base } }+// packs: { [packId]: { state: 'none'|'ready'|'downloading'|'failed'|'needs-wifi', progress, bytes, base, dir } }+// (`dir`: the pack's folder as the native side names it, for the Apple Watch's clips, lib/watch-sync.js)let state = { available: false, checked: false, packs: {}, prefs: readPrefs() }const listeners = new Set()const emit = () => { for (const fn of listeners) fn() }@@ -75,7 +76,7 @@ async function readPack(p, id) {const st = await p.Packs.status({ packId: id, probe: probeFor(id) })if (st?.state !== 'ready') { setPack(id, { state: state.packs[id]?.state === 'downloading' ? 'downloading' : 'none', bytes: 0, base: null }); return }const { path } = await p.Packs.localPath({ packId: id, probe: probeFor(id) })- setPack(id, { state: 'ready', bytes: Number(st.bytes) || 0, base: path ? p.toUrl(path) : null, progress: 1 })+ setPack(id, { state: 'ready', bytes: Number(st.bytes) || 0, base: path ? p.toUrl(path) : null, dir: path || null, progress: 1 })} catch { setPack(id, { state: 'none', base: null }) }}diff --git a/frontend/src/lib/watch-sync.js b/frontend/src/lib/watch-sync.jsindex 7aeceffe2..aafab9830 100644--- a/frontend/src/lib/watch-sync.js+++ b/frontend/src/lib/watch-sync.js@@ -15,7 +15,9 @@// The iPhone app only. Without a paired watch with the app on it, nothing is sent and nothing// arrives: the phone behaves exactly as before.import { MOBILE, nativePlatform } from './mobile.js'-import { EXIDX, gifSrc, figureOf, isVideoSrc } from './exercises.js'+import { EXIDX, gifSrc, figureOf } from './exercises.js'+import { packId, packDrawingId } from './anim-quality.js'+import { getState as animPacks } from './anim-packs.js'import { exerciseNameFor } from './i18n.js'import { modeOf, setLabel, lastEntryFor, effectiveRoutineIds } from './history.js'import { isWarmupRow } from './workout-model.js'@@ -104,21 +106,29 @@ export function buildSnapshot(S, { timer = null, ready = null } = {}) {}/**- * The running workout's animations for the watch: the app's own clip of each exercise in it, as a- * path inside the app package (the native side turns each into a small GIF and sends it as a file;- * the watch deletes them when the workout ends). Nothing for a clip that is not a packaged file.+ * The running workout's animations for the watch, from what this phone itself shows for each+ * exercise: the smallest downloaded animation pack of the figure (240 px, `file`: a path in that+ * pack's folder) when there is one, else the app's built-in clip (`path` inside the app package:+ * the 180 px animated WebP of the store build, an MP4 loop in other builds). The native side makes+ * each into a small GIF and sends it as a file; the watch deletes them when the workout ends.+ * Nothing is ever fetched from anywhere.*/-export function clipsFor (S) {+export const WATCH_PACK_PX = 240+export function clipsFor (S, packs = animPacks().packs || {}) {const A = S && S.activeif (!A || !Array.isArray(A.entries)) return []const figure = figureOf(S)+ const pack = packs[packId(WATCH_PACK_PX, figure)]+ const packDir = pack && pack.state === 'ready' && typeof pack.dir === 'string' && pack.dir ? pack.dir.replace(/\/$/, '') : nullconst out = [], seen = new Set()for (const e of A.entries) {const ex = e && EXIDX[e.id]if (!ex || seen.has(e.id) || !ex.gif) continue- const path = gifSrc(ex, figure)- if (!isVideoSrc(path) || /^[a-z]+:|^\//i.test(path) || path.includes('..')) continueseen.add(e.id)+ const drawing = packDrawingId(ex, figure)+ if (packDir && drawing && /^[A-Za-z0-9_-]+$/.test(drawing)) { out.push({ exId: e.id, file: `${packDir}/${drawing}.webp` }); continue }+ const path = gifSrc(ex, figure)+ if (!/\.(webp|mp4|gif)$/i.test(path) || /^[a-z]+:|^\//i.test(path) || path.includes('..')) continueout.push({ exId: e.id, path })}return outdiff --git a/frontend/src/lib/watch-sync.test.js b/frontend/src/lib/watch-sync.test.jsindex 3d098835d..078360a3b 100644--- a/frontend/src/lib/watch-sync.test.js+++ b/frontend/src/lib/watch-sync.test.js@@ -170,15 +170,26 @@ describe('heart rate from the watch', () => {})describe('the workout\'s animations for the watch', () => {- it('are the app\'s own packaged clips of the running workout, one per exercise, nothing else', async () => {+ it('are what the phone shows: the built-in clip of each exercise in the app package', async () => {const { clipsFor } = await import('./watch-sync.js')const { EXIDX } = await import('./exercises.js')- const S = state()- const clips = clipsFor(S)+ const clips = clipsFor(state(), {})expect(clips.map(c => c.exId)).toEqual(['0025', '0001'].filter(id => EXIDX[id]?.gif))for (const c of clips) {- expect(c.path).toMatch(/^exercise-media\/clip\/.+\.mp4$/)+ expect(c.path).toMatch(/^exercise-media\/clip\/[^/]+\.(mp4|webp)$/)+ expect(c.file).toBeUndefined()}- expect(clipsFor({ active: null })).toEqual([])+ expect(clipsFor({ active: null }, {})).toEqual([])+ })++ it('or the downloaded 240 px pack of the figure, when it is there', async () => {+ const { clipsFor } = await import('./watch-sync.js')+ const packs = { 'anim-240-male': { state: 'ready', dir: '/private/var/assetpacks/anim-240-male/' } }+ const clips = clipsFor(state(), packs)+ expect(clips.length).toBeGreaterThan(0)+ for (const c of clips) expect(c.file).toMatch(/^\/private\/var\/assetpacks\/anim-240-male\/[A-Za-z0-9_-]+\.webp$/)+ // a pack that is not ready, or of the other figure, is not used+ expect(clipsFor(state(), { 'anim-240-female': { state: 'ready', dir: '/x' } })[0].path).toBeDefined()+ expect(clipsFor(state(), { 'anim-240-male': { state: 'downloading', dir: '/x' } })[0].path).toBeDefined()})})