GlucoseValue.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // GlucoseValue.swift
  3. // LoopKit
  4. //
  5. // Created by Nathan Racklyeft on 3/2/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import HealthKit
  9. public protocol GlucoseValue: SampleValue {
  10. }
  11. public struct SimpleGlucoseValue: Equatable, GlucoseValue {
  12. public let startDate: Date
  13. public let endDate: Date
  14. public let quantity: HKQuantity
  15. public init(startDate: Date, endDate: Date? = nil, quantity: HKQuantity) {
  16. self.startDate = startDate
  17. self.endDate = endDate ?? startDate
  18. self.quantity = quantity
  19. }
  20. public init(_ glucoseValue: GlucoseValue) {
  21. self.startDate = glucoseValue.startDate
  22. self.endDate = glucoseValue.endDate
  23. self.quantity = glucoseValue.quantity
  24. }
  25. }
  26. extension SimpleGlucoseValue: Codable {
  27. public init(from decoder: Decoder) throws {
  28. let container = try decoder.container(keyedBy: CodingKeys.self)
  29. self.startDate = try container.decode(Date.self, forKey: .startDate)
  30. self.endDate = try container.decode(Date.self, forKey: .endDate)
  31. self.quantity = HKQuantity(unit: HKUnit(from: try container.decode(String.self, forKey: .quantityUnit)),
  32. doubleValue: try container.decode(Double.self, forKey: .quantity))
  33. }
  34. public func encode(to encoder: Encoder) throws {
  35. var container = encoder.container(keyedBy: CodingKeys.self)
  36. try container.encode(startDate, forKey: .startDate)
  37. try container.encode(endDate, forKey: .endDate)
  38. try container.encode(quantity.doubleValue(for: .milligramsPerDeciliter), forKey: .quantity)
  39. try container.encode(HKUnit.milligramsPerDeciliter.unitString, forKey: .quantityUnit)
  40. }
  41. private enum CodingKeys: String, CodingKey {
  42. case startDate
  43. case endDate
  44. case quantity
  45. case quantityUnit
  46. }
  47. }
  48. public struct PredictedGlucoseValue: Equatable, GlucoseValue {
  49. public let startDate: Date
  50. public let quantity: HKQuantity
  51. public init(startDate: Date, quantity: HKQuantity) {
  52. self.startDate = startDate
  53. self.quantity = quantity
  54. }
  55. }
  56. extension PredictedGlucoseValue: Codable {
  57. public init(from decoder: Decoder) throws {
  58. let container = try decoder.container(keyedBy: CodingKeys.self)
  59. self.startDate = try container.decode(Date.self, forKey: .startDate)
  60. self.quantity = HKQuantity(unit: HKUnit(from: try container.decode(String.self, forKey: .quantityUnit)),
  61. doubleValue: try container.decode(Double.self, forKey: .quantity))
  62. }
  63. public func encode(to encoder: Encoder) throws {
  64. var container = encoder.container(keyedBy: CodingKeys.self)
  65. try container.encode(startDate, forKey: .startDate)
  66. try container.encode(quantity.doubleValue(for: .milligramsPerDeciliter), forKey: .quantity)
  67. try container.encode(HKUnit.milligramsPerDeciliter.unitString, forKey: .quantityUnit)
  68. }
  69. private enum CodingKeys: String, CodingKey {
  70. case startDate
  71. case quantity
  72. case quantityUnit
  73. }
  74. }
  75. extension HKQuantitySample: GlucoseValue { }