This commit is contained in:
Trey t
2023-07-08 11:48:14 -05:00
parent 5ca99cf832
commit 0b477bc182
5 changed files with 168 additions and 121 deletions

View File

@@ -18,6 +18,12 @@ enum WatchActions: Codable {
case workoutComplete(Data)
}
enum PhoneToWatchActions: Codable {
case inExercise(WatchPackageModel)
case reset
case endWorkout
}
class BridgeModule: NSObject, ObservableObject {
private let kMessageKey = "message"
@@ -100,10 +106,7 @@ class BridgeModule: NSObject, ObservableObject {
self.workoutStartDate = nil
self.workoutEndDate = nil
}
let watchModel = WatchPackageModel(currentExerciseName: "", currentTimeLeft: -100, workoutStartDate: Date())
let data = try! JSONEncoder().encode(watchModel)
send(data)
sendResetToWatch()
}
private func startWorkoutTimer() {
@@ -134,11 +137,7 @@ class BridgeModule: NSObject, ObservableObject {
@objc func updateCurrentExerciseTimer() {
if currentExerciseTimeLeft > 0 {
currentExerciseTimeLeft -= 1
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date())
let data = try! JSONEncoder().encode(watchModel)
send(data)
if currentExerciseTimeLeft == 0 {
playFinished()
} else {
@@ -146,6 +145,7 @@ class BridgeModule: NSObject, ObservableObject {
playBeep()
}
}
sendCurrentExerciseToWatch()
} else {
nextExercise()
}
@@ -209,25 +209,16 @@ class BridgeModule: NSObject, ObservableObject {
if let duration = exercise.duration,
duration > 0 {
print(duration)
self.startExerciseTimerWith(duration: duration)
} else {
var intWatchDispaly = -1
if let reps = self.currentExercise?.reps,
reps > 0 {
intWatchDispaly = reps
}
// if not a timer we need to set the watch display with number of reps
// if timer it will set when timer updates
let watchModel = WatchPackageModel(currentExerciseName: self.currentExercise?.exercise.name ?? "-", currentTimeLeft: intWatchDispaly, workoutStartDate: self.workoutStartDate ?? Date())
let data = try! JSONEncoder().encode(watchModel)
self.send(data)
}
self.sendCurrentExerciseToWatch()
}
}
func completeWorkout() {
self.currentExerciseTimer?.invalidate()
self.currentExerciseTimer = nil
workoutEndDate = Date()
//if connected to watch
@@ -276,12 +267,41 @@ class BridgeModule: NSObject, ObservableObject {
}
extension BridgeModule: WCSessionDelegate {
func sendWorkoutCompleteToWatch() {
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date(), workoutEndDate: Date())
func sendResetToWatch() {
let watchModel = PhoneToWatchActions.reset
let data = try! JSONEncoder().encode(watchModel)
send(data)
}
func sendWorkoutCompleteToWatch() {
let model = PhoneToWatchActions.endWorkout
let data = try! JSONEncoder().encode(model)
send(data)
}
func sendCurrentExerciseToWatch() {
if let duration = currentExercise?.duration,
duration > 0 {
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date())
let model = PhoneToWatchActions.inExercise(watchModel)
let data = try! JSONEncoder().encode(model)
send(data)
} else {
var intWatchDispaly = -1
if let reps = self.currentExercise?.reps,
reps > 0 {
intWatchDispaly = reps
}
// if not a timer we need to set the watch display with number of reps
// if timer it will set when timer updates
let watchModel = WatchPackageModel(currentExerciseName: self.currentExercise?.exercise.name ?? "-", currentTimeLeft: intWatchDispaly, workoutStartDate: self.workoutStartDate ?? Date())
let model = PhoneToWatchActions.inExercise(watchModel)
let data = try! JSONEncoder().encode(model)
self.send(data)
}
}
func session(_ session: WCSession, didReceiveMessageData messageData: Data) {
if let model = try? JSONDecoder().decode(WatchActions.self, from: messageData) {
switch model {