HomeRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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(
  94. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  95. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  96. )
  97. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  98. .padding(.leading, 8)
  99. }
  100. if let tempTarget = viewModel.tempTarget {
  101. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  102. if viewModel.units == .mmolL {
  103. Text(
  104. targetFormatter
  105. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  106. )
  107. .font(.caption)
  108. .foregroundColor(.secondary)
  109. if tempTarget.targetBottom != tempTarget.targetTop {
  110. Text("-").font(.caption)
  111. .foregroundColor(.secondary)
  112. Text(
  113. targetFormatter
  114. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  115. " \(viewModel.units.rawValue)"
  116. )
  117. .font(.caption)
  118. .foregroundColor(.secondary)
  119. } else {
  120. Text(viewModel.units.rawValue).font(.caption)
  121. .foregroundColor(.secondary)
  122. }
  123. } else {
  124. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  125. .font(.caption)
  126. .foregroundColor(.secondary)
  127. if tempTarget.targetBottom != tempTarget.targetTop {
  128. Text("-").font(.caption)
  129. .foregroundColor(.secondary)
  130. Text(
  131. targetFormatter
  132. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(viewModel.units.rawValue)"
  133. )
  134. .font(.caption)
  135. .foregroundColor(.secondary)
  136. } else {
  137. Text(viewModel.units.rawValue).font(.caption)
  138. .foregroundColor(.secondary)
  139. }
  140. }
  141. }
  142. Spacer()
  143. if let progress = viewModel.bolusProgress {
  144. Text("Bolusing")
  145. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  146. ProgressView(value: Double(progress))
  147. .progressViewStyle(BolusProgressViewStyle())
  148. .padding(.trailing, 8)
  149. .onTapGesture {
  150. viewModel.cancelBolus()
  151. }
  152. }
  153. }
  154. .frame(maxWidth: .infinity, maxHeight: 30)
  155. }
  156. var legendPanal: some View {
  157. HStack(alignment: .center) {
  158. Group {
  159. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  160. Text("BG")
  161. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  162. }
  163. Group {
  164. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  165. .padding(.leading, 8)
  166. Text("IOB")
  167. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  168. }
  169. Group {
  170. Circle().fill(Color.zt).frame(width: 8, height: 8)
  171. .padding(.leading, 8)
  172. Text("ZT")
  173. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  174. }
  175. Group {
  176. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  177. .padding(.leading, 8)
  178. Text("COB")
  179. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  180. }
  181. Group {
  182. Circle().fill(Color.uam).frame(width: 8, height: 8)
  183. .padding(.leading, 8)
  184. Text("UAM")
  185. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  186. }
  187. if let eventualBG = viewModel.eventualBG {
  188. Text(
  189. "⇢ " + numberFormatter.string(
  190. from: (viewModel.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  191. )!
  192. )
  193. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  194. }
  195. }
  196. .frame(maxWidth: .infinity, maxHeight: 30)
  197. }
  198. var body: some View {
  199. GeometryReader { geo in
  200. VStack(spacing: 0) {
  201. header
  202. .frame(maxHeight: 70)
  203. .padding(.top, geo.safeAreaInsets.top)
  204. .background(Color.gray.opacity(0.2))
  205. infoPanal
  206. MainChartView(
  207. glucose: $viewModel.glucose,
  208. suggestion: $viewModel.suggestion,
  209. tempBasals: $viewModel.tempBasals,
  210. boluses: $viewModel.boluses,
  211. suspensions: $viewModel.suspensions,
  212. hours: .constant(viewModel.filteredHours),
  213. maxBasal: $viewModel.maxBasal,
  214. autotunedBasalProfile: $viewModel.autotunedBasalProfile,
  215. basalProfile: $viewModel.basalProfile,
  216. tempTargets: $viewModel.tempTargets,
  217. carbs: $viewModel.carbs,
  218. timerDate: $viewModel.timerDate,
  219. units: $viewModel.units
  220. )
  221. .padding(.bottom)
  222. .modal(for: .dataTable, from: self)
  223. legendPanal
  224. ZStack {
  225. Rectangle().fill(Color.gray.opacity(0.2)).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. }