HomeRootView.swift 33 KB

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