HKUnit.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // HKUnit.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 1/17/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import HealthKit
  9. extension HKUnit {
  10. static let milligramsPerDeciliter: HKUnit = {
  11. return HKUnit.gramUnit(with: .milli).unitDivided(by: .literUnit(with: .deci))
  12. }()
  13. static let millimolesPerLiter: HKUnit = {
  14. return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: .liter())
  15. }()
  16. static let internationalUnitsPerHour: HKUnit = {
  17. return HKUnit.internationalUnit().unitDivided(by: .hour())
  18. }()
  19. static let gramsPerUnit: HKUnit = {
  20. return HKUnit.gram().unitDivided(by: .internationalUnit())
  21. }()
  22. var foundationUnit: Unit? {
  23. if self == HKUnit.milligramsPerDeciliter {
  24. return UnitConcentrationMass.milligramsPerDeciliter
  25. }
  26. if self == HKUnit.millimolesPerLiter {
  27. return UnitConcentrationMass.millimolesPerLiter(withGramsPerMole: HKUnitMolarMassBloodGlucose)
  28. }
  29. if self == HKUnit.gram() {
  30. return UnitMass.grams
  31. }
  32. return nil
  33. }
  34. /// The smallest value expected to be visible on a chart
  35. var chartableIncrement: Double {
  36. if self == .milligramsPerDeciliter {
  37. return 1
  38. } else {
  39. return 1 / 25
  40. }
  41. }
  42. }