HomeRootView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  31. .font(.system(size: 12, weight: .bold))
  32. }
  33. HStack {
  34. Text("COB").font(.caption2).foregroundColor(.secondary)
  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.gray.opacity(0.05))
  196. // .background(Color.gray.opacity(0.2))
  197. infoPanal
  198. .background(Color.backgroundColor)
  199. MainChartView(
  200. glucose: $viewModel.glucose,
  201. suggestion: $viewModel.suggestion,
  202. tempBasals: $viewModel.tempBasals,
  203. boluses: $viewModel.boluses,
  204. suspensions: $viewModel.suspensions,
  205. hours: .constant(viewModel.filteredHours),
  206. maxBasal: $viewModel.maxBasal,
  207. autotunedBasalProfile: $viewModel.autotunedBasalProfile,
  208. basalProfile: $viewModel.basalProfile,
  209. tempTargets: $viewModel.tempTargets,
  210. carbs: $viewModel.carbs,
  211. timerDate: $viewModel.timerDate,
  212. units: $viewModel.units
  213. )
  214. .background(Color.gray.opacity(0.05))
  215. .padding(.bottom)
  216. .modal(for: .dataTable, from: self)
  217. legendPanal
  218. .background(Color.backgroundColor)
  219. ZStack {
  220. Rectangle().fill(Color.gray.opacity(0.05)).frame(height: 50 + geo.safeAreaInsets.bottom)
  221. HStack {
  222. Button { viewModel.showModal(for: .addCarbs) }
  223. label: {
  224. Image("carbs")
  225. .renderingMode(.template)
  226. .resizable()
  227. .frame(width: 24, height: 24)
  228. }.foregroundColor(.loopGreen)
  229. Spacer()
  230. Button { viewModel.showModal(for: .addTempTarget) }
  231. label: {
  232. Image("target")
  233. .renderingMode(.template)
  234. .resizable()
  235. .frame(width: 24, height: 24)
  236. }.foregroundColor(.loopYellow)
  237. Spacer()
  238. Button { viewModel.showModal(for: .bolus(waitForDuggestion: false)) }
  239. label: {
  240. Image("bolus")
  241. .renderingMode(.template)
  242. .resizable()
  243. .frame(width: 24, height: 24)
  244. }.foregroundColor(.insulin)
  245. Spacer()
  246. if viewModel.allowManualTemp {
  247. Button { viewModel.showModal(for: .manualTempBasal) }
  248. label: {
  249. Image("bolus1")
  250. .renderingMode(.template)
  251. .resizable()
  252. .frame(width: 24, height: 24)
  253. }.foregroundColor(.insulin)
  254. Spacer()
  255. }
  256. Button { viewModel.showModal(for: .settings) }
  257. label: {
  258. Image("settings1")
  259. .renderingMode(.template)
  260. .resizable()
  261. .frame(width: 24, height: 24)
  262. }.foregroundColor(.loopGray)
  263. }
  264. .padding(.horizontal, 24)
  265. .padding(.bottom, geo.safeAreaInsets.bottom)
  266. }
  267. }
  268. .edgesIgnoringSafeArea(.vertical)
  269. }
  270. .navigationTitle("Home")
  271. .navigationBarHidden(true)
  272. .ignoresSafeArea(.keyboard)
  273. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  274. VStack(alignment: .leading) {
  275. Text(viewModel.statusTitle).foregroundColor(.white)
  276. .padding(.bottom, 4)
  277. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  278. if let errorMessage = viewModel.errorMessage, let date = viewModel.errorDate {
  279. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  280. .padding(.bottom, 4)
  281. .padding(.top, 8)
  282. Text(errorMessage).font(.caption).foregroundColor(.white)
  283. }
  284. }
  285. .padding()
  286. .background(
  287. RoundedRectangle(cornerRadius: 8, style: .continuous)
  288. .fill(Color(UIColor.darkGray))
  289. )
  290. .onTapGesture {
  291. isStatusPopupPresented = false
  292. }
  293. .gesture(
  294. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  295. .onEnded { value in
  296. if value.translation.height < 0 {
  297. isStatusPopupPresented = false
  298. }
  299. }
  300. )
  301. }
  302. }
  303. }
  304. }