GlucoseSchedule.swift 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // GlucoseSchedule.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 2/12/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public typealias GlucoseSchedule = SingleQuantitySchedule
  11. public typealias InsulinSensitivitySchedule = GlucoseSchedule
  12. public extension InsulinSensitivitySchedule {
  13. private func convertTo(unit: HKUnit) -> InsulinSensitivitySchedule? {
  14. guard unit != self.unit else {
  15. return self
  16. }
  17. let convertedDailyItems: [RepeatingScheduleValue<Double>] = self.items.map {
  18. RepeatingScheduleValue(startTime: $0.startTime,
  19. value: HKQuantity(unit: self.unit, doubleValue: $0.value).doubleValue(for: unit)
  20. )
  21. }
  22. return InsulinSensitivitySchedule(unit: unit,
  23. dailyItems: convertedDailyItems,
  24. timeZone: timeZone)
  25. }
  26. func schedule(for glucoseUnit: HKUnit) -> InsulinSensitivitySchedule? {
  27. // InsulinSensitivitySchedule stores only the glucose unit.
  28. precondition(glucoseUnit == .millimolesPerLiter || glucoseUnit == .milligramsPerDeciliter)
  29. return self.convertTo(unit: glucoseUnit)
  30. }
  31. }