HomeRootView.swift 12 KB

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