HomeRootView.swift 33 KB

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