WIP
This commit is contained in:
@@ -18,28 +18,28 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
|
||||
static let shared = BridgeModule()
|
||||
@Published var isShowingOnExternalDisplay = false
|
||||
|
||||
private var timer: Timer?
|
||||
@Published var timeLeft: Int = 0
|
||||
|
||||
@Published var currentExercise: ExerciseElement?
|
||||
var currentWorkout: Workout?
|
||||
var currentExerciseIdx: Int = -1
|
||||
|
||||
var workoutStartDate: Date?
|
||||
|
||||
// workoutEndDate ties into WatchPackageModel.workoutEndDate which
|
||||
// tells the watch app to stop the workout
|
||||
var workoutEndDate: Date?
|
||||
|
||||
@Published var isInWorkout = false
|
||||
var completedWorkout: (() -> Void)?
|
||||
@Published var currentWorkoutRunTimeInSeconds: Int = -1
|
||||
private var currentWorkoutRunTimer: Timer?
|
||||
|
||||
@Published var isInWorkout = false
|
||||
var completedWorkoutFromWatch: (() -> Void)?
|
||||
var totalCaloire: Float?
|
||||
var heartRates: [Int]?
|
||||
|
||||
func start(workout: Workout, atExerciseIndex: Int = 0) {
|
||||
var currentWorkout: Workout?
|
||||
public private(set) var workoutStartDate: Date?
|
||||
|
||||
private var currentExerciseTimer: Timer?
|
||||
public private(set) var currentExerciseIdx: Int = -1
|
||||
@Published var currentExerciseTimeLeft: Int = 0
|
||||
@Published var currentExercise: ExerciseElement?
|
||||
|
||||
private var isWatchConnected = false
|
||||
// workoutEndDate fills out WatchPackageModel.workoutEndDate which
|
||||
// tells the watch app to stop the workout
|
||||
public private(set) var workoutEndDate: Date?
|
||||
public private(set) var totalCaloire: Float?
|
||||
public private(set) var heartRates: [Int]?
|
||||
|
||||
func start(workout: Workout) {
|
||||
self.currentWorkout = workout
|
||||
currentWorkoutRunTimeInSeconds = 0
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
@@ -58,23 +58,35 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func goToExerciseAt(index: Int) {
|
||||
guard let currentWorkout = currentWorkout else {
|
||||
return
|
||||
}
|
||||
|
||||
currentExerciseIdx = index
|
||||
let exercise = currentWorkout.exercises[index]
|
||||
updateCurrent(exercise: exercise)
|
||||
}
|
||||
|
||||
func resetCurrentWorkout() {
|
||||
currentWorkoutRunTimeInSeconds = 0
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
|
||||
currentWorkoutRunTimeInSeconds = -1
|
||||
currentExerciseIdx = -1
|
||||
|
||||
currentExercise = nil
|
||||
currentWorkout = nil
|
||||
|
||||
isInWorkout = false
|
||||
workoutStartDate = nil
|
||||
workoutEndDate = nil
|
||||
DispatchQueue.main.async {
|
||||
self.currentWorkoutRunTimeInSeconds = 0
|
||||
self.currentWorkoutRunTimer?.invalidate()
|
||||
self.currentWorkoutRunTimer = nil
|
||||
|
||||
self.currentExerciseTimer?.invalidate()
|
||||
self.currentExerciseTimer = nil
|
||||
|
||||
self.currentWorkoutRunTimeInSeconds = -1
|
||||
self.currentExerciseIdx = -1
|
||||
|
||||
self.currentExercise = nil
|
||||
self.currentWorkout = nil
|
||||
|
||||
self.isInWorkout = false
|
||||
self.workoutStartDate = nil
|
||||
self.workoutEndDate = nil
|
||||
}
|
||||
|
||||
let watchModel = WatchPackageModel(currentExerciseName: "", currentTimeLeft: -100, workoutStartDate: Date())
|
||||
let data = try! JSONEncoder().encode(watchModel)
|
||||
@@ -94,30 +106,26 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
|
||||
private func startTimerWith(duration: Int) {
|
||||
DispatchQueue.main.async {
|
||||
self.timer?.invalidate()
|
||||
self.timer = nil
|
||||
self.timeLeft = duration
|
||||
self.timer = Timer.scheduledTimer(timeInterval: 1,
|
||||
self.currentExerciseTimer?.invalidate()
|
||||
self.currentExerciseTimer = nil
|
||||
self.currentExerciseTimeLeft = duration
|
||||
self.currentExerciseTimer = Timer.scheduledTimer(timeInterval: 1,
|
||||
target: self,
|
||||
selector: #selector(self.updateCounter),
|
||||
selector: #selector(self.updateCurrentExerciseTimer),
|
||||
userInfo: nil,
|
||||
repeats: true)
|
||||
self.timer?.fire()
|
||||
self.currentExerciseTimer?.fire()
|
||||
}
|
||||
}
|
||||
|
||||
@objc func updateCounter() {
|
||||
if timeLeft > 0 {
|
||||
timeLeft -= 1
|
||||
@objc func updateCurrentExerciseTimer() {
|
||||
if currentExerciseTimeLeft > 0 {
|
||||
currentExerciseTimeLeft -= 1
|
||||
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: timeLeft, workoutStartDate: workoutStartDate ?? Date())
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date())
|
||||
let data = try! JSONEncoder().encode(watchModel)
|
||||
send(data)
|
||||
|
||||
} else {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
|
||||
nextExercise()
|
||||
}
|
||||
}
|
||||
@@ -129,7 +137,7 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
let nextExercise = currentWorkout.exercises[currentExerciseIdx]
|
||||
updateCurrent(exercise: nextExercise)
|
||||
} else {
|
||||
|
||||
completeWorkout()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,11 +155,22 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func completeWorkout() {
|
||||
workoutEndDate = Date()
|
||||
|
||||
//if connected to watch
|
||||
if WCSession.default.isReachable {
|
||||
self.sendWorkoutCompleteToWatch()
|
||||
} else {
|
||||
completedWorkout?()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BridgeModule: WCSessionDelegate {
|
||||
func sendWorkoutCompleteToWatch() {
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: timeLeft, workoutStartDate: workoutStartDate ?? Date(), workoutEndDate: Date())
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date(), workoutEndDate: Date())
|
||||
let data = try! JSONEncoder().encode(watchModel)
|
||||
send(data)
|
||||
}
|
||||
@@ -165,7 +184,7 @@ extension BridgeModule: WCSessionDelegate {
|
||||
let model = try! JSONDecoder().decode(WatchFinishWorkoutModel.self, from: data)
|
||||
totalCaloire = Float(model.totalBurnedEnergery)
|
||||
heartRates = model.allHeartRates
|
||||
completedWorkoutFromWatch?()
|
||||
completedWorkout?()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,16 +192,25 @@ extension BridgeModule: WCSessionDelegate {
|
||||
|
||||
func session(_ session: WCSession,
|
||||
activationDidCompleteWith activationState: WCSessionActivationState,
|
||||
error: Error?) {}
|
||||
|
||||
error: Error?) {
|
||||
switch activationState {
|
||||
case .notActivated:
|
||||
print("notActivated")
|
||||
case .inactive:
|
||||
print("inactive")
|
||||
case .activated:
|
||||
print("activated")
|
||||
}
|
||||
}
|
||||
#if os(iOS)
|
||||
func sessionDidBecomeInactive(_ session: WCSession) {}
|
||||
func sessionDidBecomeInactive(_ session: WCSession) {
|
||||
|
||||
}
|
||||
|
||||
func sessionDidDeactivate(_ session: WCSession) {
|
||||
session.activate()
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
func send(_ data: Data) {
|
||||
guard WCSession.default.activationState == .activated else {
|
||||
return
|
||||
@@ -196,8 +224,7 @@ extension BridgeModule: WCSessionDelegate {
|
||||
return
|
||||
}
|
||||
#endif
|
||||
WCSession.default.sendMessageData(data, replyHandler: nil)
|
||||
{ error in
|
||||
WCSession.default.sendMessageData(data, replyHandler: nil) { error in
|
||||
print("Cannot send message: \(String(describing: error))")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user