This commit is contained in:
Trey t
2023-08-16 00:16:38 -05:00
parent c0fdca9996
commit b501f66bf3
10 changed files with 234 additions and 81 deletions

View File

@@ -39,8 +39,9 @@ class WatchMainViewModel: NSObject, ObservableObject {
func autorizeHealthKit() {
let healthKitTypes: Set = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
HKQuantityType.workoutType()
]
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in
@@ -49,22 +50,7 @@ class WatchMainViewModel: NSObject, ObservableObject {
}
}
}
private func startHeartRateQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier) {
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = {
query, samples, deletedObjects, queryAnchor, error in
guard let samples = samples as? [HKQuantitySample] else {
return
}
}
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!, predicate: devicePredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)
query.updateHandler = updateHandler
healthStore.execute(query)
}
func nextExercise() {
let nextExerciseAction = WatchActions.nextExercise
let data = try! JSONEncoder().encode(nextExerciseAction)
@@ -153,8 +139,13 @@ extension WatchMainViewModel {
DispatchQueue.main.async() {
self.hkWorkoutSession = nil
self.hkBuilder = nil
let totalEnergy = workout?.totalEnergyBurned?.doubleValue(for: .kilocalorie()) ?? -1
let watchFinishWorkoutModel = WatchFinishWorkoutModel(totalBurnedEnergery: totalEnergy, allHeartRates: self.heartRates)
self.heartRates.removeAll()
self.isInWorkout = false
guard let id = workout?.uuid else {
return
}
let watchFinishWorkoutModel = WatchFinishWorkoutModel(healthKitUUID: id)
let data = try! JSONEncoder().encode(watchFinishWorkoutModel)
let watchAction = WatchActions.workoutComplete(data)
let watchActionData = try! JSONEncoder().encode(watchAction)
@@ -162,9 +153,6 @@ extension WatchMainViewModel {
if sendDetails {
self.send(watchActionData)
}
self.heartRates.removeAll()
self.isInWorkout = false
}
}
}