HomeRootView.swift 16 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("").font(.caption2).foregroundColor(.secondary) // Was "IOB"
  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("").font(.caption2).foregroundColor(.secondary) // Was "COB"
  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.05))
  202. // .background(Color.gray.opacity(0.2))
  203. Divider().background(Color.gray) // Added 29/4
  204. infoPanal
  205. .background(Color.backgroundColor)
  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. .background(Color.gray.opacity(0.05))
  222. // .padding(.bottom)
  223. .modal(for: .dataTable, from: self)
  224. Divider().background(Color.gray) // Added 29/4
  225. legendPanal
  226. .background(Color.backgroundColor)
  227. Divider().background(Color.gray)
  228. ZStack {
  229. Rectangle().fill(Color.gray.opacity(0.05)).frame(height: 50 + geo.safeAreaInsets.bottom)
  230. HStack {
  231. Button { viewModel.showModal(for: .addCarbs) }
  232. label: {
  233. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  234. Image("carbs")
  235. .renderingMode(.template)
  236. .resizable()
  237. .frame(width: 24, height: 24)
  238. .foregroundColor(.loopGreen)
  239. .padding(8)
  240. if let carbsReq = viewModel.carbsRequired {
  241. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  242. .font(.caption)
  243. .foregroundColor(.white)
  244. .padding(4)
  245. .background(Capsule().fill(Color.red))
  246. }
  247. }
  248. }
  249. Spacer()
  250. Button { viewModel.showModal(for: .addTempTarget) }
  251. label: {
  252. Image("target")
  253. .renderingMode(.template)
  254. .resizable()
  255. .frame(width: 24, height: 24)
  256. .padding(8)
  257. }.foregroundColor(.loopYellow)
  258. Spacer()
  259. Button { viewModel.showModal(for: .bolus(waitForDuggestion: false)) }
  260. label: {
  261. Image("bolus")
  262. .renderingMode(.template)
  263. .resizable()
  264. .frame(width: 24, height: 24)
  265. .padding(8)
  266. }.foregroundColor(.insulin)
  267. Spacer()
  268. if viewModel.allowManualTemp {
  269. Button { viewModel.showModal(for: .manualTempBasal) }
  270. label: {
  271. Image("bolus1")
  272. .renderingMode(.template)
  273. .resizable()
  274. .frame(width: 24, height: 24)
  275. .padding(8)
  276. }.foregroundColor(.insulin)
  277. Spacer()
  278. }
  279. Button { viewModel.showModal(for: .settings) }
  280. label: {
  281. Image("settings1")
  282. .renderingMode(.template)
  283. .resizable()
  284. .frame(width: 24, height: 24)
  285. .padding(8)
  286. }.foregroundColor(.loopGray)
  287. }
  288. .padding(.horizontal, 24)
  289. .padding(.bottom, geo.safeAreaInsets.bottom)
  290. }
  291. }
  292. .edgesIgnoringSafeArea(.vertical)
  293. }
  294. .navigationTitle("Home")
  295. .navigationBarHidden(true)
  296. .ignoresSafeArea(.keyboard)
  297. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  298. VStack(alignment: .leading) {
  299. Text(viewModel.statusTitle).foregroundColor(.white)
  300. .padding(.bottom, 4)
  301. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  302. if let errorMessage = viewModel.errorMessage, let date = viewModel.errorDate {
  303. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  304. .padding(.bottom, 4)
  305. .padding(.top, 8)
  306. Text(errorMessage).font(.caption).foregroundColor(.white)
  307. }
  308. }
  309. .padding()
  310. .background(
  311. RoundedRectangle(cornerRadius: 8, style: .continuous)
  312. .fill(Color(UIColor.darkGray))
  313. )
  314. .onTapGesture {
  315. isStatusPopupPresented = false
  316. }
  317. .gesture(
  318. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  319. .onEnded { value in
  320. if value.translation.height < 0 {
  321. isStatusPopupPresented = false
  322. }
  323. }
  324. )
  325. }
  326. }
  327. }
  328. }