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