ShareGlucose+GlucoseKit.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // ShareGlucose+GlucoseKit.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 5/8/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. import LoopKit
  11. enum GlucoseLimits {
  12. static var minimum: UInt16 = 40
  13. static var maximum: UInt16 = 400
  14. }
  15. extension ShareGlucose: GlucoseValue {
  16. public var startDate: Date {
  17. return timestamp
  18. }
  19. public var quantity: HKQuantity {
  20. return HKQuantity(unit: .milligramsPerDeciliter, doubleValue: Double(min(max(glucose, GlucoseLimits.minimum), GlucoseLimits.maximum)))
  21. }
  22. }
  23. extension ShareGlucose: GlucoseDisplayable {
  24. public var isStateValid: Bool {
  25. return glucose >= 39
  26. }
  27. public var trendType: GlucoseTrend? {
  28. return GlucoseTrend(rawValue: Int(trend))
  29. }
  30. public var trendRate: HKQuantity? {
  31. return nil
  32. }
  33. public var isLocal: Bool {
  34. return false
  35. }
  36. // TODO Placeholder. This functionality will come with LOOP-1311
  37. public var glucoseRangeCategory: GlucoseRangeCategory? {
  38. return nil
  39. }
  40. }
  41. extension ShareGlucose {
  42. public var condition: GlucoseCondition? {
  43. if glucose < GlucoseLimits.minimum {
  44. return .belowRange
  45. } else if glucose > GlucoseLimits.maximum {
  46. return .aboveRange
  47. } else {
  48. return nil
  49. }
  50. }
  51. }
  52. extension GlucoseDisplayable {
  53. public var stateDescription: String {
  54. if isStateValid {
  55. return LocalizedString("OK", comment: "Sensor state description for the valid state")
  56. } else {
  57. return LocalizedString("Needs Attention", comment: "Sensor state description for the non-valid state")
  58. }
  59. }
  60. }