PumpView.swift 7.3 KB

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