HomeRootView.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 currentTempTarget: FetchedResults<TempTargets>
  32. @FetchRequest(
  33. entity: TempTargetsSlider.entity(),
  34. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  35. ) var currentEnactedTempTarget: 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 currentTempTarget.first?.active ?? false {
  185. let hbt = currentTempTarget.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 currentEnactedTempTarget.first?.enabled ?? false {
  196. let hbt = currentEnactedTempTarget.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. (tirFormatter.string(from: (fetchedPercent.first?.duration ?? 0) as NSNumber) ?? "") + " min"
  221. )
  222. )
  223. .font(.system(size: 12))
  224. .foregroundColor(.orange)
  225. .padding(.trailing, 8)
  226. }
  227. if let progress = state.bolusProgress {
  228. Text("Bolusing")
  229. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  230. ProgressView(value: Double(progress))
  231. .progressViewStyle(BolusProgressViewStyle())
  232. .padding(.trailing, 8)
  233. .onTapGesture {
  234. state.cancelBolus()
  235. }
  236. }
  237. }
  238. .frame(maxWidth: .infinity, maxHeight: 30)
  239. }
  240. @ViewBuilder private func statPanel() -> some View {
  241. if state.displayStatistics {
  242. VStack(spacing: 8) {
  243. durationButton(states: durationState.allCases, selectedState: $selectedState)
  244. switch selectedState {
  245. case .day:
  246. let hba1c_all = numberFormatter
  247. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  248. let average_ = targetFormatter
  249. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ?? ""
  250. let median_ = targetFormatter
  251. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ?? ""
  252. let tir_low = tirFormatter
  253. .string(from: (state.statistics?.Statistics.Distribution.Hypos.day ?? 0) as NSNumber) ?? ""
  254. let tir_high = tirFormatter
  255. .string(from: (state.statistics?.Statistics.Distribution.Hypers.day ?? 0) as NSNumber) ?? ""
  256. let tir_ = tirFormatter
  257. .string(from: (state.statistics?.Statistics.Distribution.TIR.day ?? 0) as NSNumber) ?? ""
  258. let hba1c_ = numberFormatter
  259. .string(from: (state.statistics?.Statistics.HbA1c.day ?? 0) as NSNumber) ?? ""
  260. let sd_ = numberFormatter
  261. .string(from: (state.statistics?.Statistics.Variance.SD.day ?? 0) as NSNumber) ?? ""
  262. let cv_ = tirFormatter
  263. .string(from: (state.statistics?.Statistics.Variance.CV.day ?? 0) as NSNumber) ?? ""
  264. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  265. case .week:
  266. let hba1c_all = numberFormatter
  267. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  268. let average_ = targetFormatter
  269. .string(from: (state.statistics?.Statistics.Glucose.Average.week ?? 0) as NSNumber) ?? ""
  270. let median_ = targetFormatter
  271. .string(from: (state.statistics?.Statistics.Glucose.Median.week ?? 0) as NSNumber) ?? ""
  272. let tir_low = tirFormatter
  273. .string(from: (state.statistics?.Statistics.Distribution.Hypos.week ?? 0) as NSNumber) ?? ""
  274. let tir_high = tirFormatter
  275. .string(from: (state.statistics?.Statistics.Distribution.Hypers.week ?? 0) as NSNumber) ?? ""
  276. let tir_ = tirFormatter
  277. .string(from: (state.statistics?.Statistics.Distribution.TIR.week ?? 0) as NSNumber) ?? ""
  278. let hba1c_ = numberFormatter
  279. .string(from: (state.statistics?.Statistics.HbA1c.week ?? 0) as NSNumber) ?? ""
  280. let sd_ = numberFormatter
  281. .string(from: (state.statistics?.Statistics.Variance.SD.week ?? 0) as NSNumber) ?? ""
  282. let cv_ = tirFormatter
  283. .string(from: (state.statistics?.Statistics.Variance.CV.week ?? 0) as NSNumber) ?? ""
  284. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  285. case .month:
  286. let hba1c_all = numberFormatter
  287. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  288. let average_ = targetFormatter
  289. .string(from: (state.statistics?.Statistics.Glucose.Average.month ?? 0) as NSNumber) ?? ""
  290. let median_ = targetFormatter
  291. .string(from: (state.statistics?.Statistics.Glucose.Median.month ?? 0) as NSNumber) ?? ""
  292. let tir_low = tirFormatter
  293. .string(from: (state.statistics?.Statistics.Distribution.Hypos.month ?? 0) as NSNumber) ?? ""
  294. let tir_high = tirFormatter
  295. .string(from: (state.statistics?.Statistics.Distribution.Hypers.month ?? 0) as NSNumber) ?? ""
  296. let tir_ = tirFormatter
  297. .string(from: (state.statistics?.Statistics.Distribution.TIR.month ?? 0) as NSNumber) ?? ""
  298. let hba1c_ = numberFormatter
  299. .string(from: (state.statistics?.Statistics.HbA1c.month ?? 0) as NSNumber) ?? ""
  300. let sd_ = numberFormatter
  301. .string(from: (state.statistics?.Statistics.Variance.SD.month ?? 0) as NSNumber) ?? ""
  302. let cv_ = tirFormatter
  303. .string(from: (state.statistics?.Statistics.Variance.CV.month ?? 0) as NSNumber) ?? ""
  304. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  305. case .total:
  306. let hba1c_all = numberFormatter
  307. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  308. let average_ = targetFormatter
  309. .string(from: (state.statistics?.Statistics.Glucose.Average.total ?? 0) as NSNumber) ?? ""
  310. let median_ = targetFormatter
  311. .string(from: (state.statistics?.Statistics.Glucose.Median.total ?? 0) as NSNumber) ?? ""
  312. let tir_low = tirFormatter
  313. .string(from: (state.statistics?.Statistics.Distribution.Hypos.total ?? 0) as NSNumber) ?? ""
  314. let tir_high = tirFormatter
  315. .string(from: (state.statistics?.Statistics.Distribution.Hypers.total ?? 0) as NSNumber) ??
  316. ""
  317. let tir_ = tirFormatter
  318. .string(from: (state.statistics?.Statistics.Distribution.TIR.total ?? 0) as NSNumber) ?? ""
  319. let hba1c_ = numberFormatter
  320. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  321. let sd_ = numberFormatter
  322. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ?? ""
  323. let cv_ = tirFormatter
  324. .string(from: (state.statistics?.Statistics.Variance.CV.total ?? 0) as NSNumber) ?? ""
  325. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  326. }
  327. }
  328. .frame(maxWidth: .infinity)
  329. .padding([.bottom], 20)
  330. }
  331. }
  332. @ViewBuilder private func averageTIRhca1c(
  333. _ hba1c_all: String,
  334. _ average_: String,
  335. _ median_: String,
  336. _ tir_low: String,
  337. _ tir_high: String,
  338. _ tir_: String,
  339. _ hba1c_: String,
  340. _ sd_: String,
  341. _ cv_: String
  342. ) -> some View {
  343. HStack {
  344. Group {
  345. if selectedState != .total {
  346. HStack {
  347. Text("HbA1c").font(.footnote).foregroundColor(.secondary)
  348. Text(hba1c_).font(.footnote)
  349. }
  350. } else {
  351. HStack {
  352. Text(
  353. "\(NSLocalizedString("HbA1c", comment: "")) (\(targetFormatter.string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? "") \(NSLocalizedString("days", comment: "")))"
  354. )
  355. .font(.footnote).foregroundColor(.secondary)
  356. Text(hba1c_all).font(.footnote)
  357. }
  358. }
  359. // Average as default. Changes to Median when clicking.
  360. let textAverageTitle = NSLocalizedString("Average", comment: "")
  361. let textMedianTitle = NSLocalizedString("Median", comment: "")
  362. let cgmReadingsTitle = NSLocalizedString("Readings", comment: "CGM readings in statPanel")
  363. HStack {
  364. Text(averageOrMedianTitle).font(.footnote).foregroundColor(.secondary)
  365. if averageOrMedianTitle == textAverageTitle {
  366. Text(averageOrmedian == "" ? average_ : average_).font(.footnote)
  367. } else if averageOrMedianTitle == textMedianTitle {
  368. Text(averageOrmedian == "" ? median_ : median_).font(.footnote)
  369. } else if averageOrMedianTitle == cgmReadingsTitle {
  370. Text(
  371. averageOrmedian != "0" ? tirFormatter
  372. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? "" : ""
  373. )
  374. .font(.footnote)
  375. }
  376. }.onTapGesture {
  377. if averageOrMedianTitle == textAverageTitle {
  378. averageOrMedianTitle = textMedianTitle
  379. averageOrmedian = median_
  380. } else if averageOrMedianTitle == textMedianTitle {
  381. averageOrMedianTitle = cgmReadingsTitle
  382. averageOrmedian = tirFormatter
  383. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? ""
  384. } else if averageOrMedianTitle == cgmReadingsTitle {
  385. averageOrMedianTitle = textAverageTitle
  386. averageOrmedian = average_
  387. }
  388. }
  389. .frame(minWidth: 110)
  390. // CV as default. Changes to SD when clicking
  391. let text_CV_Title = NSLocalizedString("CV", comment: "")
  392. let text_SD_Title = NSLocalizedString("SD", comment: "")
  393. HStack {
  394. Text(CV_or_SD_Title).font(.footnote).foregroundColor(.secondary)
  395. if CV_or_SD_Title == text_CV_Title {
  396. Text(CVorSD == "" ? cv_ : cv_).font(.footnote)
  397. } else {
  398. Text(CVorSD == "" ? sd_ : sd_).font(.footnote)
  399. }
  400. }.onTapGesture {
  401. if CV_or_SD_Title == text_CV_Title {
  402. CV_or_SD_Title = text_SD_Title
  403. CVorSD = sd_
  404. } else {
  405. CV_or_SD_Title = text_CV_Title
  406. CVorSD = cv_
  407. }
  408. }
  409. }
  410. }
  411. HStack {
  412. Group {
  413. HStack {
  414. Text(
  415. NSLocalizedString("Low", comment: " ")
  416. )
  417. .font(.footnote)
  418. .foregroundColor(.secondary)
  419. Text(tir_low + " %").font(.footnote).foregroundColor(.loopRed)
  420. }
  421. HStack {
  422. Text("Normal").font(.footnote).foregroundColor(.secondary)
  423. Text(tir_ + " %").font(.footnote).foregroundColor(.loopGreen)
  424. }
  425. HStack {
  426. Text(
  427. NSLocalizedString("High", comment: " ")
  428. )
  429. .font(.footnote).foregroundColor(.secondary)
  430. Text(tir_high + " %").font(.footnote).foregroundColor(.loopYellow)
  431. }
  432. }
  433. }
  434. if state.settingsManager.preferences.displayLoops {
  435. HStack {
  436. Group {
  437. let loopTitle = NSLocalizedString("Loops", comment: "Nr of Loops in statPanel")
  438. let errorTitle = NSLocalizedString("Errors", comment: "Loop Errors in statPanel")
  439. HStack {
  440. Text(loopStatTitle).font(.footnote).foregroundColor(.secondary)
  441. Text(
  442. loopStatTitle == loopTitle ? tirFormatter
  443. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ?? "" :
  444. tirFormatter
  445. .string(from: (state.statistics?.Statistics.LoopCycles.errors ?? 0) as NSNumber) ?? ""
  446. ).font(.footnote)
  447. }.onTapGesture {
  448. if loopStatTitle == loopTitle {
  449. loopStatTitle = errorTitle
  450. } else if loopStatTitle == errorTitle {
  451. loopStatTitle = loopTitle
  452. }
  453. }
  454. HStack {
  455. Text("Interval").font(.footnote)
  456. .foregroundColor(.secondary)
  457. Text(
  458. targetFormatter
  459. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  460. ""
  461. ).font(.footnote)
  462. }
  463. HStack {
  464. Text("Duration").font(.footnote)
  465. .foregroundColor(.secondary)
  466. Text(
  467. numberFormatter
  468. .string(
  469. from: (state.statistics?.Statistics.LoopCycles.median_duration ?? 0) as NSNumber
  470. ) ?? ""
  471. ).font(.footnote)
  472. }
  473. }
  474. }
  475. }
  476. }
  477. var legendPanel: some View {
  478. ZStack {
  479. HStack(alignment: .center) {
  480. Group {
  481. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  482. Text("BG")
  483. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  484. }
  485. Group {
  486. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  487. .padding(.leading, 8)
  488. Text("IOB")
  489. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  490. }
  491. Group {
  492. Circle().fill(Color.zt).frame(width: 8, height: 8)
  493. .padding(.leading, 8)
  494. Text("ZT")
  495. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  496. }
  497. Group {
  498. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  499. .padding(.leading, 8)
  500. Text("COB")
  501. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  502. }
  503. Group {
  504. Circle().fill(Color.uam).frame(width: 8, height: 8)
  505. .padding(.leading, 8)
  506. Text("UAM")
  507. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  508. }
  509. if let eventualBG = state.eventualBG {
  510. Text(
  511. "⇢ " + numberFormatter.string(
  512. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  513. )!
  514. )
  515. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  516. }
  517. }
  518. .frame(maxWidth: .infinity)
  519. .padding([.bottom], 20)
  520. }
  521. }
  522. var mainChart: some View {
  523. ZStack {
  524. if state.animatedBackground {
  525. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  526. .ignoresSafeArea()
  527. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  528. }
  529. MainChartView(
  530. glucose: $state.glucose,
  531. suggestion: $state.suggestion,
  532. statistcs: $state.statistics,
  533. tempBasals: $state.tempBasals,
  534. boluses: $state.boluses,
  535. suspensions: $state.suspensions,
  536. hours: .constant(state.filteredHours),
  537. maxBasal: $state.maxBasal,
  538. autotunedBasalProfile: $state.autotunedBasalProfile,
  539. basalProfile: $state.basalProfile,
  540. tempTargets: $state.tempTargets,
  541. carbs: $state.carbs,
  542. timerDate: $state.timerDate,
  543. units: $state.units,
  544. smooth: $state.smooth
  545. )
  546. }
  547. .padding(.bottom)
  548. .modal(for: .dataTable, from: self)
  549. }
  550. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  551. ZStack {
  552. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  553. HStack {
  554. Button { state.showModal(for: .addCarbs) }
  555. label: {
  556. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  557. Image("carbs")
  558. .renderingMode(.template)
  559. .resizable()
  560. .frame(width: 24, height: 24)
  561. .foregroundColor(.loopYellow)
  562. .padding(8)
  563. if let carbsReq = state.carbsRequired {
  564. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  565. .font(.caption)
  566. .foregroundColor(.white)
  567. .padding(4)
  568. .background(Capsule().fill(Color.red))
  569. }
  570. }
  571. }
  572. Spacer()
  573. Button { state.showModal(for: .addTempTarget) }
  574. label: {
  575. Image("target")
  576. .renderingMode(.template)
  577. .resizable()
  578. .frame(width: 24, height: 24)
  579. .padding(8)
  580. }.foregroundColor(.loopGreen)
  581. Spacer()
  582. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  583. label: {
  584. Image("bolus")
  585. .renderingMode(.template)
  586. .resizable()
  587. .frame(width: 24, height: 24)
  588. .padding(8)
  589. }.foregroundColor(.insulin)
  590. Spacer()
  591. if state.allowManualTemp {
  592. Button { state.showModal(for: .manualTempBasal) }
  593. label: {
  594. Image("bolus1")
  595. .renderingMode(.template)
  596. .resizable()
  597. .frame(width: 24, height: 24)
  598. .padding(8)
  599. }.foregroundColor(.insulin)
  600. Spacer()
  601. }
  602. Button { state.showModal(for: .settings) }
  603. label: {
  604. Image("settings1")
  605. .renderingMode(.template)
  606. .resizable()
  607. .frame(width: 24, height: 24)
  608. .padding(8)
  609. }.foregroundColor(.loopGray)
  610. }
  611. .padding(.horizontal, 24)
  612. .padding(.bottom, geo.safeAreaInsets.bottom)
  613. }
  614. }
  615. var body: some View {
  616. GeometryReader { geo in
  617. VStack(spacing: 0) {
  618. header(geo)
  619. infoPanel
  620. mainChart
  621. legendPanel
  622. statPanel()
  623. bottomPanel(geo)
  624. }
  625. .edgesIgnoringSafeArea(.vertical)
  626. }
  627. .onAppear(perform: configureView)
  628. .navigationTitle("Home")
  629. .navigationBarHidden(true)
  630. .ignoresSafeArea(.keyboard)
  631. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  632. popup
  633. .padding()
  634. .background(
  635. RoundedRectangle(cornerRadius: 8, style: .continuous)
  636. .fill(Color(UIColor.darkGray))
  637. )
  638. .onTapGesture {
  639. isStatusPopupPresented = false
  640. }
  641. .gesture(
  642. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  643. .onEnded { value in
  644. if value.translation.height < 0 {
  645. isStatusPopupPresented = false
  646. }
  647. }
  648. )
  649. }
  650. }
  651. private var popup: some View {
  652. VStack(alignment: .leading, spacing: 4) {
  653. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  654. .padding(.bottom, 4)
  655. if let suggestion = state.suggestion {
  656. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  657. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  658. } else {
  659. Text("No sugestion found").font(.body).foregroundColor(.white)
  660. }
  661. if let errorMessage = state.errorMessage, let date = state.errorDate {
  662. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  663. .foregroundColor(.white)
  664. .font(.headline)
  665. .padding(.bottom, 4)
  666. .padding(.top, 8)
  667. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  668. }
  669. }
  670. }
  671. }
  672. }