HomeRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. private var dateFormatter: DateFormatter {
  20. let dateFormatter = DateFormatter()
  21. dateFormatter.timeStyle = .short
  22. return dateFormatter
  23. }
  24. var header: some View {
  25. HStack(alignment: .bottom) {
  26. Spacer()
  27. VStack(alignment: .leading, spacing: 12) {
  28. // HStack {
  29. // Text("").font(.caption2).foregroundColor(.secondary) // Was "IOB"
  30. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  31. .font(.system(size: 12, weight: .bold))
  32. // }
  33. // HStack {
  34. // Text("").font(.caption2).foregroundColor(.secondary) // Was "COB"
  35. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  36. .font(.system(size: 12, weight: .bold))
  37. // }
  38. }
  39. Spacer()
  40. CurrentGlucoseView(
  41. recentGlucose: $viewModel.recentGlucose,
  42. delta: $viewModel.glucoseDelta,
  43. units: $viewModel.units
  44. )
  45. .onTapGesture {
  46. viewModel.openCGM()
  47. }
  48. Spacer()
  49. PumpView(
  50. reservoir: $viewModel.reservoir,
  51. battery: $viewModel.battery,
  52. name: $viewModel.pumpName,
  53. expiresAtDate: $viewModel.pumpExpiresAtDate,
  54. timerDate: $viewModel.timerDate
  55. )
  56. .onTapGesture {
  57. viewModel.setupPump = true
  58. }
  59. .popover(isPresented: $viewModel.setupPump) {
  60. if let pumpManager = viewModel.provider.apsManager.pumpManager {
  61. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: viewModel)
  62. }
  63. }
  64. Spacer()
  65. LoopView(
  66. suggestion: $viewModel.suggestion,
  67. enactedSuggestion: $viewModel.enactedSuggestion,
  68. closedLoop: $viewModel.closedLoop,
  69. timerDate: $viewModel.timerDate,
  70. isLooping: $viewModel.isLooping,
  71. lastLoopDate: $viewModel.lastLoopDate
  72. ).onTapGesture {
  73. isStatusPopupPresented = true
  74. }.onLongPressGesture {
  75. viewModel.runLoop()
  76. }
  77. Spacer()
  78. }.frame(maxWidth: .infinity)
  79. }
  80. var infoPanal: some View {
  81. HStack(alignment: .center) {
  82. if viewModel.pumpSuspended {
  83. Text("Pump suspended")
  84. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  85. .padding(.leading, 8)
  86. } else if let tempRate = viewModel.tempRate {
  87. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  88. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  89. .padding(.leading, 8)
  90. }
  91. if let tempTarget = viewModel.tempTarget {
  92. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  93. if viewModel.units == .mmolL {
  94. Text(
  95. targetFormatter
  96. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  97. )
  98. .font(.caption)
  99. .foregroundColor(.secondary)
  100. if tempTarget.targetBottom != tempTarget.targetTop {
  101. Text("-").font(.caption)
  102. .foregroundColor(.secondary)
  103. Text(
  104. targetFormatter
  105. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  106. " \(viewModel.units.rawValue)"
  107. )
  108. .font(.caption)
  109. .foregroundColor(.secondary)
  110. } else {
  111. Text(viewModel.units.rawValue).font(.caption)
  112. .foregroundColor(.secondary)
  113. }
  114. } else {
  115. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  116. .font(.caption)
  117. .foregroundColor(.secondary)
  118. if tempTarget.targetBottom != tempTarget.targetTop {
  119. Text("-").font(.caption)
  120. .foregroundColor(.secondary)
  121. Text(
  122. targetFormatter
  123. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(viewModel.units.rawValue)"
  124. )
  125. .font(.caption)
  126. .foregroundColor(.secondary)
  127. } else {
  128. Text(viewModel.units.rawValue).font(.caption)
  129. .foregroundColor(.secondary)
  130. }
  131. }
  132. }
  133. Spacer()
  134. if let progress = viewModel.bolusProgress {
  135. Text("Bolusing")
  136. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  137. ProgressView(value: Double(progress))
  138. .progressViewStyle(BolusProgressViewStyle())
  139. .padding(.trailing, 8)
  140. .onTapGesture {
  141. viewModel.cancelBolus()
  142. }
  143. }
  144. }
  145. .frame(maxWidth: .infinity, maxHeight: 30)
  146. }
  147. var legendPanal: some View {
  148. HStack(alignment: .center) {
  149. Group {
  150. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  151. Text("BG")
  152. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  153. }
  154. Group {
  155. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  156. .padding(.leading, 8)
  157. Text("IOB")
  158. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  159. }
  160. Group {
  161. Circle().fill(Color.zt).frame(width: 8, height: 8)
  162. .padding(.leading, 8)
  163. Text("ZT")
  164. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  165. }
  166. Group {
  167. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  168. .padding(.leading, 8)
  169. Text("COB")
  170. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  171. }
  172. Group {
  173. Circle().fill(Color.uam).frame(width: 8, height: 8)
  174. .padding(.leading, 8)
  175. Text("UAM")
  176. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  177. }
  178. if let eventualBG = viewModel.eventualBG {
  179. Text(
  180. "⇢ " + numberFormatter.string(
  181. from: (viewModel.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  182. )!
  183. )
  184. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  185. }
  186. }
  187. .frame(maxWidth: .infinity, maxHeight: 30)
  188. }
  189. var body: some View {
  190. GeometryReader { geo in
  191. VStack(spacing: 0) {
  192. header
  193. .frame(maxHeight: 70)
  194. .padding(.top, geo.safeAreaInsets.top)
  195. .background(Color.backgroundColor)
  196. // .background(Color.gray.opacity(0.2))
  197. Divider().background(Color.gray) // Added 29/4
  198. infoPanal
  199. .background(Color.secondary.opacity(0.05))
  200. // Divider().background(Color.gray) // Added 29/4
  201. MainChartView(
  202. glucose: $viewModel.glucose,
  203. suggestion: $viewModel.suggestion,
  204. tempBasals: $viewModel.tempBasals,
  205. boluses: $viewModel.boluses,
  206. suspensions: $viewModel.suspensions,
  207. hours: .constant(viewModel.filteredHours),
  208. maxBasal: $viewModel.maxBasal,
  209. autotunedBasalProfile: $viewModel.autotunedBasalProfile,
  210. basalProfile: $viewModel.basalProfile,
  211. tempTargets: $viewModel.tempTargets,
  212. carbs: $viewModel.carbs,
  213. timerDate: $viewModel.timerDate,
  214. units: $viewModel.units
  215. )
  216. .background(Color.gray.opacity(0.05))
  217. // .padding(.bottom)
  218. .modal(for: .dataTable, from: self)
  219. Divider().background(Color.gray) // Added 29/4
  220. legendPanal
  221. .background(Color.secondary.opacity(0.05))
  222. Divider().background(Color.gray) // Added 29/4
  223. ZStack {
  224. Rectangle().fill(Color.backgroundColor)
  225. .frame(height: 50 + geo.safeAreaInsets.bottom)
  226. HStack {
  227. Button { viewModel.showModal(for: .addCarbs) }
  228. label: {
  229. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  230. Image("carbs")
  231. .renderingMode(.template)
  232. .resizable()
  233. .frame(width: 24, height: 24)
  234. .foregroundColor(.loopGreen)
  235. .padding(8)
  236. if let carbsReq = viewModel.carbsRequired {
  237. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  238. .font(.caption)
  239. .foregroundColor(.white)
  240. .padding(4)
  241. .background(Capsule().fill(Color.red))
  242. }
  243. }
  244. }
  245. Spacer()
  246. Button { viewModel.showModal(for: .addTempTarget) }
  247. label: {
  248. Image("target")
  249. .renderingMode(.template)
  250. .resizable()
  251. .frame(width: 24, height: 24)
  252. .padding(8)
  253. }.foregroundColor(.loopYellow)
  254. Spacer()
  255. Button { viewModel.showModal(for: .bolus(waitForDuggestion: false)) }
  256. label: {
  257. Image("bolus")
  258. .renderingMode(.template)
  259. .resizable()
  260. .frame(width: 24, height: 24)
  261. .padding(8)
  262. }.foregroundColor(.insulin)
  263. Spacer()
  264. if viewModel.allowManualTemp {
  265. Button { viewModel.showModal(for: .manualTempBasal) }
  266. label: {
  267. Image("bolus1")
  268. .renderingMode(.template)
  269. .resizable()
  270. .frame(width: 24, height: 24)
  271. .padding(8)
  272. }.foregroundColor(.insulin)
  273. Spacer()
  274. }
  275. Button { viewModel.showModal(for: .settings) }
  276. label: {
  277. Image("settings1")
  278. .renderingMode(.template)
  279. .resizable()
  280. .frame(width: 24, height: 24)
  281. .padding(8)
  282. }.foregroundColor(.loopGray)
  283. }
  284. .padding(.horizontal, 24)
  285. .padding(.bottom, geo.safeAreaInsets.bottom)
  286. }
  287. }
  288. .edgesIgnoringSafeArea(.vertical)
  289. }
  290. .navigationTitle("Home")
  291. .navigationBarHidden(true)
  292. .ignoresSafeArea(.keyboard)
  293. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  294. VStack(alignment: .leading) {
  295. Text(viewModel.statusTitle).foregroundColor(.white)
  296. .padding(.bottom, 4)
  297. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  298. if let errorMessage = viewModel.errorMessage, let date = viewModel.errorDate {
  299. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  300. .padding(.bottom, 4)
  301. .padding(.top, 8)
  302. Text(errorMessage).font(.caption).foregroundColor(.white)
  303. }
  304. }
  305. .padding()
  306. .background(
  307. RoundedRectangle(cornerRadius: 8, style: .continuous)
  308. .fill(Color(UIColor.darkGray))
  309. )
  310. .onTapGesture {
  311. isStatusPopupPresented = false
  312. }
  313. .gesture(
  314. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  315. .onEnded { value in
  316. if value.translation.height < 0 {
  317. isStatusPopupPresented = false
  318. }
  319. }
  320. )
  321. }
  322. }
  323. }
  324. }