-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathWatchPredictedGlucose.swift
More file actions
53 lines (43 loc) · 1.4 KB
/
WatchPredictedGlucose.swift
File metadata and controls
53 lines (43 loc) · 1.4 KB
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
//
// WatchPredictedGlucose.swift
// WatchApp Extension
//
// Created by Bharat Mediratta on 6/26/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//
import Foundation
import LoopKit
import HealthKit
import LoopAlgorithm
struct WatchPredictedGlucose: Equatable {
let values: [PredictedGlucoseValue]
init?(values: [PredictedGlucoseValue]) {
guard values.count > 1 else {
return nil
}
self.values = values
}
}
extension WatchPredictedGlucose: RawRepresentable {
typealias RawValue = [String: Any]
var rawValue: RawValue {
return [
"v": values.map { Int16($0.quantity.doubleValue(for: .milligramsPerDeciliter).clamped(to: Double(Int16.min)...Double(Int16.max))) },
"d": values[0].startDate,
"i": values[1].startDate.timeIntervalSince(values[0].startDate)
]
}
init?(rawValue: RawValue) {
guard
let values = rawValue["v"] as? [Int16],
let firstDate = rawValue["d"] as? Date,
let interval = rawValue["i"] as? TimeInterval
else {
return nil
}
self.values = values.enumerated().map { tuple in
PredictedGlucoseValue(startDate: firstDate + Double(tuple.0) * interval,
quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: Double(tuple.1)))
}
}
}