PumpView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. let remainingTimeString = remainingTimeString(time: date.timeIntervalSince(timerDate))
  81. Text(remainingTimeString)
  82. .font(date.timeIntervalSince(timerDate) > 0 ? .callout : .subheadline)
  83. .fontWeight(.bold)
  84. .fontDesign(.rounded)
  85. .lineLimit(2)
  86. .multilineTextAlignment(.leading)
  87. .frame(
  88. // If the string is > 6 chars, i.e., exceeds "xd yh", limit width to 80 pts
  89. // This forces the "Replace pod" string to wrap to 2 lines.
  90. maxWidth: remainingTimeString.count > 6 ? 80 : .infinity,
  91. alignment: .leading
  92. )
  93. }
  94. // aligns the stopwatch icon exactly with the first pixel of the reservoir icon
  95. .padding(.leading, date.timeIntervalSince(timerDate) > 0 ? 12 : 0)
  96. }
  97. }
  98. }
  99. }
  100. private func remainingTimeString(time: TimeInterval) -> String {
  101. guard time > 0 else {
  102. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  103. }
  104. var time = time
  105. let days = Int(time / 1.days.timeInterval)
  106. time -= days.days.timeInterval
  107. let hours = Int(time / 1.hours.timeInterval)
  108. time -= hours.hours.timeInterval
  109. let minutes = Int(time / 1.minutes.timeInterval)
  110. if days >= 1 {
  111. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  112. NSLocalizedString("h", comment: "abbreviation for hours")
  113. }
  114. if hours >= 1 {
  115. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  116. }
  117. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  118. }
  119. private var batteryColor: Color {
  120. guard let battery = battery.first else {
  121. return .gray
  122. }
  123. switch battery.percent {
  124. case ...10:
  125. return Color.loopRed
  126. case ...20:
  127. return Color.orange
  128. default:
  129. return Color.loopGreen
  130. }
  131. }
  132. private var reservoirColor: Color {
  133. guard let reservoir = reservoir else {
  134. return .gray
  135. }
  136. switch reservoir {
  137. case ...10:
  138. return Color.loopRed
  139. case ...30:
  140. return Color.orange
  141. default:
  142. return Color.insulin
  143. }
  144. }
  145. private var timerColor: Color {
  146. guard let expisesAt = expiresAtDate else {
  147. return .gray
  148. }
  149. let time = expisesAt.timeIntervalSince(timerDate)
  150. switch time {
  151. case ...8.hours.timeInterval:
  152. return Color.loopRed
  153. case ...1.days.timeInterval:
  154. return Color.orange
  155. default:
  156. return Color.loopGreen
  157. }
  158. }
  159. }
  160. // #Preview("message") {
  161. // PumpView(
  162. // reservoir: .constant(Decimal(10.0)),
  163. // battery: .constant(nil),
  164. // name: .constant("Pump test"),
  165. // expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  166. // timerDate: .constant(Date()),
  167. // pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
  168. // )
  169. // }
  170. //
  171. // #Preview("pump reservoir") {
  172. // PumpView(
  173. // reservoir: .constant(Decimal(40.0)),
  174. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  175. // name: .constant("Pump test"),
  176. // expiresAtDate: .constant(nil),
  177. // timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  178. // pumpStatusHighlightMessage: .constant(nil)
  179. // )
  180. // }
  181. //
  182. // #Preview("pump expiration") {
  183. // PumpView(
  184. // reservoir: .constant(Decimal(10.0)),
  185. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  186. // name: .constant("Pump test"),
  187. // expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  188. // timerDate: .constant(Date().addingTimeInterval(2.hours)),
  189. // pumpStatusHighlightMessage: .constant(nil)
  190. // )
  191. // }
  192. //
  193. // #Preview("no pump") {
  194. // PumpView(
  195. // reservoir: .constant(nil),
  196. // name: .constant(nil),
  197. // expiresAtDate: .constant(""),
  198. // timerDate: .constant(nil),
  199. // timeZone: .constant(Date()),
  200. // pumpStatusHighlightMessage: .constant(nil)
  201. // )
  202. // }