Skip to content

Instantly share code, notes, and snippets.

@nderkach
Created September 14, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nderkach/c87f71f6d49c7e5683133b2a4a850d2a to your computer and use it in GitHub Desktop.
Save nderkach/c87f71f6d49c7e5683133b2a4a850d2a to your computer and use it in GitHub Desktop.
/// Initiates HK queries for new data based on the given type
///
/// - parameter type: `HKObjectType` which has new data avilable.
private func handleSample(_ sample: HKSample) {
switch sample.sampleType {
case HKObjectType.categoryType(forIdentifier: .sleepAnalysis):
guard let totalTimeAsleep = sample.metadata?["Asleep"] as? Double else {
return
}
let totalTimeAsleepInHours = totalTimeAsleep / 60.0 / 60.0
print(String(format: "Slept %f hrs today", totalTimeAsleepInHours))
self.sendMessageToTelegram(withText: String(format: "Slept %f hrs today", totalTimeAsleepInHours))
case HKObjectType.categoryType(forIdentifier: .mindfulSession):
let durationInSeconds = sample.endDate.timeIntervalSince(sample.startDate)
if durationInSeconds == meditatedSeconds {
return
}
meditatedSeconds = durationInSeconds
let durationInMinutes = durationInSeconds / 60.0
print(String(format: "Meditated %f minutes", durationInMinutes))
self.sendMessageToTelegram(withText: String(format: "Meditated %f minutes", durationInMinutes))
default:
debugPrint("Unhandled HKObjectType: \(sample.sampleType)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment