PumpView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import SwiftUI
  2. struct PumpView: View {
  3. @Binding var reservoir: Decimal?
  4. @Binding var battery: Battery?
  5. @Binding var name: String
  6. @Binding var expiresAtDate: Date?
  7. @Binding var timerDate: Date
  8. @Binding var pumpStatusHighlightMessage: String?
  9. private var reservoirFormatter: NumberFormatter {
  10. let formatter = NumberFormatter()
  11. formatter.numberStyle = .decimal
  12. formatter.maximumFractionDigits = 0
  13. return formatter
  14. }
  15. private var batteryFormatter: NumberFormatter {
  16. let formatter = NumberFormatter()
  17. formatter.numberStyle = .percent
  18. return formatter
  19. }
  20. var body: some View {
  21. if let pumpStatusHighlightMessage = pumpStatusHighlightMessage { // display message instead pump info
  22. VStack(alignment: .center) {
  23. Text(pumpStatusHighlightMessage).font(.footnote).fontWeight(.bold)
  24. .multilineTextAlignment(.center).frame(maxWidth: /*@START_MENU_TOKEN@*/ .infinity/*@END_MENU_TOKEN@*/)
  25. }.frame(width: 100)
  26. } else {
  27. VStack(alignment: .leading, spacing: 12) {
  28. if reservoir == nil && battery == nil {
  29. VStack(alignment: .center, spacing: 12) {
  30. HStack { // no cgm defined so display a generic CGM
  31. Image(systemName: "keyboard.onehanded.left").font(.body).imageScale(.large)
  32. }
  33. HStack {
  34. Text("Add pump").font(.caption).bold()
  35. }
  36. }.frame(alignment: .top)
  37. }
  38. if let reservoir = reservoir {
  39. HStack {
  40. Image(systemName: "drop.fill")
  41. .resizable()
  42. .aspectRatio(contentMode: .fit)
  43. .frame(maxHeight: 10)
  44. .foregroundColor(reservoirColor)
  45. if reservoir == 0xDEAD_BEEF {
  46. Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.footnote)
  47. .fontWeight(.bold)
  48. } else {
  49. Text(
  50. reservoirFormatter
  51. .string(from: reservoir as NSNumber)! +
  52. NSLocalizedString(" U", comment: "Insulin unit")
  53. )
  54. .font(.footnote).fontWeight(.bold)
  55. }
  56. }.frame(alignment: .top)
  57. }
  58. if let battery = battery, battery.display ?? false, expiresAtDate == nil {
  59. HStack {
  60. Image(systemName: "battery.100")
  61. .resizable()
  62. .aspectRatio(contentMode: .fit)
  63. .frame(maxHeight: 10)
  64. .foregroundColor(batteryColor)
  65. Text("\(Int(battery.percent ?? 100)) %").font(.footnote)
  66. .fontWeight(.bold)
  67. }.frame(alignment: .bottom)
  68. }
  69. if let date = expiresAtDate {
  70. HStack {
  71. Image(systemName: "stopwatch.fill")
  72. .resizable()
  73. .aspectRatio(contentMode: .fit)
  74. .frame(maxHeight: 10)
  75. .foregroundColor(timerColor)
  76. Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.footnote)
  77. .fontWeight(.bold)
  78. }.frame(alignment: .bottom)
  79. }
  80. }
  81. }
  82. }
  83. private func remainingTimeString(time: TimeInterval) -> String {
  84. guard time > 0 else {
  85. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  86. }
  87. var time = time
  88. let days = Int(time / 1.days.timeInterval)
  89. time -= days.days.timeInterval
  90. let hours = Int(time / 1.hours.timeInterval)
  91. time -= hours.hours.timeInterval
  92. let minutes = Int(time / 1.minutes.timeInterval)
  93. if days >= 1 {
  94. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  95. NSLocalizedString("h", comment: "abbreviation for hours")
  96. }
  97. if hours >= 1 {
  98. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  99. }
  100. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  101. }
  102. private var batteryColor: Color {
  103. guard let battery = battery, let percent = battery.percent else {
  104. return .gray
  105. }
  106. switch percent {
  107. case ...10:
  108. return .loopRed
  109. case ...20:
  110. return .loopYellow
  111. default:
  112. return .loopGreen
  113. }
  114. }
  115. private var reservoirColor: Color {
  116. guard let reservoir = reservoir else {
  117. return .gray
  118. }
  119. switch reservoir {
  120. case ...10:
  121. return .loopRed
  122. case ...30:
  123. return .loopYellow
  124. default:
  125. return .insulin
  126. }
  127. }
  128. private var timerColor: Color {
  129. guard let expisesAt = expiresAtDate else {
  130. return .gray
  131. }
  132. let time = expisesAt.timeIntervalSince(timerDate)
  133. switch time {
  134. case ...8.hours.timeInterval:
  135. return .loopRed
  136. case ...1.days.timeInterval:
  137. return .loopYellow
  138. default:
  139. return .loopGreen
  140. }
  141. }
  142. }
  143. #Preview("message") {
  144. PumpView(
  145. reservoir: .constant(Decimal(10.0)),
  146. battery: .constant(nil),
  147. name: .constant("Pump test"),
  148. expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  149. timerDate: .constant(Date()),
  150. pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
  151. )
  152. }
  153. #Preview("pump reservoir") {
  154. PumpView(
  155. reservoir: .constant(Decimal(40.0)),
  156. battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  157. name: .constant("Pump test"),
  158. expiresAtDate: .constant(nil),
  159. timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  160. pumpStatusHighlightMessage: .constant(nil)
  161. )
  162. }
  163. #Preview("pump expiration") {
  164. PumpView(
  165. reservoir: .constant(Decimal(10.0)),
  166. battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  167. name: .constant("Pump test"),
  168. expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  169. timerDate: .constant(Date().addingTimeInterval(2.hours)),
  170. pumpStatusHighlightMessage: .constant(nil)
  171. )
  172. }
  173. #Preview("no pump") {
  174. PumpView(
  175. reservoir: .constant(nil),
  176. battery: .constant(nil),
  177. name: .constant(""),
  178. expiresAtDate: .constant(nil),
  179. timerDate: .constant(Date()),
  180. pumpStatusHighlightMessage: .constant(nil)
  181. )
  182. }