Skip to content

Instantly share code, notes, and snippets.

@hungtruong
Last active May 2, 2019 03:59
Show Gist options
  • Save hungtruong/a45a91a9d598a448db7ce3947d70304d to your computer and use it in GitHub Desktop.
Save hungtruong/a45a91a9d598a448db7ce3947d70304d to your computer and use it in GitHub Desktop.
if let calories = message["calories"] as? Int,
let distance = message["distance"] as? Int,
let start = message["start"] as? Date,
let end = message["end"] as? Date
{
let calorieQuantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
let calorieQuantity = HKQuantity(unit: .kilocalorie(), doubleValue: Double(calories))
let caloriesBurned = HKQuantitySample(type: calorieQuantityType, quantity: calorieQuantity, start: start, end: end)
let distanceQuantityType = HKQuantityType.quantityType(forIdentifier: .distanceCycling)!
let distanceQuantity = HKQuantity(unit: .meter(), doubleValue: Double(distance))
let distanceCycled = HKQuantitySample(type: distanceQuantityType, quantity: distanceQuantity, start: start, end: end)
self.workoutBuilder.add([caloriesBurned, distanceCycled]) { (_, _) in
self.endWorkout(date: end)
}
}
if let segment = message["segmentName"] as? String,
let dateInterval = message["dateInterval"] as? DateInterval {
let workoutEvent = HKWorkoutEvent(type: .segment, dateInterval: dateInterval,
metadata: ["Segment Name" : segment])
self.workoutBuilder.addWorkoutEvents([workoutEvent]) { (success, error) in
print(success ? "Success saving segment" : error as Any)
}
}
if let workoutName = message["workoutName"] as? String {
let metadata = [HKMetadataKeyWorkoutBrandName: workoutName]
self.workoutBuilder.addMetadata(metadata) { (success, error) in
print(success ? "Success saving metadata" : error as Any)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment