PumpView.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 pumpStatusHighlightMessage: String?
  9. let battery: [OpenAPS_Battery]
  10. @Environment(\.colorScheme) var colorScheme
  11. private var batteryFormatter: NumberFormatter {
  12. let formatter = NumberFormatter()
  13. formatter.numberStyle = .percent
  14. return formatter
  15. }
  16. private var hourglassIcon: String {
  17. guard let expiration = expiresAtDate else { return "hourglass" }
  18. let hoursRemaining = expiration.timeIntervalSince(timerDate) / 3600
  19. switch hoursRemaining {
  20. case 60 ... 72:
  21. return "hourglass.bottomhalf.filled"
  22. case 12 ..< 60:
  23. return "hourglass"
  24. case -8 ..< 12:
  25. return "hourglass.tophalf.filled"
  26. default:
  27. return "hourglass"
  28. }
  29. }
  30. var body: some View {
  31. if let pumpStatusHighlightMessage = pumpStatusHighlightMessage { // display message instead pump info
  32. VStack(alignment: .center) {
  33. Text(pumpStatusHighlightMessage).font(.footnote).fontWeight(.bold)
  34. .multilineTextAlignment(.center).frame(maxWidth: /*@START_MENU_TOKEN@*/ .infinity/*@END_MENU_TOKEN@*/)
  35. }.frame(width: 100)
  36. } else {
  37. VStack(alignment: .leading, spacing: 20) {
  38. if reservoir == nil && battery.isEmpty {
  39. VStack(alignment: .center, spacing: 12) {
  40. HStack {
  41. Image(systemName: "keyboard.onehanded.left")
  42. .font(.body)
  43. .imageScale(.large)
  44. }
  45. HStack {
  46. Text("Add pump")
  47. .font(.caption)
  48. .bold()
  49. }
  50. }
  51. .frame(alignment: .top)
  52. }
  53. if let reservoir = reservoir {
  54. HStack {
  55. Image(systemName: "cross.vial.fill")
  56. .font(.callout)
  57. if reservoir == 0xDEAD_BEEF {
  58. Text("50+ " + String(localized: "U", comment: "Insulin unit"))
  59. .font(.callout)
  60. .fontWeight(.bold)
  61. .fontDesign(.rounded)
  62. } else {
  63. Text(
  64. Formatter.integerFormatter
  65. .string(from: reservoir as NSNumber)! + String(localized: " U", comment: "Insulin unit")
  66. )
  67. .font(.callout)
  68. .fontWeight(.bold)
  69. .fontDesign(.rounded)
  70. }
  71. }
  72. .padding(.vertical, 5)
  73. .padding(.horizontal, 10)
  74. .foregroundStyle(reservoirColor)
  75. .overlay(
  76. Capsule()
  77. .stroke(reservoirColor.opacity(0.4), lineWidth: 2)
  78. )
  79. }
  80. if (battery.first?.display) != nil, let shouldBatteryDisplay = battery.first?.display, shouldBatteryDisplay {
  81. HStack {
  82. Image(systemName: "battery.100")
  83. .font(.callout)
  84. .foregroundStyle(batteryColor)
  85. Text("\(Formatter.integerFormatter.string(for: battery.first?.percent ?? 100) ?? "100") %")
  86. .font(.callout).fontWeight(.bold).fontDesign(.rounded)
  87. }
  88. }
  89. if let date = expiresAtDate {
  90. HStack {
  91. Image(systemName: hourglassIcon)
  92. .font(.callout)
  93. .foregroundStyle(timerColor, Color.yellow)
  94. .symbolRenderingMode(.palette)
  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 String(localized: "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)" + String(localized: "d", comment: "abbreviation for days") + " \(hours)" +
  127. String(localized: "h", comment: "abbreviation for hours")
  128. }
  129. if hours >= 1 {
  130. var remainingHoursString = "\(hours)" + String(localized: "h", comment: "abbreviation for hours")
  131. if hours < 12 {
  132. remainingHoursString += " " + "\(minutes)" +
  133. String(localized: "m", comment: "abbreviation for minutes")
  134. }
  135. return remainingHoursString
  136. }
  137. return "\(minutes)" + String(localized: "m", comment: "abbreviation for minutes")
  138. }
  139. private var batteryColor: Color {
  140. guard let battery = battery.first else {
  141. return .gray
  142. }
  143. switch battery.percent {
  144. case ...10:
  145. return Color.loopRed
  146. case ...20:
  147. return Color.orange
  148. default:
  149. return Color.loopGreen
  150. }
  151. }
  152. private var reservoirColor: Color {
  153. guard let reservoir = reservoir else {
  154. return .gray
  155. }
  156. switch reservoir {
  157. case ...10:
  158. return Color.loopRed
  159. case ...30:
  160. return Color.orange
  161. default:
  162. return Color.insulin
  163. }
  164. }
  165. private var timerColor: Color {
  166. guard let expisesAt = expiresAtDate else {
  167. return .gray
  168. }
  169. let time = expisesAt.timeIntervalSince(timerDate)
  170. switch time {
  171. case ...8.hours.timeInterval:
  172. return Color.loopRed
  173. case ...1.days.timeInterval:
  174. return Color.orange
  175. default:
  176. return Color.loopGreen
  177. }
  178. }
  179. }
  180. // #Preview("message") {
  181. // PumpView(
  182. // reservoir: .constant(Decimal(10.0)),
  183. // battery: .constant(nil),
  184. // name: .constant("Pump test"),
  185. // expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  186. // timerDate: .constant(Date()),
  187. // pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
  188. // )
  189. // }
  190. //
  191. // #Preview("pump reservoir") {
  192. // PumpView(
  193. // reservoir: .constant(Decimal(40.0)),
  194. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  195. // name: .constant("Pump test"),
  196. // expiresAtDate: .constant(nil),
  197. // timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  198. // pumpStatusHighlightMessage: .constant(nil)
  199. // )
  200. // }
  201. //
  202. // #Preview("pump expiration") {
  203. // PumpView(
  204. // reservoir: .constant(Decimal(10.0)),
  205. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  206. // name: .constant("Pump test"),
  207. // expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  208. // timerDate: .constant(Date().addingTimeInterval(2.hours)),
  209. // pumpStatusHighlightMessage: .constant(nil)
  210. // )
  211. // }
  212. //
  213. // #Preview("no pump") {
  214. // PumpView(
  215. // reservoir: .constant(nil),
  216. // name: .constant(nil),
  217. // expiresAtDate: .constant(""),
  218. // timerDate: .constant(nil),
  219. // timeZone: .constant(Date()),
  220. // pumpStatusHighlightMessage: .constant(nil)
  221. // )
  222. // }