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): a forgotten-workout check counts phone changes and running rests (#790 follow-up)

Duarte Santosa5d5da2a5194
diff --git a/frontend/ios/App/Watch/SessionStore.swift b/frontend/ios/App/Watch/SessionStore.swift
index cefca5833..e168dcd0d 100644
--- a/frontend/ios/App/Watch/SessionStore.swift
+++ b/frontend/ios/App/Watch/SessionStore.swift
@@ -660,12 +660,15 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
}
private func checkIdle() {
- guard let w = workout, holdEndsAt == nil else { return }
+ // A rest or a hold running is a workout going on, however long it takes.
+ guard let w = workout, holdEndsAt == nil, restEndsAt == nil else { return }
let now = Date().timeIntervalSince1970 * 1000
- let seen = workoutSeenAt.map { $0.timeIntervalSince1970 * 1000 }
+ let ms = { (d: Date?) in d.map { $0.timeIntervalSince1970 * 1000 } }
+ let seen = ms(workoutSeenAt)
let last = Idle.lastSet(w) ?? seen ?? now
- // Picked up from the phone or answered "keep going" since: counted from then, never ended earlier.
- let since = [last, seen ?? 0, idleSnooze.map { $0.timeIntervalSince1970 * 1000 } ?? 0].max() ?? last
+ // Picked up from the phone, changed there (a set edited, an exercise added) or answered
+ // "keep going" since: counted from then, never ended earlier.
+ let since = Idle.since(lastSet: last, seen: seen, activity: ms(activityAt), snooze: ms(idleSnooze))
switch Idle.state(since: since, now: now) {
case .active: break
case .ask:
diff --git a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
index 0e416dc49..7850978f5 100644
--- a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
+++ b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
@@ -425,6 +425,13 @@ enum Idle: Equatable {
return ticks.max() ?? w.start.flatMap { $0 > 0 ? $0 : nil }
}
+ /// What idle time counts from: the newest of the last set, when this watch picked the workout
+ /// up, the last change to it from either side (SessionStore's activityAt, the same one
+ /// SensorPlan uses) and a "keep going" answered.
+ static func since(lastSet: Double, seen: Double?, activity: Double?, snooze: Double?) -> Double {
+ [lastSet, seen ?? 0, activity ?? 0, snooze ?? 0].max() ?? lastSet
+ }
+
/// `since`: the last thing that counts (the last set, or a "keep going" answered since).
static func state(since: Double, now: Double) -> Idle {
let idle = now - since
diff --git a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/IdleTests.swift b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/IdleTests.swift
index efa270567..5c392971a 100644
--- a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/IdleTests.swift
+++ b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/IdleTests.swift
@@ -35,4 +35,15 @@ final class IdleTests: XCTestCase {
XCTAssertEqual(Idle.lastSet(workout(start: 1000, ats: [nil, nil])), 1000)
XCTAssertNil(Idle.lastSet(workout(start: 0, ats: [nil])), "a phone that sent no start: the watch's own time then")
}
+
+ func testAChangeFromThePhoneCountsAsTheWorkoutGoingOn() {
+ // Last set at 0, the phone edited the workout at 30 min: asked at 50, not at 20; ended at 75.
+ let since = Idle.since(lastSet: 0, seen: nil, activity: 30 * min, snooze: nil)
+ XCTAssertEqual(since, 30 * min)
+ XCTAssertEqual(Idle.state(since: since, now: 25 * min), .active)
+ XCTAssertEqual(Idle.state(since: since, now: 50 * min), .ask)
+ XCTAssertEqual(Idle.state(since: since, now: 75 * min), .end)
+ XCTAssertEqual(Idle.since(lastSet: 10 * min, seen: 2 * min, activity: nil, snooze: 40 * min), 40 * min, "keep going")
+ XCTAssertEqual(Idle.since(lastSet: 10 * min, seen: nil, activity: nil, snooze: nil), 10 * min)
+ }
}