This commit is contained in:
Trey t
2023-07-17 19:26:11 -05:00
parent 593cc496cd
commit af538362e8
23 changed files with 637 additions and 438 deletions

View File

@@ -36,17 +36,14 @@ class BridgeModule: NSObject, ObservableObject {
@Published var currentWorkoutRunTimeInSeconds: Int = -1
private var currentWorkoutRunTimer: Timer?
var currentWorkout: Workout?
public private(set) var workoutStartDate: Date?
private var currentExerciseTimer: Timer?
public private(set) var currentExerciseIdx: Int = -1 {
didSet {
self.currentExercisePositionString = "\(self.currentExerciseIdx+1)/\(self.currentWorkout?.exercises.count ?? 0)"
}
}
@Published public private(set) var currentExerciseInfo = CurrentWorkoutInfo()
@Published var previewWorkout: Workout?
@Published var currentExerciseTimeLeft: Int = 0
@Published var currentExercise: ExerciseElement?
var currentExercisePositionString: String?
private var isWatchConnected = false
@@ -59,33 +56,33 @@ class BridgeModule: NSObject, ObservableObject {
var audioPlayer: AVAudioPlayer?
func start(workout: Workout) {
self.currentWorkout = workout
currentExerciseInfo.complete = {
self.completeWorkout()
}
currentExerciseInfo.start(workout: workout)
currentWorkoutRunTimeInSeconds = 0
currentWorkoutRunTimer?.invalidate()
currentWorkoutRunTimer = nil
currentExerciseIdx = 0
let exercise = workout.exercises[currentExerciseIdx]
updateCurrent(exercise: exercise)
startWorkoutTimer()
workoutStartDate = Date()
isInWorkout = true
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
if let superetExercise = currentExerciseInfo.currentExercise {
updateCurrent(exercise: superetExercise)
startWorkoutTimer()
workoutStartDate = Date()
isInWorkout = true
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
}
}
func goToExerciseAt(index: Int) {
guard let currentWorkout = currentWorkout else {
return
func goToExerciseAt(section: Int, row: Int) {
if let superetExercise = currentExerciseInfo.goToWorkoutAt(supersetIndex: section,
exerciseIndex: row) {
updateCurrent(exercise: superetExercise)
}
currentExerciseIdx = index
let exercise = currentWorkout.exercises[index]
updateCurrent(exercise: exercise)
}
func resetCurrentWorkout() {
@@ -98,11 +95,8 @@ class BridgeModule: NSObject, ObservableObject {
self.currentExerciseTimer = nil
self.currentWorkoutRunTimeInSeconds = -1
self.currentExerciseIdx = -1
self.currentExercise = nil
self.currentWorkout = nil
self.currentExerciseInfo.reset()
self.isInWorkout = false
self.workoutStartDate = nil
self.workoutEndDate = nil
@@ -162,38 +156,24 @@ class BridgeModule: NSObject, ObservableObject {
}
func nextExercise() {
currentExerciseIdx += 1
if let currentWorkout = currentWorkout {
if currentExerciseIdx < currentWorkout.exercises.count {
let nextExercise = currentWorkout.exercises[currentExerciseIdx]
updateCurrent(exercise: nextExercise)
} else {
completeWorkout()
}
if let nextSupersetExercise = currentExerciseInfo.nextExercise {
updateCurrent(exercise: nextSupersetExercise)
} else {
completeWorkout()
}
}
func previousExercise() {
currentExerciseIdx -= 1
if currentExerciseIdx < 0 {
currentExerciseIdx = 0
}
if let currentWorkout = currentWorkout {
if currentExerciseIdx < currentWorkout.exercises.count {
let nextExercise = currentWorkout.exercises[currentExerciseIdx]
updateCurrent(exercise: nextExercise)
} else {
completeWorkout()
}
if let nextSupersetExercise = currentExerciseInfo.previousExercise {
updateCurrent(exercise: nextSupersetExercise)
} else {
completeWorkout()
}
}
func restartExercise() {
if let currentWorkout = currentWorkout {
if currentExerciseIdx < currentWorkout.exercises.count {
let nextExercise = currentWorkout.exercises[currentExerciseIdx]
updateCurrent(exercise: nextExercise)
}
if let currentExercise = currentExerciseInfo.currentExercise {
updateCurrent(exercise: currentExercise)
}
}
@@ -201,15 +181,12 @@ class BridgeModule: NSObject, ObservableObject {
currentWorkoutRunTimeInSeconds += 1
}
func updateCurrent(exercise: ExerciseElement) {
func updateCurrent(exercise: SupersetExercise) {
DispatchQueue.main.async {
self.currentExerciseTimer?.invalidate()
self.currentExerciseTimer = nil
self.currentExercise = exercise
if let duration = exercise.duration,
duration > 0 {
if let duration = exercise.duration, duration > 0 {
self.startExerciseTimerWith(duration: duration)
}
self.sendCurrentExerciseToWatch()
@@ -281,25 +258,25 @@ extension BridgeModule: WCSessionDelegate {
}
func sendCurrentExerciseToWatch() {
if let duration = currentExercise?.duration,
if let currentExercise = currentExerciseInfo.currentExercise,
let duration = currentExercise.duration ,
duration > 0 {
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: currentExerciseTimeLeft, workoutStartDate: workoutStartDate ?? Date())
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,
if let currentExercise = currentExerciseInfo.currentExercise,
let reps = 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: currentExercise.exercise.name, currentTimeLeft: reps, workoutStartDate: self.workoutStartDate ?? Date())
let model = PhoneToWatchActions.inExercise(watchModel)
let data = try! JSONEncoder().encode(model)
self.send(data)
}
// 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)
}
}