PumpView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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(.callout).fontWeight(.bold).fontDesign(.rounded)
  79. }
  80. }
  81. }
  82. }
  83. }
  84. private func remainingTimeString(time: TimeInterval) -> String {
  85. guard time > 0 else {
  86. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  87. }
  88. var time = time
  89. let days = Int(time / 1.days.timeInterval)
  90. time -= days.days.timeInterval
  91. let hours = Int(time / 1.hours.timeInterval)
  92. time -= hours.hours.timeInterval
  93. let minutes = Int(time / 1.minutes.timeInterval)
  94. if days >= 1 {
  95. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  96. NSLocalizedString("h", comment: "abbreviation for hours")
  97. }
  98. if hours >= 1 {
  99. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  100. }
  101. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  102. }
  103. private var batteryColor: Color {
  104. guard let battery = battery.first else {
  105. return .gray
  106. }
  107. switch battery.percent {
  108. case ...10:
  109. return .red
  110. case ...20:
  111. return .yellow
  112. default:
  113. return .green
  114. }
  115. }
  116. private var reservoirColor: Color {
  117. guard let reservoir = reservoir else {
  118. return .gray
  119. }
  120. switch reservoir {
  121. case ...10:
  122. return .red
  123. case ...30:
  124. return .yellow
  125. default:
  126. return .blue
  127. }
  128. }
  129. private var timerColor: Color {
  130. guard let expisesAt = expiresAtDate else {
  131. return .gray
  132. }
  133. let time = expisesAt.timeIntervalSince(timerDate)
  134. switch time {
  135. case ...8.hours.timeInterval:
  136. return .red
  137. case ...1.days.timeInterval:
  138. return .yellow
  139. default:
  140. return .green
  141. }
  142. }
  143. }
  144. // #Preview("message") {
  145. // PumpView(
  146. // reservoir: .constant(Decimal(10.0)),
  147. // battery: .constant(nil),
  148. // name: .constant("Pump test"),
  149. // expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
  150. // timerDate: .constant(Date()),
  151. // pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
  152. // )
  153. // }
  154. //
  155. // #Preview("pump reservoir") {
  156. // PumpView(
  157. // reservoir: .constant(Decimal(40.0)),
  158. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
  159. // name: .constant("Pump test"),
  160. // expiresAtDate: .constant(nil),
  161. // timerDate: .constant(Date().addingTimeInterval(-24.hours)),
  162. // pumpStatusHighlightMessage: .constant(nil)
  163. // )
  164. // }
  165. //
  166. // #Preview("pump expiration") {
  167. // PumpView(
  168. // reservoir: .constant(Decimal(10.0)),
  169. // battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
  170. // name: .constant("Pump test"),
  171. // expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
  172. // timerDate: .constant(Date().addingTimeInterval(2.hours)),
  173. // pumpStatusHighlightMessage: .constant(nil)
  174. // )
  175. // }
  176. //
  177. // #Preview("no pump") {
  178. // PumpView(
  179. // reservoir: .constant(nil),
  180. // name: .constant(nil),
  181. // expiresAtDate: .constant(""),
  182. // timerDate: .constant(nil),
  183. // timeZone: .constant(Date()),
  184. // pumpStatusHighlightMessage: .constant(nil)
  185. // )
  186. // }