PumpView.swift 7.5 KB

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