CorrectionRange.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // CorrectionRange.swift
  3. // NightscoutUploadKit
  4. //
  5. // Created by Pete Schwamb on 5/28/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public struct CorrectionRange {
  11. typealias RawValue = [String: Any]
  12. let minValue: Double
  13. let maxValue: Double
  14. public init(minValue: HKQuantity, maxValue: HKQuantity) {
  15. // BG values in nightscout are in mg/dL.
  16. let unit = HKUnit.milligramsPerDeciliter
  17. self.minValue = minValue.doubleValue(for: unit)
  18. self.maxValue = maxValue.doubleValue(for: unit)
  19. }
  20. public var dictionaryRepresentation: [String: Any] {
  21. var rval = [String: Any]()
  22. rval["minValue"] = minValue
  23. rval["maxValue"] = maxValue
  24. return rval
  25. }
  26. init?(rawValue: RawValue) {
  27. guard
  28. let minValue = rawValue["minValue"] as? Double,
  29. let maxValue = rawValue["maxValue"] as? Double
  30. else {
  31. return nil
  32. }
  33. self.minValue = minValue
  34. self.maxValue = maxValue
  35. }
  36. }