PumpView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 pumpStatutHighlightMessage: 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 pumpStatutHighlightMessage = pumpStatutHighlightMessage { // display message instead pump info
  22. VStack(alignment: .center) {
  23. Text(pumpStatutHighlightMessage).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)! + NSLocalizedString(" U", comment: "Insulin unit")
  52. )
  53. .font(.footnote).fontWeight(.bold)
  54. }
  55. }.frame(alignment: .top)
  56. }
  57. if let battery = battery, battery.display ?? false, expiresAtDate == nil {
  58. HStack {
  59. Image(systemName: "battery.100")
  60. .resizable()
  61. .aspectRatio(contentMode: .fit)
  62. .frame(maxHeight: 10)
  63. .foregroundColor(batteryColor)
  64. Text("\(Int(battery.percent ?? 100)) %").font(.footnote)
  65. .fontWeight(.bold)
  66. }.frame(alignment: .bottom)
  67. }
  68. if let date = expiresAtDate {
  69. HStack {
  70. Image(systemName: "stopwatch.fill")
  71. .resizable()
  72. .aspectRatio(contentMode: .fit)
  73. .frame(maxHeight: 10)
  74. .foregroundColor(timerColor)
  75. Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.footnote)
  76. .fontWeight(.bold)
  77. }.frame(alignment: .bottom)
  78. }
  79. }
  80. }
  81. }
  82. private func remainingTimeString(time: TimeInterval) -> String {
  83. guard time > 0 else {
  84. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  85. }
  86. var time = time
  87. let days = Int(time / 1.days.timeInterval)
  88. time -= days.days.timeInterval
  89. let hours = Int(time / 1.hours.timeInterval)
  90. time -= hours.hours.timeInterval
  91. let minutes = Int(time / 1.minutes.timeInterval)
  92. if days >= 1 {
  93. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  94. NSLocalizedString("h", comment: "abbreviation for hours")
  95. }
  96. if hours >= 1 {
  97. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  98. }
  99. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  100. }
  101. private var batteryColor: Color {
  102. guard let battery = battery, let percent = battery.percent else {
  103. return .gray
  104. }
  105. switch percent {
  106. case ...10:
  107. return .loopRed
  108. case ...20:
  109. return .loopYellow
  110. default:
  111. return .loopGreen
  112. }
  113. }
  114. private var reservoirColor: Color {
  115. guard let reservoir = reservoir else {
  116. return .gray
  117. }
  118. switch reservoir {
  119. case ...10:
  120. return .loopRed
  121. case ...30:
  122. return .loopYellow
  123. default:
  124. return .insulin
  125. }
  126. }
  127. private var timerColor: Color {
  128. guard let expisesAt = expiresAtDate else {
  129. return .gray
  130. }
  131. let time = expisesAt.timeIntervalSince(timerDate)
  132. switch time {
  133. case ...8.hours.timeInterval:
  134. return .loopRed
  135. case ...1.days.timeInterval:
  136. return .loopYellow
  137. default:
  138. return .loopGreen
  139. }
  140. }
  141. }
  142. #Preview("message") {
  143. PumpView(
  144. reservoir: .constant(Decimal(10.0)),
  145. battery: .constant(nil),
  146. name: .constant("Pump test"),
  147. expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  148. timerDate: .constant(Date()),
  149. pumpStatutHighlightMessage: .constant("⚠️\n Insulin suspended")
  150. )
  151. }
  152. #Preview("pump reservoir") {
  153. PumpView(
  154. reservoir: .constant(Decimal(40.0)),
  155. battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  156. name: .constant("Pump test"),
  157. expiresAtDate: .constant(nil),
  158. timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  159. pumpStatutHighlightMessage: .constant(nil)
  160. )
  161. }
  162. #Preview("pump expiration") {
  163. PumpView(
  164. reservoir: .constant(Decimal(10.0)),
  165. battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  166. name: .constant("Pump test"),
  167. expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  168. timerDate: .constant(Date().addingTimeInterval(2.hours)),
  169. pumpStatutHighlightMessage: .constant(nil)
  170. )
  171. }
  172. #Preview("no pump") {
  173. PumpView(
  174. reservoir: .constant(nil),
  175. battery: .constant(nil),
  176. name: .constant(""),
  177. expiresAtDate: .constant(nil),
  178. timerDate: .constant(Date()),
  179. pumpStatutHighlightMessage: .constant(nil)
  180. )
  181. }