HomeRootView.swift 8.3 KB

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