HomeRootView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 legendPanal: some View {
  84. HStack(alignment: .firstTextBaseline) {
  85. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  86. .padding(.leading, 8)
  87. Text("BG")
  88. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  89. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  90. .padding(.leading, 8)
  91. Text("IOB")
  92. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  93. Circle().fill(Color.zt).frame(width: 8, height: 8)
  94. .padding(.leading, 8)
  95. Text("ZT")
  96. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  97. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  98. .padding(.leading, 8)
  99. Text("COB")
  100. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  101. Circle().fill(Color.uam).frame(width: 8, height: 8)
  102. .padding(.leading, 8)
  103. Text("UAM")
  104. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  105. }
  106. .frame(maxWidth: .infinity, maxHeight: 30)
  107. }
  108. var body: some View {
  109. GeometryReader { geo in
  110. VStack(spacing: 0) {
  111. header
  112. .frame(maxHeight: 70)
  113. .padding(.top, geo.safeAreaInsets.top)
  114. .background(Color.gray.opacity(0.2))
  115. infoPanal
  116. MainChartView(
  117. glucose: $viewModel.glucose,
  118. suggestion: $viewModel.suggestion,
  119. tempBasals: $viewModel.tempBasals,
  120. boluses: $viewModel.boluses,
  121. hours: .constant(viewModel.filteredHours),
  122. maxBasal: $viewModel.maxBasal,
  123. basalProfile: $viewModel.basalProfile,
  124. tempTargets: $viewModel.tempTargets,
  125. carbs: $viewModel.carbs,
  126. units: viewModel.units
  127. )
  128. .padding(.bottom)
  129. legendPanal
  130. ZStack {
  131. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  132. HStack {
  133. Button { viewModel.showModal(for: .addCarbs) }
  134. label: {
  135. Image("carbs")
  136. .renderingMode(.template)
  137. .resizable()
  138. .frame(width: 24, height: 24)
  139. }.foregroundColor(.loopGreen)
  140. Spacer()
  141. Button { viewModel.showModal(for: .addTempTarget) }
  142. label: {
  143. Image("target")
  144. .renderingMode(.template)
  145. .resizable()
  146. .frame(width: 24, height: 24)
  147. }.foregroundColor(.loopYellow)
  148. Spacer()
  149. Button { viewModel.showModal(for: .bolus) }
  150. label: {
  151. Image("bolus")
  152. .renderingMode(.template)
  153. .resizable()
  154. .frame(width: 24, height: 24)
  155. }.foregroundColor(.insulin)
  156. Spacer()
  157. if viewModel.allowManualTemp {
  158. Button { viewModel.showModal(for: .manualTempBasal) }
  159. label: {
  160. Image("bolus1")
  161. .renderingMode(.template)
  162. .resizable()
  163. .frame(width: 24, height: 24)
  164. }.foregroundColor(.insulin)
  165. Spacer()
  166. }
  167. Button { viewModel.showModal(for: .settings) }
  168. label: {
  169. Image("settings1")
  170. .renderingMode(.template)
  171. .resizable()
  172. .frame(width: 24, height: 24)
  173. }.foregroundColor(.loopGray)
  174. }
  175. .padding(.horizontal, 24)
  176. .padding(.bottom, geo.safeAreaInsets.bottom)
  177. }
  178. }
  179. .edgesIgnoringSafeArea(.vertical)
  180. }
  181. .navigationTitle("Home")
  182. .navigationBarHidden(true)
  183. .ignoresSafeArea(.keyboard)
  184. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  185. VStack(alignment: .leading) {
  186. Text(viewModel.statusTitle).foregroundColor(.white)
  187. .padding(.bottom, 4)
  188. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  189. }
  190. .padding()
  191. .background(
  192. RoundedRectangle(cornerRadius: 8, style: .continuous)
  193. .fill(Color(UIColor.darkGray))
  194. )
  195. .onTapGesture {
  196. isStatusPopupPresented = false
  197. }
  198. .gesture(
  199. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  200. .onEnded { value in
  201. if value.translation.height < 0 {
  202. isStatusPopupPresented = false
  203. }
  204. }
  205. )
  206. }
  207. }
  208. }
  209. }