Created
June 1, 2015 13:36
-
-
Save danielctull/3d6fc0ac12015c6508d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import HealthKit | |
| public extension HKWorkout { | |
| public func predicateForSamplesDuringActivePeriods() -> NSPredicate { | |
| let subpredicates = activePeriods.map { start, end -> NSPredicate in | |
| HKQuery.predicateForSamplesWithStartDate(start, endDate: end, options: .StrictStartDate | .StrictEndDate) | |
| } | |
| return NSCompoundPredicate(type: .OrPredicateType, subpredicates: subpredicates) | |
| } | |
| public var activePeriods: [(start: NSDate, end: NSDate)] { | |
| get { | |
| var activePeriods: [(start: NSDate, end: NSDate)] = [] | |
| var start: NSDate? = self.startDate | |
| func addActivePeriod(start: NSDate?, end: NSDate) { | |
| if let start = start { | |
| activePeriods.append((start: start, end: end)) | |
| } | |
| } | |
| if let events = self.workoutEvents as? [HKWorkoutEvent] { | |
| for event in events { | |
| switch event.type { | |
| case .Pause: | |
| addActivePeriod(start, event.date) | |
| start = nil | |
| case .Resume: | |
| start = event.date | |
| } | |
| } | |
| } | |
| addActivePeriod(start, self.endDate) | |
| return activePeriods | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment