From bfb373c8afd4243ef723c1dd4963123cb64ac53c Mon Sep 17 00:00:00 2001 From: Trey t Date: Tue, 2 Jul 2024 11:46:09 -0500 Subject: [PATCH] WIP --- iphone/Werkout_ios/HealthKitHelper.swift | 11 ++++++----- .../Views/AllWorkouts/AllWorkoutsView.swift | 3 ++- .../Views/CompletedWorkout/CompletedWorkoutView.swift | 3 +++ iphone/Werkout_watch Watch App/WatchDelegate.swift | 3 ++- iphone/Werkout_watch Watch App/WatchWorkout.swift | 4 ++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/iphone/Werkout_ios/HealthKitHelper.swift b/iphone/Werkout_ios/HealthKitHelper.swift index 7c07d08..e06e08c 100644 --- a/iphone/Werkout_ios/HealthKitHelper.swift +++ b/iphone/Werkout_ios/HealthKitHelper.swift @@ -26,18 +26,17 @@ class HealthKitHelper { maxHeartRate: nil, avgHeartRate: nil) - var completion: ((HealthKitWorkoutData) -> Void)? + var completion: ((HealthKitWorkoutData?) -> Void)? - func getDetails(forHealthKitUUID uuid: UUID, completion: @escaping ((HealthKitWorkoutData) -> Void)) { + func getDetails(forHealthKitUUID uuid: UUID, completion: @escaping ((HealthKitWorkoutData?) -> Void)) { self.completion = completion self.returnCount = 0 print("get details \(uuid.uuidString)") - let predicate = HKQuery.predicateForObject(with: uuid) let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), - predicate: predicate, - limit: 0, + predicate: HKQuery.predicateForObject(with: uuid), + limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (sampleQuery, results, error ) -> Void in @@ -45,6 +44,7 @@ class HealthKitHelper { self.shitReturned() self.shitReturned() print( "There was an error while reading the samples: \(queryError.localizedDescription)") + self.completion?(nil) } else { for samples: HKSample in results! { let workout: HKWorkout = (samples as! HKWorkout) @@ -54,6 +54,7 @@ class HealthKitHelper { } } } + healthStore.execute(query) } diff --git a/iphone/Werkout_ios/Views/AllWorkouts/AllWorkoutsView.swift b/iphone/Werkout_ios/Views/AllWorkouts/AllWorkoutsView.swift index e75ae25..7f96b39 100644 --- a/iphone/Werkout_ios/Views/AllWorkouts/AllWorkoutsView.swift +++ b/iphone/Werkout_ios/Views/AllWorkouts/AllWorkoutsView.swift @@ -148,10 +148,11 @@ struct AllWorkoutsView: View { HKObjectType.quantityType(forIdentifier: .heartRate)!, HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!, HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!, + HKObjectType.activitySummaryType(), HKQuantityType.workoutType() ] - healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in + healthStore.requestAuthorization(toShare: nil, read: healthKitTypes) { (succ, error) in if !succ { fatalError("Error requesting authorization from health store: \(String(describing: error)))") } diff --git a/iphone/Werkout_ios/Views/CompletedWorkout/CompletedWorkoutView.swift b/iphone/Werkout_ios/Views/CompletedWorkout/CompletedWorkoutView.swift index 0715921..c6ba9c7 100644 --- a/iphone/Werkout_ios/Views/CompletedWorkout/CompletedWorkoutView.swift +++ b/iphone/Werkout_ios/Views/CompletedWorkout/CompletedWorkoutView.swift @@ -81,6 +81,9 @@ struct CompletedWorkoutView: View { gettingHealthKitData = true healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID, completion: { healthKitWorkoutData in + guard let healthStore = healthKitWorkoutData else { + return + } self.healthKitWorkoutData = healthKitWorkoutData gettingHealthKitData = false }) diff --git a/iphone/Werkout_watch Watch App/WatchDelegate.swift b/iphone/Werkout_watch Watch App/WatchDelegate.swift index 57d79dd..8553b51 100644 --- a/iphone/Werkout_watch Watch App/WatchDelegate.swift +++ b/iphone/Werkout_watch Watch App/WatchDelegate.swift @@ -30,9 +30,10 @@ class WatchDelegate: NSObject, WKApplicationDelegate { HKObjectType.quantityType(forIdentifier: .heartRate)!, HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!, HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!, + HKObjectType.activitySummaryType(), HKQuantityType.workoutType() ] - HKHealthStore().requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in + HKHealthStore().requestAuthorization(toShare: nil, read: healthKitTypes) { (succ, error) in if !succ { fatalError("Error requesting authorization from health store: \(String(describing: error)))") } diff --git a/iphone/Werkout_watch Watch App/WatchWorkout.swift b/iphone/Werkout_watch Watch App/WatchWorkout.swift index be0b59f..9eddb70 100644 --- a/iphone/Werkout_watch Watch App/WatchWorkout.swift +++ b/iphone/Werkout_watch Watch App/WatchWorkout.swift @@ -45,14 +45,14 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive func startWorkout() { if isInWorkout { return } + isInWorkout = true setupCore() hkWorkoutSession.startActivity(with: Date()) - isInWorkout = true //WKInterfaceDevice.current().play(.start) } func stopWorkout(sendDetails: Bool) { - hkWorkoutSession.endCurrentActivity(on: Date()) +// hkWorkoutSession.endCurrentActivity(on: Date()) hkWorkoutSession.end() }