Glucose+SensorDisplayable.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // GlucoseRxMessage.swift
  3. // Loop
  4. //
  5. // Created by Nathan Racklyeft on 5/30/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. import LoopKit
  10. extension Glucose: GlucoseDisplayable {
  11. public var isStateValid: Bool {
  12. return state == .known(.ok) && status == .ok
  13. }
  14. public var stateDescription: String {
  15. var messages = [String]()
  16. switch state {
  17. case .known(.ok):
  18. break // Suppress the "OK" message
  19. default:
  20. messages.append(state.localizedDescription)
  21. }
  22. switch self.status {
  23. case .ok:
  24. if messages.isEmpty {
  25. messages.append(status.localizedDescription)
  26. } else {
  27. break // Suppress the "OK" message
  28. }
  29. case .lowBattery, .unknown:
  30. messages.append(status.localizedDescription)
  31. }
  32. return messages.joined(separator: ". ")
  33. }
  34. public var trendType: GlucoseTrend? {
  35. guard trend < Int(Int8.max) else {
  36. return nil
  37. }
  38. switch trend {
  39. case let x where x <= -30:
  40. return .downDownDown
  41. case let x where x <= -20:
  42. return .downDown
  43. case let x where x <= -10:
  44. return .down
  45. case let x where x < 10:
  46. return .flat
  47. case let x where x < 20:
  48. return .up
  49. case let x where x < 30:
  50. return .upUp
  51. default:
  52. return .upUpUp
  53. }
  54. }
  55. public var isLocal: Bool {
  56. return true
  57. }
  58. // TODO Placeholders. This functionality will come with LOOP-1311
  59. public var glucoseRangeCategory: GlucoseRangeCategory? {
  60. return nil
  61. }
  62. }
  63. extension Glucose {
  64. public var condition: GlucoseCondition? {
  65. if glucoseMessage.glucose < GlucoseLimits.minimum {
  66. return .belowRange
  67. } else if glucoseMessage.glucose > GlucoseLimits.maximum {
  68. return .aboveRange
  69. } else {
  70. return nil
  71. }
  72. }
  73. }