Created
September 14, 2018 17:55
-
-
Save nderkach/c87f71f6d49c7e5683133b2a4a850d2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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