Skip to content

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.

Watch: the running workout's animations, sent from the phone

Duarte Santos5c8497b2a8a3
diff --git a/frontend/ios/App/App/WatchBridgePlugin.swift b/frontend/ios/App/App/WatchBridgePlugin.swift
index 07b34ab6c..d606f9297 100644
--- a/frontend/ios/App/App/WatchBridgePlugin.swift
+++ b/frontend/ios/App/App/WatchBridgePlugin.swift
@@ -1,5 +1,8 @@
+import AVFoundation
import Capacitor
import Foundation
+import ImageIO
+import UniformTypeIdentifiers
import WatchConnectivity
/**
@@ -24,6 +27,12 @@ import WatchConnectivity
* const { events } = await WatchBridge.drain()
* await WatchBridge.ack({ ids })
* WatchBridge.addListener('watchEvent', e => ...)
+ * 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.
*/
@objc(WatchBridgePlugin)
public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {
@@ -34,6 +43,7 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {
CAPPluginMethod(name: "sendSnapshot", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "drain", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ack", returnType: CAPPluginReturnPromise),
+ CAPPluginMethod(name: "sendClips", returnType: CAPPluginReturnPromise),
]
private let queue = DispatchQueue(label: "ch.opengym.watch-events")
@@ -103,6 +113,67 @@ public class WatchBridgePlugin: CAPPlugin, CAPBridgedPlugin, WCSessionDelegate {
}
}
+ /// The clips already on their way for each workout, by exercise.
+ private var clipsSent: [String: Set<String>] = [:]
+ private let clipQueue = DispatchQueue(label: "ch.opengym.watch-clips", qos: .utility)
+
+ @objc func sendClips(_ call: CAPPluginCall) {
+ guard let wid = call.getString("wid"), WCSession.isSupported() else { call.resolve(["sent": 0]); return }
+ let s = WCSession.default
+ guard s.activationState == .activated, s.isPaired, s.isWatchAppInstalled else { call.resolve(["sent": 0]); return }
+ let clips = (call.getArray("clips") ?? []).compactMap { $0 as? [String: Any] }
+ call.resolve(["queued": clips.count])
+ clipQueue.async {
+ 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 }
+ s.transferFile(gif, metadata: ["exId": exId, "wid": wid])
+ sent.insert(exId)
+ }
+ self.clipsSent[wid] = sent
+ }
+ }
+
+ /// 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? {
+ 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)
+ let seconds = CMTimeGetSeconds(asset.duration)
+ guard seconds.isFinite, seconds > 0 else { return nil }
+ let gen = AVAssetImageGenerator(asset: asset)
+ gen.appliesPreferredTrackTransform = true
+ gen.maximumSize = CGSize(width: 160, height: 160)
+ gen.requestedTimeToleranceBefore = .zero
+ gen.requestedTimeToleranceAfter = .zero
+ let count = max(2, min(30, Int(seconds * 10)))
+ let step = seconds / Double(count)
+ guard 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 frameProps = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: step]] as CFDictionary
+ var added = 0
+ for i in 0..<count {
+ let t = CMTime(seconds: step * Double(i), preferredTimescale: 600)
+ if let img = try? gen.copyCGImage(at: t, actualTime: nil) {
+ CGImageDestinationAddImage(dest, img, frameProps)
+ 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/ios/App/Watch/OpenGymWatchApp.swift b/frontend/ios/App/Watch/OpenGymWatchApp.swift
index 759bf3a10..163313a7b 100644
--- a/frontend/ios/App/Watch/OpenGymWatchApp.swift
+++ b/frontend/ios/App/Watch/OpenGymWatchApp.swift
@@ -163,6 +163,9 @@ struct ExercisePage: View {
HeartRateBadge(health: store.health)
}
}
+ if let clip = store.clips[entry.id] {
+ ClipView(clip: clip).frame(height: 54)
+ }
SetDots(entry: entry, current: si) { store.pick(set: $0, in: index) }
if let si = si {
let target = entry.sets[si]
@@ -367,3 +370,21 @@ struct HealthToggle: View {
.font(.footnote)
}
}
+
+/* ---------- an exercise's animation, sent by the phone for this workout ---------- */
+
+struct ClipView: View {
+ let clip: ClipFrames
+
+ var body: some View {
+ TimelineView(.animation(minimumInterval: 1.0 / 12)) { ctx in
+ let t = ctx.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: max(0.1, clip.duration))
+ let i = min(clip.images.count - 1, Int(t / max(0.1, clip.duration) * Double(clip.images.count)))
+ Image(uiImage: clip.images[i])
+ .resizable()
+ .scaledToFit()
+ .clipShape(RoundedRectangle(cornerRadius: 8))
+ }
+ .accessibilityHidden(true)
+ }
+}
diff --git a/frontend/ios/App/Watch/SessionStore.swift b/frontend/ios/App/Watch/SessionStore.swift
index c5ad2d07a..3aebc8811 100644
--- a/frontend/ios/App/Watch/SessionStore.swift
+++ b/frontend/ios/App/Watch/SessionStore.swift
@@ -1,4 +1,6 @@
import Foundation
+import ImageIO
+import UIKit
import UserNotifications
import WatchConnectivity
import WatchKit
@@ -10,9 +12,17 @@ import WatchKit
* here goes back as an event with transferUserInfo, which iOS delivers even when the phone app is
* not running, and the phone applies it through its own workout screen.
*
- * Text only: no exercise animations or pictures on the watch.
+ * Exercise animations: never part of the watch app. The phone sends the running workout's own,
+ * as small GIF files (WatchBridgePlugin.sendClips); they are kept here only while that workout runs
+ * and deleted when it ends. Without them the page shows the exercise by name.
*/
+/// One animation, as frames to cycle through.
+struct ClipFrames {
+ let images: [UIImage]
+ let duration: Double
+}
+
struct Snapshot: Codable {
let v: Int
let unit: String?
@@ -76,6 +86,11 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
@Published private(set) var ready: Ready?
/// Apple Health: the workout session, its heart rate, and the switch (WorkoutManager).
let health = WorkoutManager()
+ /// The running workout's animations, by exercise id, from the phone.
+ @Published private(set) var clips: [String: ClipFrames] = [:]
+ private static var clipDir: URL {
+ FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("clips")
+ }
/// When the set now up began: the end of the rest before it, or the start of the session.
private var setStartedAt = Date()
/// The rest running after a set, for that rest's heart rate once it is over.
@@ -185,6 +200,7 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
// A session started here that the phone has not heard of yet stays.
if self.workout != nil && self.workout?.id == self.startedHere { return }
if self.workout != nil { self.health.end(save: true) }
+ self.dropClips()
self.workout = nil
self.pending.removeAll()
self.skipRest()
@@ -299,6 +315,7 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
"at": Date().timeIntervalSince1970 * 1000,
])
skipRest()
+ dropClips()
workout = nil
startedHere = nil
pending.removeAll()
@@ -397,6 +414,51 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
WCSession.default.transferUserInfo(["event": json])
}
+ /* ---------- the animations ---------- */
+
+ /// The workout is over: its animations go, from memory and from the disk.
+ func dropClips() {
+ clips = [:]
+ try? FileManager.default.removeItem(at: SessionStore.clipDir)
+ }
+
+ static func decode(_ url: URL) -> ClipFrames? {
+ guard let src = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }
+ let count = CGImageSourceGetCount(src)
+ guard count > 0 else { return nil }
+ var images: [UIImage] = []
+ var total = 0.0
+ for i in 0..<count {
+ guard let cg = CGImageSourceCreateImageAtIndex(src, i, nil) else { continue }
+ images.append(UIImage(cgImage: cg))
+ let props = CGImageSourceCopyPropertiesAtIndex(src, i, nil) as? [String: Any]
+ let gif = props?[kCGImagePropertyGIFDictionary as String] as? [String: Any]
+ let delay = (gif?[kCGImagePropertyGIFUnclampedDelayTime as String] as? Double) ?? (gif?[kCGImagePropertyGIFDelayTime as String] as? Double) ?? 0.1
+ total += delay > 0.01 ? delay : 0.1
+ }
+ return images.isEmpty ? nil : ClipFrames(images: images, duration: total)
+ }
+
+ func session(_ session: WCSession, didReceive file: WCSessionFile) {
+ // The file is gone once this returns: moved into the cache now.
+ guard let exId = file.metadata?["exId"] as? String else { return }
+ let dir = SessionStore.clipDir
+ try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ let dest = dir.appendingPathComponent(exId.replacingOccurrences(of: "/", with: "_") + ".gif")
+ try? FileManager.default.removeItem(at: dest)
+ guard (try? FileManager.default.moveItem(at: file.fileURL, to: dest)) != nil,
+ let frames = SessionStore.decode(dest) else { return }
+ let wid = file.metadata?["wid"] as? String
+ DispatchQueue.main.async {
+ // Only for the workout running here; a late file of an earlier one is dropped.
+ guard self.workout != nil, wid == nil || wid == self.workout?.id else {
+ try? FileManager.default.removeItem(at: dest)
+ return
+ }
+ self.clips[exId] = frames
+ }
+ }
+
/* ---------- WCSessionDelegate ---------- */
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
diff --git a/frontend/src/lib/watch-sync.js b/frontend/src/lib/watch-sync.js
index eadd75d10..7aeceffe2 100644
--- a/frontend/src/lib/watch-sync.js
+++ b/frontend/src/lib/watch-sync.js
@@ -15,7 +15,7 @@
// 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 } from './exercises.js'
+import { EXIDX, gifSrc, figureOf, isVideoSrc } from './exercises.js'
import { exerciseNameFor } from './i18n.js'
import { modeOf, setLabel, lastEntryFor, effectiveRoutineIds } from './history.js'
import { isWarmupRow } from './workout-model.js'
@@ -103,6 +103,27 @@ 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.
+ */
+export function clipsFor (S) {
+ const A = S && S.active
+ if (!A || !Array.isArray(A.entries)) return []
+ const figure = figureOf(S)
+ const 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('..')) continue
+ seen.add(e.id)
+ out.push({ exId: e.id, path })
+ }
+ return out
+}
+
/* ---------- applying the watch's events ---------- */
let handlers = null // the workout screen's: setDone, finish
@@ -222,7 +243,7 @@ export async function startWatchSync ({ useStore, useUI }) {
const p = await watchPlugin()
if (!p) return () => {}
getStateRef = () => useStore.getState()
- let lastSent = null, tm = null
+ let lastSent = null, lastClips = null, tm = null
// The ready sessions only change with the plan, the log or the day: built again only then.
let readyKey = null, ready = null
const readyFor = S => {
@@ -240,9 +261,17 @@ export async function startWatchSync ({ useStore, useUI }) {
if (!st || !st.paired || !st.installed) return
const S = useStore.getState().S
const json = JSON.stringify(buildSnapshot(S, { timer: useUI.getState().timer, ready: readyFor(S) }))
- if (json === lastSent) return
- const r = await p.WatchBridge.sendSnapshot({ json })
- if (r && r.sent) lastSent = json
+ if (json !== lastSent) {
+ const r = await p.WatchBridge.sendSnapshot({ json })
+ if (r && r.sent) lastSent = json
+ }
+ // The workout's animations, once per workout and exercise (the native side remembers).
+ const clips = clipsFor(S)
+ const key = S.active ? S.active.id + '|' + clips.map(c => c.exId).join(',') : ''
+ if (clips.length && key !== lastClips) {
+ await p.WatchBridge.sendClips({ wid: S.active.id, clips })
+ lastClips = key
+ }
} catch { /* no watch, or not now: the next change tries again */ }
}
const schedule = () => { clearTimeout(tm); tm = setTimeout(send, SEND_DEBOUNCE_MS) }
diff --git a/frontend/src/lib/watch-sync.test.js b/frontend/src/lib/watch-sync.test.js
index ad4c1f234..3d098835d 100644
--- a/frontend/src/lib/watch-sync.test.js
+++ b/frontend/src/lib/watch-sync.test.js
@@ -168,3 +168,17 @@ describe('heart rate from the watch', () => {
expect(patchSet).toHaveBeenCalledWith({ wid: 'w-1', exId: '0025', entryIdx: 0, setIdx: 1 }, { restHr: { avg: 112, max: 128 } })
})
})
+
+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 () => {
+ const { clipsFor } = await import('./watch-sync.js')
+ const { EXIDX } = await import('./exercises.js')
+ const S = state()
+ const clips = clipsFor(S)
+ 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(clipsFor({ active: null })).toEqual([])
+ })
+})