HomeRootView.swift 25 KB

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