Compare commits
2 Commits
watch_fix_
...
watch_fix_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfb373c8af | ||
|
|
cedab87193 |
@@ -26,18 +26,17 @@ class HealthKitHelper {
|
|||||||
maxHeartRate: nil,
|
maxHeartRate: nil,
|
||||||
avgHeartRate: 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.completion = completion
|
||||||
self.returnCount = 0
|
self.returnCount = 0
|
||||||
|
|
||||||
print("get details \(uuid.uuidString)")
|
print("get details \(uuid.uuidString)")
|
||||||
|
|
||||||
let predicate = HKQuery.predicateForObject(with: uuid)
|
|
||||||
let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(),
|
let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(),
|
||||||
predicate: predicate,
|
predicate: HKQuery.predicateForObject(with: uuid),
|
||||||
limit: 0,
|
limit: HKObjectQueryNoLimit,
|
||||||
sortDescriptors: nil)
|
sortDescriptors: nil)
|
||||||
{ (sampleQuery, results, error ) -> Void in
|
{ (sampleQuery, results, error ) -> Void in
|
||||||
|
|
||||||
@@ -45,6 +44,7 @@ class HealthKitHelper {
|
|||||||
self.shitReturned()
|
self.shitReturned()
|
||||||
self.shitReturned()
|
self.shitReturned()
|
||||||
print( "There was an error while reading the samples: \(queryError.localizedDescription)")
|
print( "There was an error while reading the samples: \(queryError.localizedDescription)")
|
||||||
|
self.completion?(nil)
|
||||||
} else {
|
} else {
|
||||||
for samples: HKSample in results! {
|
for samples: HKSample in results! {
|
||||||
let workout: HKWorkout = (samples as! HKWorkout)
|
let workout: HKWorkout = (samples as! HKWorkout)
|
||||||
@@ -54,6 +54,7 @@ class HealthKitHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
healthStore.execute(query)
|
healthStore.execute(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -148,10 +148,11 @@ struct AllWorkoutsView: View {
|
|||||||
HKObjectType.quantityType(forIdentifier: .heartRate)!,
|
HKObjectType.quantityType(forIdentifier: .heartRate)!,
|
||||||
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
|
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
|
||||||
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
|
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
|
||||||
|
HKObjectType.activitySummaryType(),
|
||||||
HKQuantityType.workoutType()
|
HKQuantityType.workoutType()
|
||||||
]
|
]
|
||||||
|
|
||||||
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in
|
healthStore.requestAuthorization(toShare: nil, read: healthKitTypes) { (succ, error) in
|
||||||
if !succ {
|
if !succ {
|
||||||
fatalError("Error requesting authorization from health store: \(String(describing: error)))")
|
fatalError("Error requesting authorization from health store: \(String(describing: error)))")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,6 +81,9 @@ struct CompletedWorkoutView: View {
|
|||||||
gettingHealthKitData = true
|
gettingHealthKitData = true
|
||||||
healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID,
|
healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID,
|
||||||
completion: { healthKitWorkoutData in
|
completion: { healthKitWorkoutData in
|
||||||
|
guard let healthStore = healthKitWorkoutData else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.healthKitWorkoutData = healthKitWorkoutData
|
self.healthKitWorkoutData = healthKitWorkoutData
|
||||||
gettingHealthKitData = false
|
gettingHealthKitData = false
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -30,9 +30,10 @@ class WatchDelegate: NSObject, WKApplicationDelegate {
|
|||||||
HKObjectType.quantityType(forIdentifier: .heartRate)!,
|
HKObjectType.quantityType(forIdentifier: .heartRate)!,
|
||||||
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
|
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
|
||||||
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
|
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
|
||||||
|
HKObjectType.activitySummaryType(),
|
||||||
HKQuantityType.workoutType()
|
HKQuantityType.workoutType()
|
||||||
]
|
]
|
||||||
HKHealthStore().requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in
|
HKHealthStore().requestAuthorization(toShare: nil, read: healthKitTypes) { (succ, error) in
|
||||||
if !succ {
|
if !succ {
|
||||||
fatalError("Error requesting authorization from health store: \(String(describing: error)))")
|
fatalError("Error requesting authorization from health store: \(String(describing: error)))")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,14 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
|
|||||||
|
|
||||||
func startWorkout() {
|
func startWorkout() {
|
||||||
if isInWorkout { return }
|
if isInWorkout { return }
|
||||||
|
isInWorkout = true
|
||||||
|
setupCore()
|
||||||
hkWorkoutSession.startActivity(with: Date())
|
hkWorkoutSession.startActivity(with: Date())
|
||||||
//WKInterfaceDevice.current().play(.start)
|
//WKInterfaceDevice.current().play(.start)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopWorkout(sendDetails: Bool) {
|
func stopWorkout(sendDetails: Bool) {
|
||||||
|
// hkWorkoutSession.endCurrentActivity(on: Date())
|
||||||
hkWorkoutSession.end()
|
hkWorkoutSession.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,10 +128,13 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
|
|||||||
}
|
}
|
||||||
|
|
||||||
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
|
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
|
||||||
workoutSession.end()
|
print("[didFailWithError] Workout Builder changed event: \(error.localizedDescription)")
|
||||||
setupCore()
|
// trying to go from ended to something so just end it all
|
||||||
startWorkout()
|
if workoutSession.state == .ended {
|
||||||
beginBuilderWorkout()
|
getWorkoutBuilderDetails(completion: {
|
||||||
|
self.setupCore()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
|
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
|
||||||
|
|||||||
Reference in New Issue
Block a user