HomeRootView.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. import SpriteKit
  2. import SwiftDate
  3. import SwiftUI
  4. import Swinject
  5. extension Home {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State var isStatusPopupPresented = false
  10. private var numberFormatter: NumberFormatter {
  11. let formatter = NumberFormatter()
  12. formatter.numberStyle = .decimal
  13. formatter.maximumFractionDigits = 2
  14. return formatter
  15. }
  16. private var targetFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 1
  20. return formatter
  21. }
  22. private var tirFormatter: NumberFormatter {
  23. let formatter = NumberFormatter()
  24. formatter.numberStyle = .decimal
  25. formatter.maximumFractionDigits = 0
  26. return formatter
  27. }
  28. private var dateFormatter: DateFormatter {
  29. let dateFormatter = DateFormatter()
  30. dateFormatter.timeStyle = .short
  31. return dateFormatter
  32. }
  33. private var spriteScene: SKScene {
  34. let scene = SnowScene()
  35. scene.scaleMode = .resizeFill
  36. scene.backgroundColor = .clear
  37. return scene
  38. }
  39. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  40. HStack(alignment: .bottom) {
  41. Spacer()
  42. cobIobView
  43. Spacer()
  44. glucoseView
  45. Spacer()
  46. pumpView
  47. Spacer()
  48. loopView
  49. Spacer()
  50. }
  51. .frame(maxWidth: .infinity)
  52. .frame(maxHeight: 70)
  53. .padding(.top, geo.safeAreaInsets.top)
  54. .background(Color.gray.opacity(0.2))
  55. }
  56. var cobIobView: some View {
  57. VStack(alignment: .leading, spacing: 12) {
  58. HStack {
  59. Text("IOB").font(.caption2).foregroundColor(.secondary)
  60. Text(
  61. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  62. NSLocalizedString(" U", comment: "Insulin unit")
  63. )
  64. .font(.system(size: 12, weight: .bold))
  65. }
  66. HStack {
  67. Text("COB").font(.caption2).foregroundColor(.secondary)
  68. Text(
  69. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  70. NSLocalizedString(" g", comment: "gram of carbs")
  71. )
  72. .font(.system(size: 12, weight: .bold))
  73. }
  74. }
  75. }
  76. var glucoseView: some View {
  77. CurrentGlucoseView(
  78. recentGlucose: $state.recentGlucose,
  79. delta: $state.glucoseDelta,
  80. units: $state.units,
  81. alarm: $state.alarm
  82. )
  83. .onTapGesture {
  84. if state.alarm == nil {
  85. state.openCGM()
  86. } else {
  87. state.showModal(for: .snooze)
  88. }
  89. }
  90. .onLongPressGesture {
  91. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  92. impactHeavy.impactOccurred()
  93. if state.alarm == nil {
  94. state.showModal(for: .snooze)
  95. } else {
  96. state.openCGM()
  97. }
  98. }
  99. }
  100. var pumpView: some View {
  101. PumpView(
  102. reservoir: $state.reservoir,
  103. battery: $state.battery,
  104. name: $state.pumpName,
  105. expiresAtDate: $state.pumpExpiresAtDate,
  106. timerDate: $state.timerDate
  107. )
  108. .onTapGesture {
  109. if state.pumpDisplayState != nil {
  110. state.setupPump = true
  111. }
  112. }
  113. }
  114. var loopView: some View {
  115. LoopView(
  116. suggestion: $state.suggestion,
  117. enactedSuggestion: $state.enactedSuggestion,
  118. closedLoop: $state.closedLoop,
  119. timerDate: $state.timerDate,
  120. isLooping: $state.isLooping,
  121. lastLoopDate: $state.lastLoopDate,
  122. manualTempBasal: $state.manualTempBasal
  123. ).onTapGesture {
  124. isStatusPopupPresented = true
  125. }.onLongPressGesture {
  126. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  127. impactHeavy.impactOccurred()
  128. state.runLoop()
  129. }
  130. }
  131. var infoPanel: some View {
  132. HStack(alignment: .center) {
  133. if state.pumpSuspended {
  134. Text("Pump suspended")
  135. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  136. .padding(.leading, 8)
  137. } else if let tempRate = state.tempRate {
  138. if state.apsManager.isManualTempBasal {
  139. Text(
  140. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  141. NSLocalizedString(" U/hr", comment: "Unit per hour with space") +
  142. NSLocalizedString(" - Manual Basal ⚠️", comment: "Manual Temp basal")
  143. )
  144. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  145. .padding(.leading, 8)
  146. } else {
  147. Text(
  148. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  149. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  150. )
  151. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  152. .padding(.leading, 8)
  153. }
  154. }
  155. if let tempTarget = state.tempTarget {
  156. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  157. if state.units == .mmolL {
  158. Text(
  159. targetFormatter
  160. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  161. )
  162. .font(.caption)
  163. .foregroundColor(.secondary)
  164. if tempTarget.targetBottom != tempTarget.targetTop {
  165. Text("-").font(.caption)
  166. .foregroundColor(.secondary)
  167. Text(
  168. targetFormatter
  169. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  170. " \(state.units.rawValue)"
  171. )
  172. .font(.caption)
  173. .foregroundColor(.secondary)
  174. } else {
  175. Text(state.units.rawValue).font(.caption)
  176. .foregroundColor(.secondary)
  177. }
  178. } else {
  179. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  180. .font(.caption)
  181. .foregroundColor(.secondary)
  182. if tempTarget.targetBottom != tempTarget.targetTop {
  183. Text("-").font(.caption)
  184. .foregroundColor(.secondary)
  185. Text(
  186. targetFormatter
  187. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  188. )
  189. .font(.caption)
  190. .foregroundColor(.secondary)
  191. } else {
  192. Text(state.units.rawValue).font(.caption)
  193. .foregroundColor(.secondary)
  194. }
  195. }
  196. }
  197. Spacer()
  198. if let progress = state.bolusProgress {
  199. Text("Bolusing")
  200. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  201. ProgressView(value: Double(progress))
  202. .progressViewStyle(BolusProgressViewStyle())
  203. .padding(.trailing, 8)
  204. .onTapGesture {
  205. state.cancelBolus()
  206. }
  207. }
  208. }
  209. .frame(maxWidth: .infinity, maxHeight: 30)
  210. }
  211. @ViewBuilder private func statPanel() -> some View {
  212. if state.displayStatistics {
  213. VStack(alignment: .center, spacing: 6) {
  214. HStack {
  215. Group {
  216. Text("Updated").font(.caption2)
  217. .foregroundColor(.secondary)
  218. Text(
  219. dateFormatter.string(from: state.statistics?.created_at ?? Date())
  220. ).font(.system(size: 12))
  221. Text(
  222. NSLocalizedString("Average", comment: "") + " " + state.settingsManager.settings.units.rawValue
  223. ).font(.caption2).foregroundColor(.secondary)
  224. Text(
  225. numberFormatter
  226. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ??
  227. ""
  228. ).font(.system(size: 12))
  229. Text("Median")
  230. .font(.caption2).foregroundColor(.secondary)
  231. Text(
  232. numberFormatter
  233. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ??
  234. ""
  235. ).font(.system(size: 12))
  236. }
  237. }
  238. HStack {
  239. Group {
  240. Text(
  241. NSLocalizedString("Low (<", comment: " ") +
  242. (numberFormatter.string(from: state.settingsManager.preferences.low as NSNumber) ?? "") + ")"
  243. ).font(.caption2).foregroundColor(.secondary)
  244. Text(
  245. (
  246. tirFormatter
  247. .string(from: (
  248. state.statistics?.Statistics.Distribution.Hypos.day ?? 0
  249. ) as NSNumber) ??
  250. "0"
  251. ) + " %"
  252. ).font(.system(size: 12)).foregroundColor(.loopRed)
  253. Text("Normal (24h)").font(.caption2).foregroundColor(.secondary)
  254. Text(
  255. (
  256. tirFormatter
  257. .string(from: (state.statistics?.Statistics.Distribution.TIR.day ?? 0) as NSNumber) ??
  258. "0"
  259. ) + " %"
  260. ).font(.system(size: 12)).foregroundColor(.loopGreen)
  261. Text(
  262. NSLocalizedString("High (>", comment: " ") +
  263. (numberFormatter.string(from: state.settingsManager.preferences.high as NSNumber) ?? "") + ")"
  264. ).font(.caption2).foregroundColor(.secondary)
  265. Text(
  266. (
  267. tirFormatter
  268. .string(from: (
  269. state.statistics?.Statistics.Distribution.Hypers.day ?? 0
  270. ) as NSNumber) ??
  271. "0"
  272. ) + " %"
  273. ).font(.system(size: 12)).foregroundColor(.loopYellow)
  274. }
  275. }
  276. HStack {
  277. Group {
  278. Text("HbA1c (24h)").font(.caption2).foregroundColor(.secondary)
  279. Text(
  280. numberFormatter
  281. .string(from: (state.statistics?.Statistics.HbA1c.day ?? 0) as NSNumber) ??
  282. ""
  283. ).font(.system(size: 12))
  284. Text(
  285. NSLocalizedString("All ", comment: "") +
  286. (
  287. numberFormatter
  288. .string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? ""
  289. ) +
  290. NSLocalizedString(" days", comment: "")
  291. ).font(.caption2).foregroundColor(.secondary)
  292. Text(
  293. numberFormatter
  294. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ??
  295. ""
  296. ).font(.system(size: 12))
  297. if !state.settingsManager.preferences.displaySD {
  298. Text(
  299. NSLocalizedString("CV (%)", comment: "CV")
  300. ).font(.caption2).foregroundColor(.secondary)
  301. Text(
  302. numberFormatter
  303. .string(from: (state.statistics?.Statistics.Variance.CV.total ?? 0) as NSNumber) ??
  304. ""
  305. ).font(.system(size: 12))
  306. } else {
  307. Text(
  308. NSLocalizedString("SD (", comment: "SD") + state.settingsManager.settings.units.rawValue + ")"
  309. ).font(.caption2).foregroundColor(.secondary)
  310. Text(
  311. numberFormatter
  312. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ??
  313. ""
  314. ).font(.system(size: 12))
  315. }
  316. }
  317. }
  318. HStack {
  319. Group {
  320. Text("Loops").font(.caption2).foregroundColor(.secondary)
  321. Text(
  322. numberFormatter
  323. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ??
  324. "0"
  325. ).font(.system(size: 12))
  326. Text("Average Interval").font(.caption2).foregroundColor(.secondary)
  327. Text(
  328. numberFormatter
  329. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  330. "0"
  331. ).font(.system(size: 12))
  332. Text("Median Duration").font(.caption2).foregroundColor(.secondary)
  333. Text(
  334. numberFormatter
  335. .string(from: (
  336. state.statistics?.Statistics.LoopCycles
  337. .median_duration ?? 0
  338. ) as NSNumber) ??
  339. "0"
  340. ).font(.system(size: 12))
  341. }
  342. }
  343. }
  344. .frame(maxWidth: .infinity, maxHeight: 100, alignment: .center)
  345. }
  346. }
  347. var legendPanel: some View {
  348. HStack(alignment: .center) {
  349. Group {
  350. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  351. Text("BG")
  352. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  353. }
  354. Group {
  355. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  356. .padding(.leading, 8)
  357. Text("IOB")
  358. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  359. }
  360. Group {
  361. Circle().fill(Color.zt).frame(width: 8, height: 8)
  362. .padding(.leading, 8)
  363. Text("ZT")
  364. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  365. }
  366. Group {
  367. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  368. .padding(.leading, 8)
  369. Text("COB")
  370. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  371. }
  372. Group {
  373. Circle().fill(Color.uam).frame(width: 8, height: 8)
  374. .padding(.leading, 8)
  375. Text("UAM")
  376. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  377. }
  378. if let eventualBG = state.eventualBG {
  379. Text(
  380. "⇢ " + numberFormatter.string(
  381. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  382. )!
  383. )
  384. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  385. }
  386. }
  387. .frame(maxWidth: .infinity, maxHeight: 30)
  388. }
  389. var mainChart: some View {
  390. ZStack {
  391. if state.animatedBackground {
  392. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  393. .ignoresSafeArea()
  394. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  395. }
  396. MainChartView(
  397. glucose: $state.glucose,
  398. suggestion: $state.suggestion,
  399. statistcs: $state.statistics,
  400. tempBasals: $state.tempBasals,
  401. boluses: $state.boluses,
  402. suspensions: $state.suspensions,
  403. hours: .constant(state.filteredHours),
  404. maxBasal: $state.maxBasal,
  405. autotunedBasalProfile: $state.autotunedBasalProfile,
  406. basalProfile: $state.basalProfile,
  407. tempTargets: $state.tempTargets,
  408. carbs: $state.carbs,
  409. timerDate: $state.timerDate,
  410. units: $state.units
  411. )
  412. }
  413. .padding(.bottom)
  414. .modal(for: .dataTable, from: self)
  415. }
  416. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  417. ZStack {
  418. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  419. HStack {
  420. Button { state.showModal(for: .addCarbs) }
  421. label: {
  422. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  423. Image("carbs")
  424. .renderingMode(.template)
  425. .resizable()
  426. .frame(width: 24, height: 24)
  427. .foregroundColor(.loopGreen)
  428. .padding(8)
  429. if let carbsReq = state.carbsRequired {
  430. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  431. .font(.caption)
  432. .foregroundColor(.white)
  433. .padding(4)
  434. .background(Capsule().fill(Color.red))
  435. }
  436. }
  437. }
  438. Spacer()
  439. Button { state.showModal(for: .addTempTarget) }
  440. label: {
  441. Image("target")
  442. .renderingMode(.template)
  443. .resizable()
  444. .frame(width: 24, height: 24)
  445. .padding(8)
  446. }.foregroundColor(.loopYellow)
  447. Spacer()
  448. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  449. label: {
  450. Image("bolus")
  451. .renderingMode(.template)
  452. .resizable()
  453. .frame(width: 24, height: 24)
  454. .padding(8)
  455. }.foregroundColor(.insulin)
  456. Spacer()
  457. if state.allowManualTemp {
  458. Button { state.showModal(for: .manualTempBasal) }
  459. label: {
  460. Image("bolus1")
  461. .renderingMode(.template)
  462. .resizable()
  463. .frame(width: 24, height: 24)
  464. .padding(8)
  465. }.foregroundColor(.insulin)
  466. Spacer()
  467. }
  468. Button { state.showModal(for: .settings) }
  469. label: {
  470. Image("settings1")
  471. .renderingMode(.template)
  472. .resizable()
  473. .frame(width: 24, height: 24)
  474. .padding(8)
  475. }.foregroundColor(.loopGray)
  476. }
  477. .padding(.horizontal, 24)
  478. .padding(.bottom, geo.safeAreaInsets.bottom)
  479. }
  480. }
  481. var body: some View {
  482. GeometryReader { geo in
  483. VStack(spacing: 0) {
  484. header(geo)
  485. infoPanel
  486. mainChart
  487. legendPanel
  488. statPanel()
  489. bottomPanel(geo)
  490. }
  491. .edgesIgnoringSafeArea(.vertical)
  492. }
  493. .onAppear(perform: configureView)
  494. .navigationTitle("Home")
  495. .navigationBarHidden(true)
  496. .ignoresSafeArea(.keyboard)
  497. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  498. popup
  499. .padding()
  500. .background(
  501. RoundedRectangle(cornerRadius: 8, style: .continuous)
  502. .fill(Color(UIColor.darkGray))
  503. )
  504. .onTapGesture {
  505. isStatusPopupPresented = false
  506. }
  507. .gesture(
  508. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  509. .onEnded { value in
  510. if value.translation.height < 0 {
  511. isStatusPopupPresented = false
  512. }
  513. }
  514. )
  515. }
  516. }
  517. private var popup: some View {
  518. VStack(alignment: .leading, spacing: 4) {
  519. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  520. .padding(.bottom, 4)
  521. if let suggestion = state.suggestion {
  522. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  523. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  524. } else {
  525. Text("No sugestion found").font(.body).foregroundColor(.white)
  526. }
  527. if let errorMessage = state.errorMessage, let date = state.errorDate {
  528. Text("Error at \(dateFormatter.string(from: date))")
  529. .foregroundColor(.white)
  530. .font(.headline)
  531. .padding(.bottom, 4)
  532. .padding(.top, 8)
  533. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  534. }
  535. }
  536. }
  537. private func colorOfGlucose(_ glucose: Decimal) -> Color {
  538. switch glucose {
  539. case 4 ... 8,
  540. 30 ... 46,
  541. 72 ... 144:
  542. return .loopGreen
  543. case 0 ... 4,
  544. 20 ... 71:
  545. return .loopRed
  546. default:
  547. return .loopYellow
  548. }
  549. }
  550. }
  551. }