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 from a downloaded pack read through Background Assets
Duarte Santosb2e2fab115d5
diff --git a/frontend/ios/App/App/AnimationPacksPlugin.swift b/frontend/ios/App/App/AnimationPacksPlugin.swiftindex 4ff06ba02..3d8a03363 100644--- a/frontend/ios/App/App/AnimationPacksPlugin.swift+++ b/frontend/ios/App/App/AnimationPacksPlugin.swift@@ -41,6 +41,18 @@ public class AnimationPacksPlugin: CAPPlugin, CAPBridgedPlugin {CAPPluginMethod(name: "diagnose", returnType: CAPPluginReturnPromise),]+ /// One file of a downloaded pack, its bytes memory-mapped (the Apple Watch's clips,+ /// WatchBridgePlugin), or nil: not downloaded, no such drawing, or before iOS 26.+ static func packData(_ id: String, _ drawing: String) -> Data? {+ guard matches(packPattern, id), drawing.range(of: "^[A-Za-z0-9_-]+$", options: .regularExpression) != nil else { return nil }+ #if canImport(BackgroundAssets)+ if #available(iOS 26.0, *) {+ return try? AssetPackManager.shared.contents(at: FilePath("\(id)/\(drawing).webp"), searchingInAssetPackWithID: id, options: .mappedIfSafe)+ }+ #endif+ return nil+ }+/// Only the ids the app knows: a pack id from the page goes nowhere else.static let packPattern = try! NSRegularExpression(pattern: "^anim-(240|360|480|720|1080)-(male|female)$")private static let probePattern = try! NSRegularExpression(pattern: "^[0-9]{4,5}$")diff --git a/frontend/ios/App/App/WatchBridgePlugin.swift b/frontend/ios/App/App/WatchBridgePlugin.swiftindex 7d2a24e59..55a27a7d4 100644--- a/frontend/ios/App/App/WatchBridgePlugin.swift+++ b/frontend/ios/App/App/WatchBridgePlugin.swift@@ -130,9 +130,7 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {if self.clipsSent.count > 4 { self.clipsSent = self.clipsSent.filter { $0.key == wid } }for clip in clips {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 }+ let gif = WatchBridgePlugin.gifFor(clip, named: exId) else { continue }s.transferFile(gif, metadata: ["exId": exId, "wid": wid])sent.insert(exId)}@@ -140,17 +138,23 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {}}- /// 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)+ /// The clip as a small GIF: a file of a downloaded animation pack (`packId` + `drawing`, read+ /// through Background Assets, never by path) or a file in the app package (`path` under+ /// public/). Anything else is refused.+ static func gifFor(_ clip: [String: Any], named exId: String) -> URL? {+ let dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("watch-clips")+ try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)+ if let pack = clip["packId"] as? String, let drawing = clip["drawing"] as? String {+ let out = dir.appendingPathComponent("\(pack)-\(drawing).gif")+ if FileManager.default.fileExists(atPath: out.path) { return out }+ guard let data = AnimationPacksPlugin.packData(pack, drawing) else { return nil }+ return ClipCodec.makeGif(data: data, to: out, maxPixels: 160) ? out : nil}- return nil+ guard let path = clip["path"] as? String, !path.hasPrefix("/"), !path.contains(".."),+ ["webp", "gif", "mp4"].contains((path as NSString).pathExtension.lowercased()) else { return nil }+ let source = Bundle.main.bundleURL.appendingPathComponent("public").appendingPathComponent(path)+ guard FileManager.default.isReadableFile(atPath: source.path) else { return nil }+ return gif(for: source, named: exId)}/// A small looping GIF of a clip (160 px), cached by exercise: an animated WebP or GIF frame bydiff --git a/frontend/ios/App/WatchCore/Sources/WatchCore/ClipCodec.swift b/frontend/ios/App/WatchCore/Sources/WatchCore/ClipCodec.swiftindex 66347e590..d911773c3 100644--- a/frontend/ios/App/WatchCore/Sources/WatchCore/ClipCodec.swift+++ b/frontend/ios/App/WatchCore/Sources/WatchCore/ClipCodec.swift@@ -38,6 +38,16 @@ enum ClipCodec {/// Frames and delays of an animated (or still) image file, each scaled to `maxPixels` at most.static func decode(_ url: URL, maxPixels: Int? = nil) -> Frames? {guard let src = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }+ return decode(src, maxPixels: maxPixels)+ }++ /// The same from bytes in memory (a file of a downloaded animation pack, read by the phone).+ static func decode(data: Data, maxPixels: Int? = nil) -> Frames? {+ guard let src = CGImageSourceCreateWithData(data as CFData, nil) else { return nil }+ return decode(src, maxPixels: maxPixels)+ }++ private static func decode(_ src: CGImageSource, maxPixels: Int?) -> Frames? {let count = CGImageSourceGetCount(src)guard count > 0 else { return nil }var images: [CGImage] = []@@ -68,7 +78,15 @@ enum ClipCodec {/// The phone's conversion: an animated WebP or GIF into a small looping GIF.static func makeGif(from source: URL, to out: URL, maxPixels: Int = 160) -> Bool {- guard let frames = decode(source, maxPixels: maxPixels) else { return false }+ write(decode(source, maxPixels: maxPixels), to: out)+ }++ static func makeGif(data: Data, to out: URL, maxPixels: Int = 160) -> Bool {+ write(decode(data: data, maxPixels: maxPixels), to: out)+ }++ private static func write(_ frames: Frames?, to out: URL) -> Bool {+ guard let frames = frames else { return false }if writeGif(frames, to: out) { return true }try? FileManager.default.removeItem(at: out)return falsediff --git a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/ClipCodecTests.swift b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/ClipCodecTests.swiftindex b0e673753..5f5809303 100644--- a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/ClipCodecTests.swift+++ b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/ClipCodecTests.swift@@ -28,6 +28,14 @@ final class ClipCodecTests: XCTestCase {XCTAssertEqual(back.duration, source.duration, accuracy: 0.1)}+ func testFromBytesInMemoryAsAPackFileIsRead() throws {+ let data = try Data(contentsOf: try fixture())+ let out = FileManager.default.temporaryDirectory.appendingPathComponent("clip-\(UUID().uuidString).gif")+ defer { try? FileManager.default.removeItem(at: out) }+ XCTAssertTrue(ClipCodec.makeGif(data: data, to: out))+ XCTAssertGreaterThan(try XCTUnwrap(ClipCodec.decode(out)).images.count, 5)+ }+func testSomethingThatIsNotAnImageGivesNothing() throws {let junk = FileManager.default.temporaryDirectory.appendingPathComponent("junk-\(UUID().uuidString).webp")try Data("not an image".utf8).write(to: junk)diff --git a/frontend/src/lib/watch-sync.js b/frontend/src/lib/watch-sync.jsindex 558fc7deb..0fd9691bf 100644--- a/frontend/src/lib/watch-sync.js+++ b/frontend/src/lib/watch-sync.js@@ -110,8 +110,8 @@ export function buildSnapshot(S, { timer = null, ready = null } = {}) {/*** 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:+ * exercise: the smallest downloaded animation pack of the figure (240 px, `packId` + `drawing`, read+ * through Background Assets on the phone) 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.@@ -121,15 +121,16 @@ 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(/\/$/, '') : null+ const pid = packId(WATCH_PACK_PX, figure)+ const pack = packs[pid] && packs[pid].state === 'ready' ? pid : 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) 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 }+ // a pack's files are read through Background Assets on the phone, never by a path+ if (pack && drawing && /^[A-Za-z0-9_-]+$/.test(drawing)) { out.push({ exId: e.id, packId: pack, drawing }); 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 })diff --git a/frontend/src/lib/watch-sync.test.js b/frontend/src/lib/watch-sync.test.jsindex a7862c70b..6253f6d40 100644--- a/frontend/src/lib/watch-sync.test.js+++ b/frontend/src/lib/watch-sync.test.js@@ -177,20 +177,24 @@ describe('the workout\'s animations for the watch', () => {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|webp)$/)- expect(c.file).toBeUndefined()+ expect(c.packId).toBeUndefined()}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 packs = { 'anim-240-male': { state: 'ready', base: 'opengym-pack://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$/)+ for (const c of clips) {+ expect(c.packId).toBe('anim-240-male')+ expect(c.drawing).toMatch(/^[A-Za-z0-9_-]+$/)+ expect(c.path).toBeUndefined()+ }// 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()+ expect(clipsFor(state(), { 'anim-240-female': { state: 'ready' } })[0].path).toBeDefined()+ expect(clipsFor(state(), { 'anim-240-male': { state: 'downloading' } })[0].path).toBeDefined()})})