forked from tidepool-org/LoopAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarbValue.swift
More file actions
48 lines (39 loc) · 1.33 KB
/
CarbValue.swift
File metadata and controls
48 lines (39 loc) · 1.33 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
//
// CarbValue.swift
//
// Copyright © 2017 LoopKit Authors. All rights reserved.
//
import Foundation
public struct CarbValue: SampleValue {
public let startDate: Date
public let endDate: Date
public var value: Double
public var quantity: LoopQuantity {
return LoopQuantity(unit: .gram, doubleValue: value)
}
public init(startDate: Date, endDate: Date? = nil, value: Double) {
self.startDate = startDate
self.endDate = endDate ?? startDate
self.value = value
}
}
extension CarbValue: Equatable {}
extension CarbValue: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.startDate = try container.decode(Date.self, forKey: .startDate)
self.endDate = try container.decode(Date.self, forKey: .endDate)
self.value = try container.decode(Double.self, forKey: .value)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(startDate, forKey: .startDate)
try container.encode(endDate, forKey: .endDate)
try container.encode(value, forKey: .value)
}
private enum CodingKeys: String, CodingKey {
case startDate
case endDate
case value
}
}