|
2 | 2 | // HeartbeatSeries.swift
|
3 | 3 | // HealthKitReporter
|
4 | 4 | //
|
5 |
| -// Created by Victor on 25.09.20. |
| 5 | +// Created by Kachalov, Victor on 12.10.21. |
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
| 9 | +import HealthKit |
9 | 10 |
|
10 |
| -public struct HeartbeatSeries: Codable { |
11 |
| - public let timeSinceSeriesStart: Double |
12 |
| - public let precededByGap: Bool |
13 |
| - public let done: Bool |
| 11 | +@available(iOS 13.0, *) |
| 12 | +public struct HeartbeatSeries: Identifiable, Sample { |
| 13 | + public struct Measurement: Codable { |
| 14 | + public let timeSinceSeriesStart: Double |
| 15 | + public let precededByGap: Bool |
| 16 | + public let done: Bool |
| 17 | + public init( |
| 18 | + timeSinceSeriesStart: Double, |
| 19 | + precededByGap: Bool, |
| 20 | + done: Bool |
| 21 | + ) { |
| 22 | + self.timeSinceSeriesStart = timeSinceSeriesStart |
| 23 | + self.precededByGap = precededByGap |
| 24 | + self.done = done |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public struct Harmonized: Codable { |
| 29 | + public let count: Int |
| 30 | + public let measurements: [Measurement] |
| 31 | + public let metadata: [String: String]? |
| 32 | + |
| 33 | + public init( |
| 34 | + count: Int, |
| 35 | + measurements: [Measurement], |
| 36 | + metadata: [String: String]? |
| 37 | + ) { |
| 38 | + self.count = count |
| 39 | + self.measurements = measurements |
| 40 | + self.metadata = metadata |
| 41 | + } |
14 | 42 |
|
| 43 | + public func copyWith( |
| 44 | + count: Int? = nil, |
| 45 | + measurements: [Measurement]? = nil, |
| 46 | + metadata: [String: String]? = nil |
| 47 | + ) -> Harmonized { |
| 48 | + return Harmonized( |
| 49 | + count: count ?? self.count, |
| 50 | + measurements: measurements ?? self.measurements, |
| 51 | + metadata: metadata ?? self.metadata |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public let uuid: String |
| 57 | + public let identifier: String |
| 58 | + public let startTimestamp: Double |
| 59 | + public let endTimestamp: Double |
| 60 | + public let device: Device? |
| 61 | + public let sourceRevision: SourceRevision |
| 62 | + public let harmonized: Harmonized |
| 63 | + |
15 | 64 | public init(
|
16 |
| - timeSinceSeriesStart: Double, |
17 |
| - precededByGap: Bool, |
18 |
| - done: Bool |
| 65 | + identifier: String, |
| 66 | + startTimestamp: Double, |
| 67 | + endTimestamp: Double, |
| 68 | + device: Device?, |
| 69 | + sourceRevision: SourceRevision, |
| 70 | + harmonized: Harmonized |
19 | 71 | ) {
|
20 |
| - self.timeSinceSeriesStart = timeSinceSeriesStart |
21 |
| - self.precededByGap = precededByGap |
22 |
| - self.done = done |
| 72 | + self.uuid = UUID().uuidString |
| 73 | + self.identifier = identifier |
| 74 | + self.startTimestamp = startTimestamp |
| 75 | + self.endTimestamp = endTimestamp |
| 76 | + self.device = device |
| 77 | + self.sourceRevision = sourceRevision |
| 78 | + self.harmonized = harmonized |
| 79 | + } |
| 80 | + init(sample: HKHeartbeatSeriesSample, measurements: [Measurement]) { |
| 81 | + self.uuid = sample.uuid.uuidString |
| 82 | + self.identifier = sample.sampleType.identifier |
| 83 | + self.startTimestamp = sample.startDate.timeIntervalSince1970 |
| 84 | + self.endTimestamp = sample.endDate.timeIntervalSince1970 |
| 85 | + self.device = Device(device: sample.device) |
| 86 | + self.sourceRevision = SourceRevision(sourceRevision: sample.sourceRevision) |
| 87 | + self.harmonized = sample.harmonize(measurements: measurements) |
23 | 88 | }
|
24 | 89 | }
|
0 commit comments