HomeRootView.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. 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. Spacer()
  64. }.frame(maxWidth: .infinity)
  65. }
  66. var infoPanal: some View {
  67. HStack(alignment: .firstTextBaseline) {
  68. if let tempRate = viewModel.tempRate {
  69. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  70. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  71. .padding(.leading, 8)
  72. }
  73. if let tepmTargetName = viewModel.tempTargetName {
  74. Text(tepmTargetName).font(.caption).foregroundColor(.secondary)
  75. }
  76. Spacer()
  77. }
  78. .frame(maxWidth: .infinity, maxHeight: 30)
  79. }
  80. var body: some View {
  81. GeometryReader { geo in
  82. VStack(spacing: 0) {
  83. header
  84. .frame(maxHeight: 70)
  85. .padding(.top, geo.safeAreaInsets.top)
  86. .background(Color.gray.opacity(0.2))
  87. infoPanal
  88. MainChartView(
  89. glucose: $viewModel.glucose,
  90. suggestion: $viewModel.suggestion,
  91. tempBasals: $viewModel.tempBasals,
  92. boluses: $viewModel.boluses,
  93. hours: .constant(viewModel.filteredHours),
  94. maxBasal: $viewModel.maxBasal,
  95. basalProfile: $viewModel.basalProfile,
  96. tempTargets: $viewModel.tempTargets,
  97. carbs: $viewModel.carbs,
  98. units: viewModel.units
  99. )
  100. .padding(.bottom)
  101. ZStack {
  102. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  103. HStack {
  104. Button { viewModel.showModal(for: .addCarbs) }
  105. label: {
  106. Image("carbs")
  107. .renderingMode(.template)
  108. .resizable()
  109. .frame(width: 24, height: 24)
  110. }.foregroundColor(.loopGreen)
  111. Spacer()
  112. Button { viewModel.showModal(for: .addTempTarget) }
  113. label: {
  114. Image("target")
  115. .renderingMode(.template)
  116. .resizable()
  117. .frame(width: 24, height: 24)
  118. }.foregroundColor(.loopYellow)
  119. Spacer()
  120. Button { viewModel.showModal(for: .bolus) }
  121. label: {
  122. Image("bolus")
  123. .renderingMode(.template)
  124. .resizable()
  125. .frame(width: 24, height: 24)
  126. }.foregroundColor(.insulin)
  127. Spacer()
  128. if viewModel.allowManualTemp {
  129. Button { viewModel.showModal(for: .manualTempBasal) }
  130. label: {
  131. Image("bolus1")
  132. .renderingMode(.template)
  133. .resizable()
  134. .frame(width: 24, height: 24)
  135. }.foregroundColor(.insulin)
  136. Spacer()
  137. }
  138. Button { viewModel.showModal(for: .settings) }
  139. label: {
  140. Image("settings1")
  141. .renderingMode(.template)
  142. .resizable()
  143. .frame(width: 24, height: 24)
  144. }.foregroundColor(.loopGray)
  145. }
  146. .padding(.horizontal, 24)
  147. .padding(.bottom, geo.safeAreaInsets.bottom)
  148. }
  149. }
  150. .edgesIgnoringSafeArea(.vertical)
  151. }
  152. .navigationTitle("Home")
  153. .navigationBarHidden(true)
  154. .ignoresSafeArea(.keyboard)
  155. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  156. VStack(alignment: .leading) {
  157. Text(viewModel.statusTitle).foregroundColor(.white)
  158. .padding(.bottom, 4)
  159. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  160. }
  161. .padding()
  162. .background(
  163. RoundedRectangle(cornerRadius: 8, style: .continuous)
  164. .fill(Color(UIColor.darkGray))
  165. )
  166. .onTapGesture {
  167. isStatusPopupPresented = false
  168. }
  169. .gesture(
  170. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  171. .onEnded { value in
  172. if value.translation.height < 0 {
  173. isStatusPopupPresented = false
  174. }
  175. }
  176. )
  177. }
  178. }
  179. }
  180. }