ConcreteGlucoseDisplayable.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // ConcreteSensorDisplayable.swift
  3. // MiaomiaoClient
  4. //
  5. // Created by Bjørn Inge Berg on 04/11/2019.
  6. // Copyright © 2019 Bjørn Inge Berg. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. public struct ConcreteGlucoseDisplayable: GlucoseDisplayable {
  11. public var glucoseRangeCategory: GlucoseRangeCategory?
  12. public var isStateValid: Bool
  13. public var trendType: GlucoseTrend?
  14. public var isLocal: Bool
  15. public var batteries : [(name: String, percentage: Int)]?
  16. }
  17. public enum GlucoseRangeCategory: Int, CaseIterable {
  18. case belowRange
  19. case urgentLow
  20. case low
  21. case normal
  22. case high
  23. case aboveRange
  24. }
  25. public enum GlucoseTrend: Int, CaseIterable {
  26. case upUpUp = 1
  27. case upUp = 2
  28. case up = 3
  29. case flat = 4
  30. case down = 5
  31. case downDown = 6
  32. case downDownDown = 7
  33. public var symbol: String {
  34. switch self {
  35. case .upUpUp:
  36. return "⇈"
  37. case .upUp:
  38. return "↑"
  39. case .up:
  40. return "↗︎"
  41. case .flat:
  42. return "→"
  43. case .down:
  44. return "↘︎"
  45. case .downDown:
  46. return "↓"
  47. case .downDownDown:
  48. return "⇊"
  49. }
  50. }
  51. public var arrows: String {
  52. switch self {
  53. case .upUpUp:
  54. return "↑↑"
  55. case .upUp:
  56. return "↑"
  57. case .up:
  58. return "↗︎"
  59. case .flat:
  60. return "→"
  61. case .down:
  62. return "↘︎"
  63. case .downDown:
  64. return "↓"
  65. case .downDownDown:
  66. return "↓↓"
  67. }
  68. }
  69. public var localizedDescription: String {
  70. switch self {
  71. case .upUpUp:
  72. return LocalizedString("Rising very fast", comment: "Glucose trend up-up-up")
  73. case .upUp:
  74. return LocalizedString("Rising fast", comment: "Glucose trend up-up")
  75. case .up:
  76. return LocalizedString("Rising", comment: "Glucose trend up")
  77. case .flat:
  78. return LocalizedString("Flat", comment: "Glucose trend flat")
  79. case .down:
  80. return LocalizedString("Falling", comment: "Glucose trend down")
  81. case .downDown:
  82. return LocalizedString("Falling fast", comment: "Glucose trend down-down")
  83. case .downDownDown:
  84. return LocalizedString("Falling very fast", comment: "Glucose trend down-down-down")
  85. }
  86. }
  87. public var direction: String {
  88. switch self {
  89. case .upUpUp:
  90. return "DoubleUp"
  91. case .upUp:
  92. return "SingleUp"
  93. case .up:
  94. return "FortyFiveUp"
  95. case .flat:
  96. return "Flat"
  97. case .down:
  98. return "FortyFiveDown"
  99. case .downDown:
  100. return "SingleDown"
  101. case .downDownDown:
  102. return "DoubleDown"
  103. }
  104. }
  105. }
  106. public protocol GlucoseDisplayable {
  107. /// Returns whether the current state is valid
  108. var isStateValid: Bool { get }
  109. /// Describes the state of the sensor in the current localization
  110. var stateDescription: String { get }
  111. /// Enumerates the trend of the sensor values
  112. var trendType: GlucoseTrend? { get }
  113. /// Returns whether the data is from a locally-connected device
  114. var isLocal: Bool { get }
  115. /// enumerates the glucose value type (e.g., normal, low, high)
  116. var glucoseRangeCategory: GlucoseRangeCategory? { get }
  117. }
  118. extension GlucoseDisplayable {
  119. public var stateDescription: String {
  120. if isStateValid {
  121. return LocalizedString("OK", comment: "Sensor state description for the valid state")
  122. } else {
  123. return LocalizedString("Needs Attention", comment: "Sensor state description for the non-valid state")
  124. }
  125. }
  126. }