PumpView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. @Binding var boluses: [PumpHistoryEvent]
  9. @State var state: Home.StateModel
  10. @State var totalBolus: Decimal = 0
  11. private var reservoirFormatter: NumberFormatter {
  12. let formatter = NumberFormatter()
  13. formatter.numberStyle = .decimal
  14. formatter.maximumFractionDigits = 0
  15. return formatter
  16. }
  17. private var batteryFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .percent
  20. return formatter
  21. }
  22. private var numberFormatter: NumberFormatter {
  23. let formatter = NumberFormatter()
  24. formatter.numberStyle = .decimal
  25. formatter.maximumFractionDigits = 2
  26. return formatter
  27. }
  28. var body: some View {
  29. HStack {
  30. Text("IOB").font(.callout).foregroundColor(.secondary)
  31. Text(
  32. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  33. NSLocalizedString(" U", comment: "Insulin unit")
  34. )
  35. .font(.callout).fontWeight(.bold)
  36. Spacer()
  37. Text("COB").font(.callout).foregroundColor(.secondary)
  38. Text(
  39. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  40. NSLocalizedString(" g", comment: "gram of carbs")
  41. )
  42. .font(.callout).fontWeight(.bold)
  43. Spacer()
  44. if let reservoir = reservoir {
  45. HStack {
  46. Image(systemName: "drop.fill")
  47. .resizable()
  48. .aspectRatio(contentMode: .fit)
  49. .frame(maxHeight: 15)
  50. .foregroundColor(reservoirColor)
  51. if reservoir == 0xDEAD_BEEF {
  52. Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.callout)
  53. .fontWeight(.bold)
  54. } else {
  55. Text(
  56. reservoirFormatter
  57. .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  58. )
  59. .font(.callout).fontWeight(.bold)
  60. }
  61. }
  62. }
  63. Spacer()
  64. if let battery = battery, battery.display ?? false, expiresAtDate == nil {
  65. HStack {
  66. Image(systemName: "battery.100")
  67. .resizable()
  68. .aspectRatio(contentMode: .fit)
  69. .frame(maxHeight: 15)
  70. .foregroundColor(batteryColor)
  71. Text("\(Int(battery.percent ?? 100)) %").font(.callout)
  72. .fontWeight(.bold)
  73. Text(calculateTINS())
  74. .font(.callout).fontWeight(.bold)
  75. }
  76. }
  77. if let date = expiresAtDate {
  78. HStack {
  79. Image(systemName: "stopwatch.fill")
  80. .resizable()
  81. .aspectRatio(contentMode: .fit)
  82. .frame(maxHeight: 15)
  83. .foregroundColor(timerColor)
  84. Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.callout)
  85. .fontWeight(.bold)
  86. }
  87. }
  88. }
  89. }
  90. //MARK: WORKS....BUT MAYBE TIMEZONE PROBLEMS COULD OCCUR
  91. //DEFINETELY SOMETHING FOR OUR TIMEZONE EXPERT.....
  92. func calculateTINS() -> String {
  93. let date = Date()
  94. let calendar = Calendar.current
  95. let offset = state.scale
  96. var offsetComponents = DateComponents()
  97. offsetComponents.hour = -offset.rawValue
  98. let startTime = calendar.date(byAdding: offsetComponents, to: date)!
  99. print("******************")
  100. print("die voll krasse start time ist: \(startTime)")
  101. let bolusesForCurrentDay = boluses.filter { $0.timestamp >= startTime && $0.type == .bolus }
  102. let totalBolus = bolusesForCurrentDay.map { $0.amount ?? 0 }.reduce(0, +)
  103. let roundedTotalBolus = Decimal(round(100 * Double(totalBolus)) / 100)
  104. return "\(roundedTotalBolus) U"
  105. }
  106. private func remainingTimeString(time: TimeInterval) -> String {
  107. guard time > 0 else {
  108. return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
  109. }
  110. var time = time
  111. let days = Int(time / 1.days.timeInterval)
  112. time -= days.days.timeInterval
  113. let hours = Int(time / 1.hours.timeInterval)
  114. time -= hours.hours.timeInterval
  115. let minutes = Int(time / 1.minutes.timeInterval)
  116. if days >= 1 {
  117. return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
  118. NSLocalizedString("h", comment: "abbreviation for hours")
  119. }
  120. if hours >= 1 {
  121. return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
  122. }
  123. return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
  124. }
  125. private var batteryColor: Color {
  126. guard let battery = battery, let percent = battery.percent else {
  127. return .gray
  128. }
  129. switch percent {
  130. case ...10:
  131. return .red
  132. case ...20:
  133. return .yellow
  134. default:
  135. return .green
  136. }
  137. }
  138. private var reservoirColor: Color {
  139. guard let reservoir = reservoir else {
  140. return .gray
  141. }
  142. switch reservoir {
  143. case ...10:
  144. return .red
  145. case ...30:
  146. return .yellow
  147. default:
  148. return .blue
  149. }
  150. }
  151. private var timerColor: Color {
  152. guard let expisesAt = expiresAtDate else {
  153. return .gray
  154. }
  155. let time = expisesAt.timeIntervalSince(timerDate)
  156. switch time {
  157. case ...8.hours.timeInterval:
  158. return .red
  159. case ...1.days.timeInterval:
  160. return .yellow
  161. default:
  162. return .green
  163. }
  164. }
  165. }
  166. struct Hairline: View {
  167. let color: Color
  168. var body: some View {
  169. Rectangle()
  170. .fill(color)
  171. .frame(width: UIScreen.main.bounds.width / 1.3, height: 1)
  172. .opacity(0.5)
  173. }
  174. }