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.

fix(watch): heart rate for sets ticked on the phone while the watch runs the session

Duarte Santos533dcbf4632b
diff --git a/frontend/ios/App/Watch/SessionStore.swift b/frontend/ios/App/Watch/SessionStore.swift
index 392abcb80..9d58e9fea 100644
--- a/frontend/ios/App/Watch/SessionStore.swift
+++ b/frontend/ios/App/Watch/SessionStore.swift
@@ -338,6 +338,10 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
self.health.start(workoutId: next.workout?.id)
}
if next.workout != nil && next.workout != before.workout { self.noteActivity() }
+ // Sets ticked on the phone: their heart rate from this watch's readings (Discord fuckdv).
+ for tick in SessionCore.phoneTicks(before.workout, next.workout) {
+ self.phoneTicked(tick, wid: next.workout?.id ?? "", resting: snap.active?.rest != nil)
+ }
if let rest = snap.active?.rest, next.workout?.id == snap.active?.id {
let end = Date(timeIntervalSince1970: rest.endsAt / 1000)
if end != self.restEndsAt { self.startRest(until: end, notify: true) }
@@ -410,6 +414,27 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
loadTargets()
}
+ /// A set ticked on the phone while this watch runs the session: the watch has the sensor, so
+ /// the set's heart rate is worked out here as for a tick on the wrist (completeSet), from when
+ /// the set started up to the phone's tick, and sent as a setHr. The rest before it closes at
+ /// the tick, and the rest after it (when the phone started one) is measured for that set.
+ private func phoneTicked(_ t: SessionCore.PhoneTick, wid: String, resting: Bool) {
+ let at = Date(timeIntervalSince1970: t.at / 1000)
+ // A tick from before this set started (one the phone made while the watch was away, already
+ // past) or from the future (a clock off between the two): no window to measure.
+ guard at > setStartedAt, at <= Date().addingTimeInterval(60) else { return }
+ let setFrom = setStartedAt
+ closeRest(at: at)
+ if let hr = health.stats(from: setFrom, to: at) {
+ send(event: [
+ "id": UUID().uuidString, "type": "setHr", "wid": wid, "exId": t.exId,
+ "entryIdx": t.entryIdx, "setIdx": t.setIdx, "hr": hr,
+ ])
+ }
+ setStartedAt = at
+ if resting { restAfter = (t.exId, t.entryIdx, t.setIdx, wid, at) }
+ }
+
/// A set passed over (a long press on Done): not done, the next one comes up.
func skipSet() {
guard let si = setIndex else { return }
diff --git a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
index a71e79592..87dd652f1 100644
--- a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
+++ b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
@@ -229,6 +229,36 @@ enum SessionCore {
return snap.discarded?.contains(wid) ?? false
}
+ /// A set the phone ticked, which the watch did not: its heart rate is the watch's to work out
+ /// (the phone has no sensor), from the watch session's readings up to `at`.
+ struct PhoneTick: Equatable {
+ var exId: String
+ var entryIdx: Int
+ var setIdx: Int
+ /// When the phone ticked it (ms since 1970).
+ var at: Double
+ }
+
+ /// The sets done in `after` and not in `before`, the same workout on both sides, oldest tick
+ /// first. A set the watch ticked itself is done in `before` already (tick), so only the phone's
+ /// show up here; a set without the phone's `at` is left out (no time to measure up to).
+ /// One side of a per-side set counts too: the row's `at` is that side's tick.
+ static func phoneTicks(_ before: ActiveWorkout?, _ after: ActiveWorkout?) -> [PhoneTick] {
+ guard let b = before, let a = after, let wid = a.id, wid == b.id else { return [] }
+ var out: [PhoneTick] = []
+ for (ei, e) in a.entries.enumerated() {
+ guard b.entries.indices.contains(ei), b.entries[ei].id == e.id else { continue }
+ let old = b.entries[ei].sets
+ for (si, row) in e.sets.enumerated() {
+ guard let at = row.at, old.indices.contains(si) else { continue }
+ let was = old[si]
+ let newSide = ["L", "R"].contains { row.sides?[$0].done == true && was.sides?[$0].done != true }
+ if (row.done && !was.done) || newSide { out.append(PhoneTick(exId: e.id, entryIdx: ei, setIdx: si, at: at)) }
+ }
+ }
+ return out.sorted { $0.at < $1.at }
+ }
+
/// Of the deleted workouts the phone names, the ones this watch has not taken out of Apple Health
/// yet (`done`: already removed, or found to have no copy of the watch's). Only workout ids.
static func healthToDelete(gone: [String]?, done: [String]) -> [String] {
diff --git a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/SessionCoreTests.swift b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/SessionCoreTests.swift
index 17b12d2e5..9bab51019 100644
--- a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/SessionCoreTests.swift
+++ b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/SessionCoreTests.swift
@@ -125,4 +125,27 @@ final class SessionCoreTests: XCTestCase {
let json = #"{"v":1,"active":null,"discarded":["p1"]}"#
XCTAssertTrue(SessionCore.discarded("p1", in: try JSONDecoder().decode(Snapshot.self, from: Data(json.utf8))))
}
+
+ // Discord fuckdv: a set ticked on the phone while the watch runs the session got no heart rate.
+ func testFindsTheSetsThePhoneTickedButNotTheWatchsOwn() {
+ var s = SessionCore.applySnapshot(SessionState(), snap(workout("p1", sets: [row(60, 8), row(60, 8), row(60, 8)])))
+ s = SessionCore.tick(s, entry: 0, set: 0, w: 60, r: 8, at: 1000, id: "e1").0
+ let before = s.workout
+ let phone = workout("p1", sets: [row(60, 8, done: true, at: 1000), row(60, 8, done: true, at: 3000), row(60, 8, done: true)])
+ s = SessionCore.applySnapshot(s, snap(phone))
+ let ticks = SessionCore.phoneTicks(before, s.workout)
+ XCTAssertEqual(ticks, [SessionCore.PhoneTick(exId: "0025", entryIdx: 0, setIdx: 1, at: 3000)],
+ "the watch's own tick and a set without a time are left out")
+ XCTAssertEqual(SessionCore.phoneTicks(s.workout, s.workout), [])
+ XCTAssertEqual(SessionCore.phoneTicks(before, workout("p2", sets: [row(60, 8, done: true, at: 5)])), [], "another workout")
+ }
+
+ func testOneSideTickedOnThePhoneCounts() {
+ var w = workout("p1", sets: [row(20, 0)])
+ w.entries[0].sets[0].sides = Sides(L: SideRow(w: 20, r: 10, done: false), R: SideRow(w: 20, r: 10, done: false))
+ let before = SessionCore.applySnapshot(SessionState(), snap(w)).workout
+ var after = w
+ after.entries[0].sets[0] = SessionCore.markSide(w.entries[0].sets[0], "L", w: 20, r: 10, at: 2000)
+ XCTAssertEqual(SessionCore.phoneTicks(before, after).map(\.at), [2000])
+ }
}