HomeRootView.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isStatusPopupPresented = false
  7. private var numberFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 2
  11. return formatter
  12. }
  13. var header: some View {
  14. HStack(alignment: .bottom) {
  15. VStack(alignment: .leading, spacing: 12) {
  16. HStack {
  17. Text("IOB").font(.caption2).foregroundColor(.secondary)
  18. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  19. .font(.system(size: 12, weight: .bold))
  20. }
  21. HStack {
  22. Text("COB").font(.caption2).foregroundColor(.secondary)
  23. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  24. .font(.system(size: 12, weight: .bold))
  25. }
  26. }
  27. .padding(.leading, 4)
  28. Spacer()
  29. CurrentGlucoseView(
  30. recentGlucose: $viewModel.recentGlucose,
  31. delta: $viewModel.glucoseDelta,
  32. units: viewModel.units
  33. )
  34. Spacer()
  35. PumpView(
  36. reservoir: $viewModel.reservoir,
  37. battery: $viewModel.battery,
  38. name: $viewModel.pumpName,
  39. expiresAtDate: $viewModel.pumpExpiresAtDate,
  40. timerDate: $viewModel.timerDate
  41. )
  42. .onTapGesture {
  43. viewModel.setupPump = true
  44. }
  45. .popover(isPresented: $viewModel.setupPump) {
  46. if let pumpManager = viewModel.provider.apsManager.pumpManager {
  47. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: viewModel)
  48. }
  49. }
  50. Spacer()
  51. LoopView(
  52. suggestion: $viewModel.suggestion,
  53. enactedSuggestion: $viewModel.enactedSuggestion,
  54. closedLoop: $viewModel.closedLoop,
  55. timerDate: $viewModel.timerDate,
  56. isLooping: $viewModel.isLooping,
  57. lastLoopDate: $viewModel.lastLoopDate
  58. ).onTapGesture {
  59. isStatusPopupPresented = true
  60. }.onLongPressGesture {
  61. viewModel.runLoop()
  62. }
  63. }.frame(maxWidth: .infinity)
  64. }
  65. var infoPanal: some View {
  66. HStack(alignment: .firstTextBaseline) {
  67. if let tempRate = viewModel.tempRate {
  68. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  69. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  70. .padding(.leading, 4)
  71. }
  72. if let tepmTargetName = viewModel.tempTargetName {
  73. Text(tepmTargetName).font(.caption).foregroundColor(.secondary)
  74. }
  75. Spacer()
  76. }
  77. .frame(maxWidth: .infinity, maxHeight: 30)
  78. }
  79. var body: some View {
  80. GeometryReader { geo in
  81. VStack(spacing: 0) {
  82. header
  83. .frame(maxHeight: 70)
  84. .padding(.top, geo.safeAreaInsets.top)
  85. .background(Color.gray.opacity(0.2))
  86. infoPanal
  87. MainChartView(
  88. glucose: $viewModel.glucose,
  89. suggestion: $viewModel.suggestion,
  90. tempBasals: $viewModel.tempBasals,
  91. boluses: $viewModel.boluses,
  92. hours: .constant(viewModel.filteredHours),
  93. maxBasal: $viewModel.maxBasal,
  94. basalProfile: $viewModel.basalProfile,
  95. tempTargets: $viewModel.tempTargets,
  96. carbs: $viewModel.carbs,
  97. units: viewModel.units
  98. )
  99. .padding(.bottom)
  100. ZStack {
  101. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  102. HStack {
  103. Button { viewModel.showModal(for: .addCarbs) }
  104. label: {
  105. Image("carbs")
  106. .renderingMode(.template)
  107. .resizable()
  108. .frame(width: 24, height: 24)
  109. }.foregroundColor(.loopGreen)
  110. Spacer()
  111. Button { viewModel.showModal(for: .addTempTarget) }
  112. label: {
  113. Image("target")
  114. .renderingMode(.template)
  115. .resizable()
  116. .frame(width: 24, height: 24)
  117. }.foregroundColor(.loopYellow)
  118. Spacer()
  119. Button { viewModel.showModal(for: .bolus) }
  120. label: {
  121. Image("bolus")
  122. .renderingMode(.template)
  123. .resizable()
  124. .frame(width: 24, height: 24)
  125. }.foregroundColor(.insulin)
  126. Spacer()
  127. if viewModel.allowManualTemp {
  128. Button { viewModel.showModal(for: .manualTempBasal) }
  129. label: {
  130. Image("bolus1")
  131. .renderingMode(.template)
  132. .resizable()
  133. .frame(width: 24, height: 24)
  134. }.foregroundColor(.insulin)
  135. Spacer()
  136. }
  137. Button { viewModel.showModal(for: .settings) }
  138. label: {
  139. Image("settings1")
  140. .renderingMode(.template)
  141. .resizable()
  142. .frame(width: 24, height: 24)
  143. }.foregroundColor(.loopGray)
  144. }
  145. .padding(.horizontal, 24)
  146. .padding(.bottom, geo.safeAreaInsets.bottom)
  147. }
  148. }
  149. .edgesIgnoringSafeArea(.vertical)
  150. }
  151. .navigationTitle("Home")
  152. .navigationBarHidden(true)
  153. .ignoresSafeArea(.keyboard)
  154. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  155. VStack(alignment: .leading) {
  156. Text(viewModel.statusTitle).foregroundColor(.white)
  157. .padding(.bottom, 4)
  158. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  159. }
  160. .padding()
  161. .background(
  162. RoundedRectangle(cornerRadius: 8, style: .continuous)
  163. .fill(Color(UIColor.darkGray))
  164. )
  165. .onTapGesture {
  166. isStatusPopupPresented = false
  167. }
  168. .gesture(
  169. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  170. .onEnded { value in
  171. if value.translation.height < 0 {
  172. isStatusPopupPresented = false
  173. }
  174. }
  175. )
  176. }
  177. }
  178. }
  179. }