HomeRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. private var targetFormatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 1
  17. return formatter
  18. }
  19. var header: some View {
  20. HStack(alignment: .bottom) {
  21. Spacer()
  22. VStack(alignment: .leading, spacing: 12) {
  23. HStack {
  24. Text("IOB").font(.caption2).foregroundColor(.secondary)
  25. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  26. .font(.system(size: 12, weight: .bold))
  27. }
  28. HStack {
  29. Text("COB").font(.caption2).foregroundColor(.secondary)
  30. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  31. .font(.system(size: 12, weight: .bold))
  32. }
  33. }
  34. Spacer()
  35. CurrentGlucoseView(
  36. recentGlucose: $viewModel.recentGlucose,
  37. delta: $viewModel.glucoseDelta,
  38. units: viewModel.units
  39. )
  40. .onTapGesture {
  41. viewModel.openCGM()
  42. }
  43. Spacer()
  44. PumpView(
  45. reservoir: $viewModel.reservoir,
  46. battery: $viewModel.battery,
  47. name: $viewModel.pumpName,
  48. expiresAtDate: $viewModel.pumpExpiresAtDate,
  49. timerDate: $viewModel.timerDate
  50. )
  51. .onTapGesture {
  52. viewModel.setupPump = true
  53. }
  54. .popover(isPresented: $viewModel.setupPump) {
  55. if let pumpManager = viewModel.provider.apsManager.pumpManager {
  56. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: viewModel)
  57. }
  58. }
  59. Spacer()
  60. LoopView(
  61. suggestion: $viewModel.suggestion,
  62. enactedSuggestion: $viewModel.enactedSuggestion,
  63. closedLoop: $viewModel.closedLoop,
  64. timerDate: $viewModel.timerDate,
  65. isLooping: $viewModel.isLooping,
  66. lastLoopDate: $viewModel.lastLoopDate
  67. ).onTapGesture {
  68. isStatusPopupPresented = true
  69. }.onLongPressGesture {
  70. viewModel.runLoop()
  71. }
  72. Spacer()
  73. }.frame(maxWidth: .infinity)
  74. }
  75. var infoPanal: some View {
  76. HStack(alignment: .firstTextBaseline) {
  77. if let tempRate = viewModel.tempRate {
  78. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  79. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  80. .padding(.leading, 8)
  81. }
  82. if let tepmTarget = viewModel.tempTarget {
  83. Text(tepmTarget.name).font(.caption).foregroundColor(.secondary)
  84. if viewModel.units == .mmolL {
  85. Text(
  86. targetFormatter
  87. .string(from: tepmTarget.targetBottom.asMmolL as NSNumber)! + " \(viewModel.units.rawValue)"
  88. )
  89. .font(.caption)
  90. .foregroundColor(.secondary)
  91. if tepmTarget.targetBottom != tepmTarget.targetTop {
  92. Text("-").font(.caption)
  93. .foregroundColor(.secondary)
  94. Text(
  95. targetFormatter
  96. .string(from: tepmTarget.targetTop.asMmolL as NSNumber)! + " \(viewModel.units.rawValue)"
  97. )
  98. .font(.caption)
  99. .foregroundColor(.secondary)
  100. }
  101. } else {
  102. Text(targetFormatter.string(from: tepmTarget.targetBottom as NSNumber)! + " \(viewModel.units.rawValue)")
  103. .font(.caption)
  104. .foregroundColor(.secondary)
  105. if tepmTarget.targetBottom != tepmTarget.targetTop {
  106. Text("-").font(.caption)
  107. .foregroundColor(.secondary)
  108. Text(targetFormatter.string(from: tepmTarget.targetTop as NSNumber)! + " \(viewModel.units.rawValue)")
  109. .font(.caption)
  110. .foregroundColor(.secondary)
  111. }
  112. }
  113. }
  114. Spacer()
  115. }
  116. .frame(maxWidth: .infinity, maxHeight: 30)
  117. }
  118. var legendPanal: some View {
  119. HStack(alignment: .firstTextBaseline) {
  120. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  121. .padding(.leading, 8)
  122. Text("BG")
  123. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  124. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  125. .padding(.leading, 8)
  126. Text("IOB")
  127. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  128. Circle().fill(Color.zt).frame(width: 8, height: 8)
  129. .padding(.leading, 8)
  130. Text("ZT")
  131. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  132. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  133. .padding(.leading, 8)
  134. Text("COB")
  135. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  136. Circle().fill(Color.uam).frame(width: 8, height: 8)
  137. .padding(.leading, 8)
  138. Text("UAM")
  139. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  140. }
  141. .frame(maxWidth: .infinity, maxHeight: 30)
  142. }
  143. var body: some View {
  144. GeometryReader { geo in
  145. VStack(spacing: 0) {
  146. header
  147. .frame(maxHeight: 70)
  148. .padding(.top, geo.safeAreaInsets.top)
  149. .background(Color.gray.opacity(0.2))
  150. infoPanal
  151. MainChartView(
  152. glucose: $viewModel.glucose,
  153. suggestion: $viewModel.suggestion,
  154. tempBasals: $viewModel.tempBasals,
  155. boluses: $viewModel.boluses,
  156. hours: .constant(viewModel.filteredHours),
  157. maxBasal: $viewModel.maxBasal,
  158. basalProfile: $viewModel.basalProfile,
  159. tempTargets: $viewModel.tempTargets,
  160. carbs: $viewModel.carbs,
  161. units: viewModel.units
  162. )
  163. .padding(.bottom)
  164. legendPanal
  165. ZStack {
  166. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  167. HStack {
  168. Button { viewModel.showModal(for: .addCarbs) }
  169. label: {
  170. Image("carbs")
  171. .renderingMode(.template)
  172. .resizable()
  173. .frame(width: 24, height: 24)
  174. }.foregroundColor(.loopGreen)
  175. Spacer()
  176. Button { viewModel.showModal(for: .addTempTarget) }
  177. label: {
  178. Image("target")
  179. .renderingMode(.template)
  180. .resizable()
  181. .frame(width: 24, height: 24)
  182. }.foregroundColor(.loopYellow)
  183. Spacer()
  184. Button { viewModel.showModal(for: .bolus) }
  185. label: {
  186. Image("bolus")
  187. .renderingMode(.template)
  188. .resizable()
  189. .frame(width: 24, height: 24)
  190. }.foregroundColor(.insulin)
  191. Spacer()
  192. if viewModel.allowManualTemp {
  193. Button { viewModel.showModal(for: .manualTempBasal) }
  194. label: {
  195. Image("bolus1")
  196. .renderingMode(.template)
  197. .resizable()
  198. .frame(width: 24, height: 24)
  199. }.foregroundColor(.insulin)
  200. Spacer()
  201. }
  202. Button { viewModel.showModal(for: .settings) }
  203. label: {
  204. Image("settings1")
  205. .renderingMode(.template)
  206. .resizable()
  207. .frame(width: 24, height: 24)
  208. }.foregroundColor(.loopGray)
  209. }
  210. .padding(.horizontal, 24)
  211. .padding(.bottom, geo.safeAreaInsets.bottom)
  212. }
  213. }
  214. .edgesIgnoringSafeArea(.vertical)
  215. }
  216. .navigationTitle("Home")
  217. .navigationBarHidden(true)
  218. .ignoresSafeArea(.keyboard)
  219. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  220. VStack(alignment: .leading) {
  221. Text(viewModel.statusTitle).foregroundColor(.white)
  222. .padding(.bottom, 4)
  223. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  224. }
  225. .padding()
  226. .background(
  227. RoundedRectangle(cornerRadius: 8, style: .continuous)
  228. .fill(Color(UIColor.darkGray))
  229. )
  230. .onTapGesture {
  231. isStatusPopupPresented = false
  232. }
  233. .gesture(
  234. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  235. .onEnded { value in
  236. if value.translation.height < 0 {
  237. isStatusPopupPresented = false
  238. }
  239. }
  240. )
  241. }
  242. }
  243. }
  244. }