PumpView.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if reservoir == 0xDEAD_BEEF {
  45. Text("50+ " + NSLocalizedString("U", comment: "Insulin unit"))
  46. .font(.callout)
  47. .fontWeight(.bold)
  48. .fontDesign(.rounded)
  49. } else {
  50. Text(
  51. Formatter.integerFormatter
  52. .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  53. )
  54. .font(.callout)
  55. .fontWeight(.bold)
  56. .fontDesign(.rounded)
  57. }
  58. }
  59. .padding(.vertical, 5)
  60. .padding(.horizontal, 10)
  61. .foregroundStyle(reservoirColor)
  62. .overlay(
  63. Capsule()
  64. .stroke(reservoirColor.opacity(0.4), lineWidth: 2)
  65. )
  66. if let timeZone = timeZone, timeZone.secondsFromGMT() != TimeZone.current.secondsFromGMT() {
  67. HStack {
  68. Image(systemName: "clock.badge.exclamationmark.fill")
  69. .font(.callout)
  70. .symbolRenderingMode(.palette)
  71. .foregroundStyle(.red, Color(.warning))
  72. Text("Timezone")
  73. .font(.callout)
  74. .fontWeight(.bold)
  75. .fontDesign(.rounded)
  76. .foregroundStyle(.red)
  77. }
  78. .padding(.leading, 12)
  79. }
  80. }
  81. if (battery.first?.display) != nil, let shouldBatteryDisplay = battery.first?.display, shouldBatteryDisplay {
  82. HStack {
  83. Image(systemName: "battery.100")
  84. .font(.callout)
  85. .foregroundStyle(batteryColor)
  86. Text("\(Formatter.integerFormatter.string(for: battery.first?.percent ?? 100) ?? "100") %")
  87. .font(.callout).fontWeight(.bold).fontDesign(.rounded)
  88. }
  89. }
  90. if let date = expiresAtDate {
  91. HStack {
  92. Image(systemName: "stopwatch.fill")
  93. .font(.callout)
  94. .foregroundStyle(timerColor)
  95. let remainingTimeString = remainingTimeString(time: date.timeIntervalSince(timerDate))
  96. Text(remainingTimeString)
  97. .font(date.timeIntervalSince(timerDate) > 0 ? .callout : .subheadline)
  98. .fontWeight(.bold)
  99. .fontDesign(.rounded)
  100. .lineLimit(2)
  101. .multilineTextAlignment(.leading)
  102. .frame(
  103. // If the string is > 6 chars, i.e., exceeds "xd yh", limit width to 80 pts
  104. // This forces the "Replace pod" string to wrap to 2 lines.
  105. maxWidth: remainingTimeString.count > 6 ? 80 : .infinity,
  106. alignment: .leading
  107. )
  108. }
  109. // aligns the stopwatch icon exactly with the first pixel of the reservoir icon
  110. .padding(.leading, date.timeIntervalSince(timerDate) > 0 ? 12 : 0)
  111. }
  112. }
  113. }
  114. }
  115. private func remainingTimeString(time: TimeInterval) -> String {
  116. guard time > 0 else {
  117. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  118. }
  119. var time = time
  120. let days = Int(time / 1.days.timeInterval)
  121. time -= days.days.timeInterval
  122. let hours = Int(time / 1.hours.timeInterval)
  123. time -= hours.hours.timeInterval
  124. let minutes = Int(time / 1.minutes.timeInterval)
  125. if days >= 1 {
  126. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  127. NSLocalizedString("h", comment: "abbreviation for hours")
  128. }
  129. if hours >= 1 {
  130. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  131. }
  132. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  133. }
  134. private var batteryColor: Color {
  135. guard let battery = battery.first else {
  136. return .gray
  137. }
  138. switch battery.percent {
  139. case ...10:
  140. return Color.loopRed
  141. case ...20:
  142. return Color.orange
  143. default:
  144. return Color.loopGreen
  145. }
  146. }
  147. private var reservoirColor: Color {
  148. guard let reservoir = reservoir else {
  149. return .gray
  150. }
  151. switch reservoir {
  152. case ...10:
  153. return Color.loopRed
  154. case ...30:
  155. return Color.orange
  156. default:
  157. return Color.insulin
  158. }
  159. }
  160. private var timerColor: Color {
  161. guard let expisesAt = expiresAtDate else {
  162. return .gray
  163. }
  164. let time = expisesAt.timeIntervalSince(timerDate)
  165. switch time {
  166. case ...8.hours.timeInterval:
  167. return Color.loopRed
  168. case ...1.days.timeInterval:
  169. return Color.orange
  170. default:
  171. return Color.loopGreen
  172. }
  173. }
  174. }
  175. // #Preview("message") {
  176. // PumpView(
  177. // reservoir: .constant(Decimal(10.0)),
  178. // battery: .constant(nil),
  179. // name: .constant("Pump test"),
  180. // expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  181. // timerDate: .constant(Date()),
  182. // pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
  183. // )
  184. // }
  185. //
  186. // #Preview("pump reservoir") {
  187. // PumpView(
  188. // reservoir: .constant(Decimal(40.0)),
  189. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  190. // name: .constant("Pump test"),
  191. // expiresAtDate: .constant(nil),
  192. // timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  193. // pumpStatusHighlightMessage: .constant(nil)
  194. // )
  195. // }
  196. //
  197. // #Preview("pump expiration") {
  198. // PumpView(
  199. // reservoir: .constant(Decimal(10.0)),
  200. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  201. // name: .constant("Pump test"),
  202. // expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  203. // timerDate: .constant(Date().addingTimeInterval(2.hours)),
  204. // pumpStatusHighlightMessage: .constant(nil)
  205. // )
  206. // }
  207. //
  208. // #Preview("no pump") {
  209. // PumpView(
  210. // reservoir: .constant(nil),
  211. // name: .constant(nil),
  212. // expiresAtDate: .constant(""),
  213. // timerDate: .constant(nil),
  214. // timeZone: .constant(Date()),
  215. // pumpStatusHighlightMessage: .constant(nil)
  216. // )
  217. // }