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: ignore taps for a second after the page turns by itself

Adrián Fernández5b81e81f9359
diff --git a/frontend/ios/App/Watch/SessionStore.swift b/frontend/ios/App/Watch/SessionStore.swift
index b14001f4d..d81b5cb4c 100644
--- a/frontend/ios/App/Watch/SessionStore.swift
+++ b/frontend/ios/App/Watch/SessionStore.swift
@@ -26,7 +26,11 @@ struct ClipFrames {
final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserNotificationCenterDelegate {
/// Everything the watch knows about the session, saved after every change.
@Published private(set) var state = SessionState() {
- didSet { if !isDemo, state != oldValue { save() } }
+ didSet {
+ if !isDemo, state != oldValue { save() }
+ // The page changed without a swipe (Done moved on, a snapshot): a moment without taps.
+ if state.entryIndex != oldValue.entryIndex { touchLock.lock(at: Date()) }
+ }
}
var workout: ActiveWorkout? { state.workout }
@@ -98,6 +102,9 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
private var setStartedAt = Date()
/// The last Done, against a double tap.
private var lastTick: Date?
+ /// Taps ignored for a second after the watch turns the page by itself (WatchCore's TouchLock).
+ private var touchLock = TouchLock()
+ private var touchAllowed: Bool { touchLock.allows(at: Date()) }
/// When this watch first had the running workout (its start, for a phone that sends none).
private var workoutSeenAt: Date?
/// The rest running after a set, for that rest's heart rate once it is over.
@@ -244,6 +251,7 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
guard ei != state.entryIndex else { return }
ackRestOver()
state.entryIndex = ei
+ touchLock.unlock() // swiped by hand: the page is the lifter's own choice
loadTargets()
}
@@ -375,6 +383,8 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
guard let w = workout, let e = entry, let si = setIndex else { return }
// A double tap on Done (a sweaty finger) is one set, not this one and the next.
if heldFor == nil, let last = lastTick, Date().timeIntervalSince(last) < 0.7 { return }
+ // In a superset the page flips to the partner: a slower second tap would log its set.
+ if heldFor == nil, !touchAllowed { return }
lastTick = Date()
ackRestOver()
let ei = entryIndex
@@ -438,7 +448,7 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
/// A set passed over (a long press on Done): not done, the next one comes up.
func skipSet() {
- guard let si = setIndex else { return }
+ guard touchAllowed, let si = setIndex else { return }
ackRestOver()
let ei = entryIndex
state = SessionCore.skip(state, entry: ei, set: si)
@@ -456,7 +466,7 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
/// The hold starts: a countdown from the seconds on the screen; at zero it is done by itself.
func startHold() {
- guard entry?.isTimed == true, setIndex != nil, seconds > 0 else { return }
+ guard touchAllowed, entry?.isTimed == true, setIndex != nil, seconds > 0 else { return }
ackRestOver()
skipRest()
noteActivity()
@@ -630,6 +640,8 @@ final class SessionStore: NSObject, ObservableObject, WCSessionDelegate, UNUserN
restTimer = Timer.scheduledTimer(withTimeInterval: end.timeIntervalSinceNow, repeats: false) { [weak self] _ in
DispatchQueue.main.async { self?.restOver() }
DispatchQueue.main.async {
+ // The set page comes back under the finger: a tap meant for the rest is not a Done.
+ self?.touchLock.lock(at: Date())
self?.closeRest(at: Date())
self?.restEndsAt = nil
self?.restStartedAt = nil
diff --git a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
index c5659a013..b314f166d 100644
--- a/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
+++ b/frontend/ios/App/WatchCore/Sources/WatchCore/SessionCore.swift
@@ -385,3 +385,15 @@ struct WorkoutSummary: Codable, Equatable {
/// The phone ended it (finished or discarded there) while the watch had it running.
var endedOnPhone: Bool
}
+
+/// After the watch turns the page by itself (a superset's partner, the next exercise, the end of a
+/// rest), taps are ignored for a second: a second Done meant for the page that just left would
+/// otherwise land on the new one and log a set nobody did. A swipe by hand unlocks it at once.
+struct TouchLock: Equatable {
+ static let seconds: TimeInterval = 1
+ private(set) var until = Date.distantPast
+
+ mutating func lock(at t: Date) { until = t.addingTimeInterval(TouchLock.seconds) }
+ mutating func unlock() { until = .distantPast }
+ func allows(at t: Date) -> Bool { t >= until }
+}
diff --git a/frontend/ios/App/WatchCore/Tests/WatchCoreTests/TouchLockTests.swift b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/TouchLockTests.swift
new file mode 100644
index 000000000..1d7515813
--- /dev/null
+++ b/frontend/ios/App/WatchCore/Tests/WatchCoreTests/TouchLockTests.swift
@@ -0,0 +1,22 @@
+import XCTest
+@testable import WatchCore
+
+/// The touch lock after a page the watch turned by itself.
+final class TouchLockTests: XCTestCase {
+ func testATapWithinASecondOfAnAutomaticPageChangeIsIgnored() {
+ let t = Date(timeIntervalSince1970: 1000)
+ var lock = TouchLock()
+ XCTAssertTrue(lock.allows(at: t), "nothing locked before the first page change")
+ lock.lock(at: t)
+ XCTAssertFalse(lock.allows(at: t.addingTimeInterval(0.8)), "a slower second tap (0.7 to 1 s) is still ignored")
+ XCTAssertTrue(lock.allows(at: t.addingTimeInterval(1)), "after a second the new page takes taps")
+ }
+
+ func testASwipeByHandUnlocksAtOnce() {
+ let t = Date(timeIntervalSince1970: 1000)
+ var lock = TouchLock()
+ lock.lock(at: t)
+ lock.unlock()
+ XCTAssertTrue(lock.allows(at: t.addingTimeInterval(0.1)))
+ }
+}