HomeRootView.swift 15 KB

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