-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHKHeartbeatSeriesSample+Detailable.swift
67 lines (53 loc) · 2.51 KB
/
HKHeartbeatSeriesSample+Detailable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
An extension that adds detail string support to a heartbeat series sample.
*/
import Foundation
import HealthKit
extension HKHeartbeatSeriesSample: Detailable {
var summaryString: String {
return "Beat-to-Beat Reading"
}
// For the overview, we'll display the start date of the sample as well as the number of heartbeats
var overviewStrings: [String] {
var overviewItems: [String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
dateFormatter.locale = Locale(identifier: "en_US")
overviewItems.append("Start: \(dateFormatter.string(from: self.startDate))")
overviewItems.append("Number of heartbeats: \(self.count)")
return overviewItems
}
// The detail strings for the HKHeartbeatSeriesSample are the individual time stamps of heart beats
// since the start of data collection. You can use an HKHeartbeatSeriesQuery to get these individual time stamps.
func getDetailStrings(_ healthStore: HKHealthStore, with completion: @escaping ([String]) -> Void) {
var details: [String] = []
let query = HKHeartbeatSeriesQuery(heartbeatSeries: self) {
(query, timeSinceSeriesStart, precededByGap, done, error) in
if error != nil {
fatalError("Error during query: \(String(describing: error))")
}
details.append("\(round(1000 * timeSinceSeriesStart) / 1000) s")
if done == true {
completion(details)
}
}
healthStore.execute(query)
}
}
extension HKHealthStore {
/// Asynchronously fetches the most recent quantity sample of a specified type.
func mostRecentQauntitySampleOfType(_ quantityType: HKQuantityType, predicate: NSPredicate? = nil, completion: @escaping (HKQuantity?, NSError?) -> Void) {
let timeSortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: quantityType, predicate: predicate, limit: 1, sortDescriptors: [timeSortDescriptor]) { _, samples, error in
if let firstSample = samples?.first as? HKQuantitySample {
completion(firstSample.quantity, nil)
} else {
completion(nil, error as NSError?)
}
}
execute(query)
}
}