PumpView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import SwiftUI
  2. struct PumpView: View {
  3. @Binding var reservoir: Decimal?
  4. @Binding var battery: Battery?
  5. @Binding var name: String
  6. @Binding var expiresAtDate: Date?
  7. @Binding var timerDate: Date
  8. @State var state: Home.StateModel
  9. private var reservoirFormatter: NumberFormatter {
  10. let formatter = NumberFormatter()
  11. formatter.numberStyle = .decimal
  12. formatter.maximumFractionDigits = 0
  13. return formatter
  14. }
  15. private var batteryFormatter: NumberFormatter {
  16. let formatter = NumberFormatter()
  17. formatter.numberStyle = .percent
  18. return formatter
  19. }
  20. private var numberFormatter: NumberFormatter {
  21. let formatter = NumberFormatter()
  22. formatter.numberStyle = .decimal
  23. formatter.maximumFractionDigits = 2
  24. return formatter
  25. }
  26. var body: some View {
  27. HStack {
  28. Text("IOB").font(.callout).foregroundColor(.secondary)
  29. Text(
  30. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  31. NSLocalizedString(" U", comment: "Insulin unit")
  32. )
  33. .font(.callout).fontWeight(.bold)
  34. Spacer()
  35. Text("COB").font(.callout).foregroundColor(.secondary)
  36. Text(
  37. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  38. NSLocalizedString(" g", comment: "gram of carbs")
  39. )
  40. .font(.callout).fontWeight(.bold)
  41. Spacer()
  42. if let reservoir = reservoir {
  43. HStack {
  44. Image(systemName: "drop.fill")
  45. .resizable()
  46. .aspectRatio(contentMode: .fit)
  47. .frame(maxHeight: 15)
  48. .foregroundColor(reservoirColor)
  49. if reservoir == 0xDEAD_BEEF {
  50. Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.callout)
  51. .fontWeight(.bold)
  52. } else {
  53. Text(
  54. reservoirFormatter
  55. .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  56. )
  57. .font(.callout).fontWeight(.bold)
  58. }
  59. }
  60. }
  61. Spacer()
  62. if let battery = battery, battery.display ?? false, expiresAtDate == nil {
  63. HStack {
  64. Image(systemName: "battery.100")
  65. .resizable()
  66. .aspectRatio(contentMode: .fit)
  67. .frame(maxHeight: 15)
  68. .foregroundColor(batteryColor)
  69. Text("\(Int(battery.percent ?? 100)) %").font(.callout)
  70. .fontWeight(.bold)
  71. }
  72. }
  73. if let date = expiresAtDate {
  74. HStack {
  75. Image(systemName: "stopwatch.fill")
  76. .resizable()
  77. .aspectRatio(contentMode: .fit)
  78. .frame(maxHeight: 15)
  79. .foregroundColor(timerColor)
  80. Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.callout)
  81. .fontWeight(.bold)
  82. }
  83. }
  84. }
  85. }
  86. private func remainingTimeString(time: TimeInterval) -> String {
  87. guard time > 0 else {
  88. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  89. }
  90. var time = time
  91. let days = Int(time / 1.days.timeInterval)
  92. time -= days.days.timeInterval
  93. let hours = Int(time / 1.hours.timeInterval)
  94. time -= hours.hours.timeInterval
  95. let minutes = Int(time / 1.minutes.timeInterval)
  96. if days >= 1 {
  97. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  98. NSLocalizedString("h", comment: "abbreviation for hours")
  99. }
  100. if hours >= 1 {
  101. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  102. }
  103. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  104. }
  105. private var batteryColor: Color {
  106. guard let battery = battery, let percent = battery.percent else {
  107. return .gray
  108. }
  109. switch percent {
  110. case ...10:
  111. return .red
  112. case ...20:
  113. return .yellow
  114. default:
  115. return .green
  116. }
  117. }
  118. private var reservoirColor: Color {
  119. guard let reservoir = reservoir else {
  120. return .gray
  121. }
  122. switch reservoir {
  123. case ...10:
  124. return .red
  125. case ...30:
  126. return .yellow
  127. default:
  128. return .blue
  129. }
  130. }
  131. private var timerColor: Color {
  132. guard let expisesAt = expiresAtDate else {
  133. return .gray
  134. }
  135. let time = expisesAt.timeIntervalSince(timerDate)
  136. switch time {
  137. case ...8.hours.timeInterval:
  138. return .red
  139. case ...1.days.timeInterval:
  140. return .yellow
  141. default:
  142. return .green
  143. }
  144. }
  145. }
  146. struct Hairline: View {
  147. let color: Color
  148. var body: some View {
  149. Rectangle()
  150. .fill(color)
  151. .frame(width: UIScreen.main.bounds.width / 1.3, height: 1)
  152. .opacity(0.5)
  153. }
  154. }