This commit is contained in:
Trey t
2024-07-01 22:34:27 -05:00
parent f1781744ca
commit 5f7c0a40b8
3 changed files with 68 additions and 25 deletions

View File

@@ -63,6 +63,7 @@ extension BridgeModule {
completedWorkout()
self.completedWorkout = nil
}
resetCurrentWorkout()
}
func start(workout: Workout) {

View File

@@ -177,7 +177,6 @@ struct WorkoutDetailView: View {
if let workoutData = createWorkoutData() {
workoutComplete = .completedWorkout(workoutData)
}
bridgeModule.resetCurrentWorkout()
}
}
.onReceive(NotificationCenter.default.publisher(

View File

@@ -10,17 +10,21 @@ import HealthKit
class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDelegate {
static let shared = WatchWorkout()
@Published var heartValue: Int?
let healthStore = HKHealthStore()
var hkWorkoutSession: HKWorkoutSession
var hkBuilder: HKLiveWorkoutBuilder
var hkWorkoutSession: HKWorkoutSession!
var hkBuilder: HKLiveWorkoutBuilder!
var heartRates = [Int]()
@Published var heartValue: Int?
@Published var isInWorkout = false
@Published var isPaused = false
private override init() {
private override init() {
super.init()
setupCore()
}
func setupCore() {
do {
let configuration = HKWorkoutConfiguration()
configuration.activityType = .functionalStrengthTraining
@@ -28,7 +32,6 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
hkWorkoutSession = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
hkBuilder = hkWorkoutSession.associatedWorkoutBuilder()
super.init()
hkWorkoutSession.delegate = self
hkBuilder.delegate = self
@@ -42,28 +45,47 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
func startWorkout() {
if isInWorkout { return }
// Start the workout session and begin data collection
hkWorkoutSession.startActivity(with: Date())
hkBuilder.beginCollection(withStart: Date()) { (succ, error) in
if !succ {
fatalError("Error beginning collection from builder: \(String(describing: error)))")
}
}
isInWorkout = true
//WKInterfaceDevice.current().play(.start)
}
func stopWorkout(sendDetails: Bool) {
hkWorkoutSession.end()
self.heartRates.removeAll()
self.isInWorkout = false
}
func togglePaused() {
self.isPaused.toggle()
}
func beginDataCollection() {
hkBuilder.beginCollection(withStart: Date()) { (succ, error) in
if !succ {
fatalError("Error beginning collection from builder: \(String(describing: error)))")
}
}
DispatchQueue.main.async {
self.isInWorkout = true
}
}
func getWorkoutBuilderDetails(completion: @escaping (() -> Void)) {
DispatchQueue.main.async {
self.heartRates.removeAll()
self.isInWorkout = false
}
hkBuilder.endCollection(withEnd: Date()) { (success, error) in
if !success || error != nil { return }
if !success || error != nil {
completion()
return
}
self.hkBuilder.finishWorkout { (workout, error) in
guard let workout = workout else { return }
if !sendDetails { return }
guard let workout = workout else {
completion()
return
}
// if !sendDetails { return }
DispatchQueue.main.async() {
let watchFinishWorkoutModel = WatchFinishWorkoutModel(healthKitUUID: workout.uuid)
@@ -71,21 +93,42 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
let watchAction = WatchActions.workoutComplete(data)
let watchActionData = try! JSONEncoder().encode(watchAction)
DataSender.send(watchActionData)
completion()
}
}
}
}
func togglePaused() {
self.isPaused.toggle()
}
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
switch toState {
case .notStarted:
print("not started")
case .running:
print("running")
startWorkout()
beginDataCollection()
case .ended:
print("ended")
getWorkoutBuilderDetails(completion: {
self.setupCore()
})
case .paused:
print("paused")
case .prepared:
print("prepared")
case .stopped:
print("stopped")
@unknown default:
fatalError()
}
print("[workoutSession] Changed State: \(toState.rawValue)")
}
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
print("[workoutSession] Encountered an error: \(error)")
workoutSession.end()
setupCore()
startWorkout()
beginBuilderWorkout()
}
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {