HomeRootView.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. import CoreData
  2. import SpriteKit
  3. import SwiftDate
  4. import SwiftUI
  5. import Swinject
  6. extension Home {
  7. struct RootView: BaseView {
  8. let resolver: Resolver
  9. @StateObject var state = StateModel()
  10. @State var isStatusPopupPresented = false
  11. @State var selectedState: durationState
  12. // Average/Median/Readings and CV/SD titles and values switches when you tap them
  13. @State var averageOrMedianTitle = NSLocalizedString("Average", comment: "")
  14. @State var median_ = ""
  15. @State var average_ = ""
  16. @State var readings = ""
  17. @State var averageOrmedian = ""
  18. @State var CV_or_SD_Title = NSLocalizedString("CV", comment: "CV")
  19. @State var cv_ = ""
  20. @State var sd_ = ""
  21. @State var CVorSD = ""
  22. // Switch between Loops and Errors when tapping in statPanel
  23. @State var loopStatTitle = NSLocalizedString("Loops", comment: "Nr of Loops in statPanel")
  24. @FetchRequest(
  25. entity: Override.entity(),
  26. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  27. ) var fetchedPercent: FetchedResults<Override>
  28. private var numberFormatter: NumberFormatter {
  29. let formatter = NumberFormatter()
  30. formatter.numberStyle = .decimal
  31. formatter.maximumFractionDigits = 2
  32. return formatter
  33. }
  34. private var targetFormatter: NumberFormatter {
  35. let formatter = NumberFormatter()
  36. formatter.numberStyle = .decimal
  37. formatter.maximumFractionDigits = 1
  38. return formatter
  39. }
  40. private var tirFormatter: NumberFormatter {
  41. let formatter = NumberFormatter()
  42. formatter.numberStyle = .decimal
  43. formatter.maximumFractionDigits = 0
  44. return formatter
  45. }
  46. private var dateFormatter: DateFormatter {
  47. let dateFormatter = DateFormatter()
  48. dateFormatter.timeStyle = .short
  49. return dateFormatter
  50. }
  51. private var spriteScene: SKScene {
  52. let scene = SnowScene()
  53. scene.scaleMode = .resizeFill
  54. scene.backgroundColor = .clear
  55. return scene
  56. }
  57. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  58. HStack(alignment: .bottom) {
  59. Spacer()
  60. cobIobView
  61. Spacer()
  62. glucoseView
  63. Spacer()
  64. pumpView
  65. Spacer()
  66. loopView
  67. Spacer()
  68. }
  69. .frame(maxWidth: .infinity)
  70. .padding(.top, geo.safeAreaInsets.top)
  71. .padding(.bottom, 6)
  72. .background(Color.gray.opacity(0.2))
  73. }
  74. var cobIobView: some View {
  75. VStack(alignment: .leading, spacing: 12) {
  76. HStack {
  77. Text("IOB").font(.footnote).foregroundColor(.secondary)
  78. Text(
  79. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  80. NSLocalizedString(" U", comment: "Insulin unit")
  81. )
  82. .font(.footnote).fontWeight(.bold)
  83. }.frame(alignment: .top)
  84. HStack {
  85. Text("COB").font(.footnote).foregroundColor(.secondary)
  86. Text(
  87. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  88. NSLocalizedString(" g", comment: "gram of carbs")
  89. )
  90. .font(.footnote).fontWeight(.bold)
  91. }.frame(alignment: .bottom)
  92. }
  93. }
  94. var glucoseView: some View {
  95. CurrentGlucoseView(
  96. recentGlucose: $state.recentGlucose,
  97. delta: $state.glucoseDelta,
  98. units: $state.units,
  99. alarm: $state.alarm
  100. )
  101. .onTapGesture {
  102. if state.alarm == nil {
  103. state.openCGM()
  104. } else {
  105. state.showModal(for: .snooze)
  106. }
  107. }
  108. .onLongPressGesture {
  109. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  110. impactHeavy.impactOccurred()
  111. if state.alarm == nil {
  112. state.showModal(for: .snooze)
  113. } else {
  114. state.openCGM()
  115. }
  116. }
  117. }
  118. var pumpView: some View {
  119. PumpView(
  120. reservoir: $state.reservoir,
  121. battery: $state.battery,
  122. name: $state.pumpName,
  123. expiresAtDate: $state.pumpExpiresAtDate,
  124. timerDate: $state.timerDate
  125. )
  126. .onTapGesture {
  127. if state.pumpDisplayState != nil {
  128. state.setupPump = true
  129. }
  130. }
  131. }
  132. var loopView: some View {
  133. LoopView(
  134. suggestion: $state.suggestion,
  135. enactedSuggestion: $state.enactedSuggestion,
  136. closedLoop: $state.closedLoop,
  137. timerDate: $state.timerDate,
  138. isLooping: $state.isLooping,
  139. lastLoopDate: $state.lastLoopDate,
  140. manualTempBasal: $state.manualTempBasal
  141. ).onTapGesture {
  142. isStatusPopupPresented = true
  143. }.onLongPressGesture {
  144. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  145. impactHeavy.impactOccurred()
  146. state.runLoop()
  147. }
  148. }
  149. var infoPanel: some View {
  150. HStack(alignment: .center) {
  151. if state.pumpSuspended {
  152. Text("Pump suspended")
  153. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  154. .padding(.leading, 8)
  155. } else if let tempRate = state.tempRate {
  156. if state.apsManager.isManualTempBasal {
  157. Text(
  158. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  159. NSLocalizedString(" U/hr", comment: "Unit per hour with space") +
  160. NSLocalizedString(" - Manual Basal ⚠️", comment: "Manual Temp basal")
  161. )
  162. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  163. .padding(.leading, 8)
  164. } else {
  165. Text(
  166. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  167. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  168. )
  169. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  170. .padding(.leading, 8)
  171. }
  172. }
  173. if let tempTarget = state.tempTarget {
  174. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  175. if state.units == .mmolL {
  176. Text(
  177. targetFormatter
  178. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  179. )
  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?.asMmolL ?? 0) as NSNumber)! +
  188. " \(state.units.rawValue)"
  189. )
  190. .font(.caption)
  191. .foregroundColor(.secondary)
  192. } else {
  193. Text(state.units.rawValue).font(.caption)
  194. .foregroundColor(.secondary)
  195. }
  196. } else {
  197. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  198. .font(.caption)
  199. .foregroundColor(.secondary)
  200. if tempTarget.targetBottom != tempTarget.targetTop {
  201. Text("-").font(.caption)
  202. .foregroundColor(.secondary)
  203. Text(
  204. targetFormatter
  205. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  206. )
  207. .font(.caption)
  208. .foregroundColor(.secondary)
  209. } else {
  210. Text(state.units.rawValue).font(.caption)
  211. .foregroundColor(.secondary)
  212. }
  213. }
  214. }
  215. Spacer()
  216. Text(
  217. (fetchedPercent.first?.enabled ?? false) ?
  218. "\((fetchedPercent.first?.percentage ?? 100).formatted(.number)) % " : ""
  219. )
  220. .font(.system(size: 12, weight: .bold))
  221. .foregroundColor(.orange)
  222. .padding(.trailing, 2)
  223. if fetchedPercent.first?.enabled ?? false {
  224. Text(
  225. (tirFormatter.string(from: (fetchedPercent.first?.duration ?? 0) as NSNumber) ?? "") == "0" ?
  226. "Perpetual" :
  227. (tirFormatter.string(from: (fetchedPercent.first?.duration ?? 0) as NSNumber) ?? "") + " min"
  228. )
  229. .font(.system(size: 12))
  230. .foregroundColor(.orange)
  231. .padding(.trailing, 8)
  232. }
  233. if let progress = state.bolusProgress {
  234. Text("Bolusing")
  235. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  236. ProgressView(value: Double(progress))
  237. .progressViewStyle(BolusProgressViewStyle())
  238. .padding(.trailing, 8)
  239. .onTapGesture {
  240. state.cancelBolus()
  241. }
  242. }
  243. }
  244. .frame(maxWidth: .infinity, maxHeight: 30)
  245. }
  246. @ViewBuilder private func statPanel() -> some View {
  247. if state.displayStatistics {
  248. VStack(spacing: 8) {
  249. durationButton(states: durationState.allCases, selectedState: $selectedState)
  250. switch selectedState {
  251. case .day:
  252. let hba1c_all = numberFormatter
  253. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  254. let average_ = targetFormatter
  255. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ?? ""
  256. let median_ = targetFormatter
  257. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ?? ""
  258. let tir_low = tirFormatter
  259. .string(from: (state.statistics?.Statistics.Distribution.Hypos.day ?? 0) as NSNumber) ?? ""
  260. let tir_high = tirFormatter
  261. .string(from: (state.statistics?.Statistics.Distribution.Hypers.day ?? 0) as NSNumber) ?? ""
  262. let tir_ = tirFormatter
  263. .string(from: (state.statistics?.Statistics.Distribution.TIR.day ?? 0) as NSNumber) ?? ""
  264. let hba1c_ = numberFormatter
  265. .string(from: (state.statistics?.Statistics.HbA1c.day ?? 0) as NSNumber) ?? ""
  266. let sd_ = numberFormatter
  267. .string(from: (state.statistics?.Statistics.Variance.SD.day ?? 0) as NSNumber) ?? ""
  268. let cv_ = tirFormatter
  269. .string(from: (state.statistics?.Statistics.Variance.CV.day ?? 0) as NSNumber) ?? ""
  270. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  271. case .week:
  272. let hba1c_all = numberFormatter
  273. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  274. let average_ = targetFormatter
  275. .string(from: (state.statistics?.Statistics.Glucose.Average.week ?? 0) as NSNumber) ?? ""
  276. let median_ = targetFormatter
  277. .string(from: (state.statistics?.Statistics.Glucose.Median.week ?? 0) as NSNumber) ?? ""
  278. let tir_low = tirFormatter
  279. .string(from: (state.statistics?.Statistics.Distribution.Hypos.week ?? 0) as NSNumber) ?? ""
  280. let tir_high = tirFormatter
  281. .string(from: (state.statistics?.Statistics.Distribution.Hypers.week ?? 0) as NSNumber) ?? ""
  282. let tir_ = tirFormatter
  283. .string(from: (state.statistics?.Statistics.Distribution.TIR.week ?? 0) as NSNumber) ?? ""
  284. let hba1c_ = numberFormatter
  285. .string(from: (state.statistics?.Statistics.HbA1c.week ?? 0) as NSNumber) ?? ""
  286. let sd_ = numberFormatter
  287. .string(from: (state.statistics?.Statistics.Variance.SD.week ?? 0) as NSNumber) ?? ""
  288. let cv_ = tirFormatter
  289. .string(from: (state.statistics?.Statistics.Variance.CV.week ?? 0) as NSNumber) ?? ""
  290. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  291. case .month:
  292. let hba1c_all = numberFormatter
  293. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  294. let average_ = targetFormatter
  295. .string(from: (state.statistics?.Statistics.Glucose.Average.month ?? 0) as NSNumber) ?? ""
  296. let median_ = targetFormatter
  297. .string(from: (state.statistics?.Statistics.Glucose.Median.month ?? 0) as NSNumber) ?? ""
  298. let tir_low = tirFormatter
  299. .string(from: (state.statistics?.Statistics.Distribution.Hypos.month ?? 0) as NSNumber) ?? ""
  300. let tir_high = tirFormatter
  301. .string(from: (state.statistics?.Statistics.Distribution.Hypers.month ?? 0) as NSNumber) ?? ""
  302. let tir_ = tirFormatter
  303. .string(from: (state.statistics?.Statistics.Distribution.TIR.month ?? 0) as NSNumber) ?? ""
  304. let hba1c_ = numberFormatter
  305. .string(from: (state.statistics?.Statistics.HbA1c.month ?? 0) as NSNumber) ?? ""
  306. let sd_ = numberFormatter
  307. .string(from: (state.statistics?.Statistics.Variance.SD.month ?? 0) as NSNumber) ?? ""
  308. let cv_ = tirFormatter
  309. .string(from: (state.statistics?.Statistics.Variance.CV.month ?? 0) as NSNumber) ?? ""
  310. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  311. case .total:
  312. let hba1c_all = numberFormatter
  313. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  314. let average_ = targetFormatter
  315. .string(from: (state.statistics?.Statistics.Glucose.Average.total ?? 0) as NSNumber) ?? ""
  316. let median_ = targetFormatter
  317. .string(from: (state.statistics?.Statistics.Glucose.Median.total ?? 0) as NSNumber) ?? ""
  318. let tir_low = tirFormatter
  319. .string(from: (state.statistics?.Statistics.Distribution.Hypos.total ?? 0) as NSNumber) ?? ""
  320. let tir_high = tirFormatter
  321. .string(from: (state.statistics?.Statistics.Distribution.Hypers.total ?? 0) as NSNumber) ??
  322. ""
  323. let tir_ = tirFormatter
  324. .string(from: (state.statistics?.Statistics.Distribution.TIR.total ?? 0) as NSNumber) ?? ""
  325. let hba1c_ = numberFormatter
  326. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  327. let sd_ = numberFormatter
  328. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ?? ""
  329. let cv_ = tirFormatter
  330. .string(from: (state.statistics?.Statistics.Variance.CV.total ?? 0) as NSNumber) ?? ""
  331. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  332. }
  333. }
  334. .frame(maxWidth: .infinity)
  335. .padding([.bottom], 20)
  336. }
  337. }
  338. @ViewBuilder private func averageTIRhca1c(
  339. _ hba1c_all: String,
  340. _ average_: String,
  341. _ median_: String,
  342. _ tir_low: String,
  343. _ tir_high: String,
  344. _ tir_: String,
  345. _ hba1c_: String,
  346. _ sd_: String,
  347. _ cv_: String
  348. ) -> some View {
  349. HStack {
  350. Group {
  351. if selectedState != .total {
  352. HStack {
  353. Text("HbA1c").font(.footnote).foregroundColor(.secondary)
  354. Text(hba1c_).font(.footnote)
  355. }
  356. } else {
  357. HStack {
  358. Text(
  359. "\(NSLocalizedString("HbA1c", comment: "")) (\(targetFormatter.string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? "") \(NSLocalizedString("days", comment: "")))"
  360. )
  361. .font(.footnote).foregroundColor(.secondary)
  362. Text(hba1c_all).font(.footnote)
  363. }
  364. }
  365. // Average as default. Changes to Median when clicking.
  366. let textAverageTitle = NSLocalizedString("Average", comment: "")
  367. let textMedianTitle = NSLocalizedString("Median", comment: "")
  368. let cgmReadingsTitle = NSLocalizedString("Readings", comment: "CGM readings in statPanel")
  369. HStack {
  370. Text(averageOrMedianTitle).font(.footnote).foregroundColor(.secondary)
  371. if averageOrMedianTitle == textAverageTitle {
  372. Text(averageOrmedian == "" ? average_ : average_).font(.footnote)
  373. } else if averageOrMedianTitle == textMedianTitle {
  374. Text(averageOrmedian == "" ? median_ : median_).font(.footnote)
  375. } else if averageOrMedianTitle == cgmReadingsTitle {
  376. Text(
  377. averageOrmedian != "0" ? tirFormatter
  378. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? "" : ""
  379. )
  380. .font(.footnote)
  381. }
  382. }.onTapGesture {
  383. if averageOrMedianTitle == textAverageTitle {
  384. averageOrMedianTitle = textMedianTitle
  385. averageOrmedian = median_
  386. } else if averageOrMedianTitle == textMedianTitle {
  387. averageOrMedianTitle = cgmReadingsTitle
  388. averageOrmedian = tirFormatter
  389. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? ""
  390. } else if averageOrMedianTitle == cgmReadingsTitle {
  391. averageOrMedianTitle = textAverageTitle
  392. averageOrmedian = average_
  393. }
  394. }
  395. .frame(minWidth: 110)
  396. // CV as default. Changes to SD when clicking
  397. let text_CV_Title = NSLocalizedString("CV", comment: "")
  398. let text_SD_Title = NSLocalizedString("SD", comment: "")
  399. HStack {
  400. Text(CV_or_SD_Title).font(.footnote).foregroundColor(.secondary)
  401. if CV_or_SD_Title == text_CV_Title {
  402. Text(CVorSD == "" ? cv_ : cv_).font(.footnote)
  403. } else {
  404. Text(CVorSD == "" ? sd_ : sd_).font(.footnote)
  405. }
  406. }.onTapGesture {
  407. if CV_or_SD_Title == text_CV_Title {
  408. CV_or_SD_Title = text_SD_Title
  409. CVorSD = sd_
  410. } else {
  411. CV_or_SD_Title = text_CV_Title
  412. CVorSD = cv_
  413. }
  414. }
  415. }
  416. }
  417. HStack {
  418. Group {
  419. HStack {
  420. Text(
  421. NSLocalizedString("Low", comment: " ")
  422. )
  423. .font(.footnote)
  424. .foregroundColor(.secondary)
  425. Text(tir_low + " %").font(.footnote).foregroundColor(.loopRed)
  426. }
  427. HStack {
  428. Text("Normal").font(.footnote).foregroundColor(.secondary)
  429. Text(tir_ + " %").font(.footnote).foregroundColor(.loopGreen)
  430. }
  431. HStack {
  432. Text(
  433. NSLocalizedString("High", comment: " ")
  434. )
  435. .font(.footnote).foregroundColor(.secondary)
  436. Text(tir_high + " %").font(.footnote).foregroundColor(.loopYellow)
  437. }
  438. }
  439. }
  440. if state.settingsManager.preferences.displayLoops {
  441. HStack {
  442. Group {
  443. let loopTitle = NSLocalizedString("Loops", comment: "Nr of Loops in statPanel")
  444. let errorTitle = NSLocalizedString("Errors", comment: "Loop Errors in statPanel")
  445. HStack {
  446. Text(loopStatTitle).font(.footnote).foregroundColor(.secondary)
  447. Text(
  448. loopStatTitle == loopTitle ? tirFormatter
  449. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ?? "" :
  450. tirFormatter
  451. .string(from: (state.statistics?.Statistics.LoopCycles.errors ?? 0) as NSNumber) ?? ""
  452. ).font(.footnote)
  453. }.onTapGesture {
  454. if loopStatTitle == loopTitle {
  455. loopStatTitle = errorTitle
  456. } else if loopStatTitle == errorTitle {
  457. loopStatTitle = loopTitle
  458. }
  459. }
  460. HStack {
  461. Text("Interval").font(.footnote)
  462. .foregroundColor(.secondary)
  463. Text(
  464. targetFormatter
  465. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  466. ""
  467. ).font(.footnote)
  468. }
  469. HStack {
  470. Text("Duration").font(.footnote)
  471. .foregroundColor(.secondary)
  472. Text(
  473. numberFormatter
  474. .string(
  475. from: (state.statistics?.Statistics.LoopCycles.median_duration ?? 0) as NSNumber
  476. ) ?? ""
  477. ).font(.footnote)
  478. }
  479. }
  480. }
  481. }
  482. }
  483. var legendPanel: some View {
  484. ZStack {
  485. HStack(alignment: .center) {
  486. Group {
  487. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  488. Text("BG")
  489. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  490. }
  491. Group {
  492. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  493. .padding(.leading, 8)
  494. Text("IOB")
  495. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  496. }
  497. Group {
  498. Circle().fill(Color.zt).frame(width: 8, height: 8)
  499. .padding(.leading, 8)
  500. Text("ZT")
  501. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  502. }
  503. Group {
  504. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  505. .padding(.leading, 8)
  506. Text("COB")
  507. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  508. }
  509. Group {
  510. Circle().fill(Color.uam).frame(width: 8, height: 8)
  511. .padding(.leading, 8)
  512. Text("UAM")
  513. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  514. }
  515. if let eventualBG = state.eventualBG {
  516. Text(
  517. "⇢ " + numberFormatter.string(
  518. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  519. )!
  520. )
  521. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  522. }
  523. }
  524. .frame(maxWidth: .infinity)
  525. .padding([.bottom], 20)
  526. }
  527. }
  528. var mainChart: some View {
  529. ZStack {
  530. if state.animatedBackground {
  531. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  532. .ignoresSafeArea()
  533. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  534. }
  535. MainChartView(
  536. glucose: $state.glucose,
  537. suggestion: $state.suggestion,
  538. statistcs: $state.statistics,
  539. tempBasals: $state.tempBasals,
  540. boluses: $state.boluses,
  541. suspensions: $state.suspensions,
  542. hours: .constant(state.filteredHours),
  543. maxBasal: $state.maxBasal,
  544. autotunedBasalProfile: $state.autotunedBasalProfile,
  545. basalProfile: $state.basalProfile,
  546. tempTargets: $state.tempTargets,
  547. carbs: $state.carbs,
  548. timerDate: $state.timerDate,
  549. units: $state.units,
  550. smooth: $state.smooth
  551. )
  552. }
  553. .padding(.bottom)
  554. .modal(for: .dataTable, from: self)
  555. }
  556. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  557. ZStack {
  558. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  559. HStack {
  560. Button { state.showModal(for: .addCarbs) }
  561. label: {
  562. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  563. Image("carbs")
  564. .renderingMode(.template)
  565. .resizable()
  566. .frame(width: 24, height: 24)
  567. .foregroundColor(.loopYellow)
  568. .padding(8)
  569. if let carbsReq = state.carbsRequired {
  570. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  571. .font(.caption)
  572. .foregroundColor(.white)
  573. .padding(4)
  574. .background(Capsule().fill(Color.red))
  575. }
  576. }
  577. }
  578. Spacer()
  579. Button { state.showModal(for: .addTempTarget) }
  580. label: {
  581. Image("target")
  582. .renderingMode(.template)
  583. .resizable()
  584. .frame(width: 24, height: 24)
  585. .padding(8)
  586. }.foregroundColor(.loopGreen)
  587. Spacer()
  588. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  589. label: {
  590. Image("bolus")
  591. .renderingMode(.template)
  592. .resizable()
  593. .frame(width: 24, height: 24)
  594. .padding(8)
  595. }.foregroundColor(.insulin)
  596. Spacer()
  597. if state.allowManualTemp {
  598. Button { state.showModal(for: .manualTempBasal) }
  599. label: {
  600. Image("bolus1")
  601. .renderingMode(.template)
  602. .resizable()
  603. .frame(width: 24, height: 24)
  604. .padding(8)
  605. }.foregroundColor(.insulin)
  606. Spacer()
  607. }
  608. Button { state.showModal(for: .settings) }
  609. label: {
  610. Image("settings1")
  611. .renderingMode(.template)
  612. .resizable()
  613. .frame(width: 24, height: 24)
  614. .padding(8)
  615. }.foregroundColor(.loopGray)
  616. }
  617. .padding(.horizontal, 24)
  618. .padding(.bottom, geo.safeAreaInsets.bottom)
  619. }
  620. }
  621. var body: some View {
  622. GeometryReader { geo in
  623. VStack(spacing: 0) {
  624. header(geo)
  625. infoPanel
  626. mainChart
  627. legendPanel
  628. statPanel()
  629. bottomPanel(geo)
  630. }
  631. .edgesIgnoringSafeArea(.vertical)
  632. }
  633. .onAppear(perform: configureView)
  634. .navigationTitle("Home")
  635. .navigationBarHidden(true)
  636. .ignoresSafeArea(.keyboard)
  637. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  638. popup
  639. .padding()
  640. .background(
  641. RoundedRectangle(cornerRadius: 8, style: .continuous)
  642. .fill(Color(UIColor.darkGray))
  643. )
  644. .onTapGesture {
  645. isStatusPopupPresented = false
  646. }
  647. .gesture(
  648. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  649. .onEnded { value in
  650. if value.translation.height < 0 {
  651. isStatusPopupPresented = false
  652. }
  653. }
  654. )
  655. }
  656. }
  657. private var popup: some View {
  658. VStack(alignment: .leading, spacing: 4) {
  659. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  660. .padding(.bottom, 4)
  661. if let suggestion = state.suggestion {
  662. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  663. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  664. } else {
  665. Text("No sugestion found").font(.body).foregroundColor(.white)
  666. }
  667. if let errorMessage = state.errorMessage, let date = state.errorDate {
  668. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  669. .foregroundColor(.white)
  670. .font(.headline)
  671. .padding(.bottom, 4)
  672. .padding(.top, 8)
  673. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  674. }
  675. }
  676. }
  677. }
  678. }