HomeRootView.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 showCancelAlert = false
  12. struct Buttons: Identifiable {
  13. let label: String
  14. let number: String
  15. var active: Bool
  16. let hours: Int16
  17. var id: String { label }
  18. }
  19. @State var timeButtons: [Buttons] = [
  20. Buttons(label: "2 hours", number: "2", active: false, hours: 2),
  21. Buttons(label: "4 hours", number: "4", active: false, hours: 4),
  22. Buttons(label: "6 hours", number: "6", active: false, hours: 6),
  23. Buttons(label: "12 hours", number: "12", active: false, hours: 12),
  24. Buttons(label: "24 hours", number: "24", active: false, hours: 24)
  25. ]
  26. let buttonFont = Font.custom("TimeButtonFont", size: 14)
  27. @Environment(\.managedObjectContext) var moc
  28. @Environment(\.colorScheme) var colorScheme
  29. @FetchRequest(
  30. entity: Override.entity(),
  31. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  32. ) var fetchedPercent: FetchedResults<Override>
  33. @FetchRequest(
  34. entity: OverridePresets.entity(),
  35. sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)], predicate: NSPredicate(
  36. format: "name != %@", "" as String
  37. )
  38. ) var fetchedProfiles: FetchedResults<OverridePresets>
  39. @FetchRequest(
  40. entity: TempTargets.entity(),
  41. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  42. ) var sliderTTpresets: FetchedResults<TempTargets>
  43. @FetchRequest(
  44. entity: TempTargetsSlider.entity(),
  45. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  46. ) var enactedSliderTT: FetchedResults<TempTargetsSlider>
  47. private var numberFormatter: NumberFormatter {
  48. let formatter = NumberFormatter()
  49. formatter.numberStyle = .decimal
  50. formatter.maximumFractionDigits = 2
  51. return formatter
  52. }
  53. private var fetchedTargetFormatter: NumberFormatter {
  54. let formatter = NumberFormatter()
  55. formatter.numberStyle = .decimal
  56. if state.units == .mmolL {
  57. formatter.maximumFractionDigits = 1
  58. } else { formatter.maximumFractionDigits = 0 }
  59. return formatter
  60. }
  61. private var targetFormatter: NumberFormatter {
  62. let formatter = NumberFormatter()
  63. formatter.numberStyle = .decimal
  64. formatter.maximumFractionDigits = 1
  65. return formatter
  66. }
  67. private var tirFormatter: NumberFormatter {
  68. let formatter = NumberFormatter()
  69. formatter.numberStyle = .decimal
  70. formatter.maximumFractionDigits = 0
  71. return formatter
  72. }
  73. private var dateFormatter: DateFormatter {
  74. let dateFormatter = DateFormatter()
  75. dateFormatter.timeStyle = .short
  76. return dateFormatter
  77. }
  78. private var spriteScene: SKScene {
  79. let scene = SnowScene()
  80. scene.scaleMode = .resizeFill
  81. scene.backgroundColor = .clear
  82. return scene
  83. }
  84. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  85. HStack(alignment: .bottom) {
  86. Spacer()
  87. cobIobView
  88. Spacer()
  89. glucoseView
  90. Spacer()
  91. pumpView
  92. Spacer()
  93. loopView
  94. Spacer()
  95. }
  96. .frame(maxWidth: .infinity)
  97. .padding(.top, 10 + geo.safeAreaInsets.top)
  98. .padding(.bottom, 10)
  99. .background(Color.gray.opacity(0.3))
  100. }
  101. var cobIobView: some View {
  102. VStack(alignment: .leading, spacing: 12) {
  103. HStack {
  104. Text("IOB").font(.footnote).foregroundColor(.secondary)
  105. Text(
  106. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  107. NSLocalizedString(" U", comment: "Insulin unit")
  108. )
  109. .font(.footnote).fontWeight(.bold)
  110. }.frame(alignment: .top)
  111. HStack {
  112. Text("COB").font(.footnote).foregroundColor(.secondary)
  113. Text(
  114. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  115. NSLocalizedString(" g", comment: "gram of carbs")
  116. )
  117. .font(.footnote).fontWeight(.bold)
  118. }.frame(alignment: .bottom)
  119. }
  120. }
  121. var glucoseView: some View {
  122. CurrentGlucoseView(
  123. recentGlucose: $state.recentGlucose,
  124. timerDate: $state.timerDate,
  125. delta: $state.glucoseDelta,
  126. units: $state.units,
  127. alarm: $state.alarm,
  128. lowGlucose: $state.lowGlucose,
  129. highGlucose: $state.highGlucose
  130. )
  131. .onTapGesture {
  132. if state.alarm == nil {
  133. state.openCGM()
  134. } else {
  135. state.showModal(for: .snooze)
  136. }
  137. }
  138. .onLongPressGesture {
  139. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  140. impactHeavy.impactOccurred()
  141. if state.alarm == nil {
  142. state.showModal(for: .snooze)
  143. } else {
  144. state.openCGM()
  145. }
  146. }
  147. }
  148. var pumpView: some View {
  149. PumpView(
  150. reservoir: $state.reservoir,
  151. battery: $state.battery,
  152. name: $state.pumpName,
  153. expiresAtDate: $state.pumpExpiresAtDate,
  154. timerDate: $state.timerDate,
  155. timeZone: $state.timeZone
  156. )
  157. .onTapGesture {
  158. if state.pumpDisplayState != nil {
  159. state.setupPump = true
  160. }
  161. }
  162. }
  163. var loopView: some View {
  164. LoopView(
  165. suggestion: $state.suggestion,
  166. enactedSuggestion: $state.enactedSuggestion,
  167. closedLoop: $state.closedLoop,
  168. timerDate: $state.timerDate,
  169. isLooping: $state.isLooping,
  170. lastLoopDate: $state.lastLoopDate,
  171. manualTempBasal: $state.manualTempBasal
  172. ).onTapGesture {
  173. isStatusPopupPresented = true
  174. }.onLongPressGesture {
  175. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  176. impactHeavy.impactOccurred()
  177. state.runLoop()
  178. }
  179. }
  180. var tempBasalString: String? {
  181. guard let tempRate = state.tempRate else {
  182. return nil
  183. }
  184. let rateString = numberFormatter.string(from: tempRate as NSNumber) ?? "0"
  185. var manualBasalString = ""
  186. if state.apsManager.isManualTempBasal {
  187. manualBasalString = NSLocalizedString(
  188. " - Manual Basal ⚠️",
  189. comment: "Manual Temp basal"
  190. )
  191. }
  192. return rateString + NSLocalizedString(" U/hr", comment: "Unit per hour with space") + manualBasalString
  193. }
  194. var tempTargetString: String? {
  195. guard let tempTarget = state.tempTarget else {
  196. return nil
  197. }
  198. let target = tempTarget.targetBottom ?? 0
  199. let unitString = targetFormatter.string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber) ?? ""
  200. let rawString = (tirFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber) ?? "") + " " + state.units
  201. .rawValue
  202. var string = ""
  203. if sliderTTpresets.first?.active ?? false {
  204. let hbt = sliderTTpresets.first?.hbt ?? 0
  205. string = ", " + (tirFormatter.string(from: state.infoPanelTTPercentage(hbt, target) as NSNumber) ?? "") + " %"
  206. }
  207. let percentString = state
  208. .units == .mmolL ? (unitString + " mmol/L" + string) : (rawString + (string == "0" ? "" : string))
  209. return tempTarget.displayName + " " + percentString
  210. }
  211. var overrideString: String? {
  212. guard fetchedPercent.first?.enabled ?? false else {
  213. return nil
  214. }
  215. var percentString = "\((fetchedPercent.first?.percentage ?? 100).formatted(.number)) %"
  216. var target = (fetchedPercent.first?.target ?? 100) as Decimal
  217. let indefinite = (fetchedPercent.first?.indefinite ?? false)
  218. let unit = state.units.rawValue
  219. if state.units == .mmolL {
  220. target = target.asMmolL
  221. }
  222. var targetString = (fetchedTargetFormatter.string(from: target as NSNumber) ?? "") + " " + unit
  223. if tempTargetString != nil || target == 0 { targetString = "" }
  224. percentString = percentString == "100 %" ? "" : percentString
  225. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  226. let addedMinutes = Int(duration)
  227. let date = fetchedPercent.first?.date ?? Date()
  228. var newDuration: Decimal = 0
  229. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() {
  230. newDuration = Decimal(Date().distance(to: date.addingTimeInterval(addedMinutes.minutes.timeInterval)).minutes)
  231. }
  232. var durationString = indefinite ?
  233. "" : newDuration >= 1 ?
  234. (newDuration.formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) + " min") :
  235. (
  236. newDuration > 0 ? (
  237. (newDuration * 60).formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) + " s"
  238. ) :
  239. ""
  240. )
  241. let smbToggleString = (fetchedPercent.first?.smbIsOff ?? false) ? " \u{20e0}" : ""
  242. var comma1 = ", "
  243. var comma2 = comma1
  244. var comma3 = comma1
  245. if targetString == "" || percentString == "" { comma1 = "" }
  246. if durationString == "" { comma2 = "" }
  247. if smbToggleString == "" { comma3 = "" }
  248. if percentString == "", targetString == "" {
  249. comma1 = ""
  250. comma2 = ""
  251. }
  252. if percentString == "", targetString == "", smbToggleString == "" {
  253. durationString = ""
  254. comma1 = ""
  255. comma2 = ""
  256. comma3 = ""
  257. }
  258. if durationString == "" {
  259. comma2 = ""
  260. }
  261. if smbToggleString == "" {
  262. comma3 = ""
  263. }
  264. if durationString == "", !indefinite {
  265. return nil
  266. }
  267. return percentString + comma1 + targetString + comma2 + durationString + comma3 + smbToggleString
  268. }
  269. var infoPanel: some View {
  270. HStack(alignment: .center) {
  271. if state.pumpSuspended {
  272. Text("Pump suspended")
  273. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  274. .padding(.leading, 8)
  275. } else if let tempBasalString = tempBasalString {
  276. Text(tempBasalString)
  277. .font(.system(size: 12, weight: .bold))
  278. .foregroundColor(.insulin)
  279. .padding(.leading, 8)
  280. }
  281. if let tempTargetString = tempTargetString {
  282. Text(tempTargetString)
  283. .font(.caption)
  284. .foregroundColor(.secondary)
  285. }
  286. Spacer()
  287. if let overrideString = overrideString {
  288. Text("👤 " + overrideString)
  289. .font(.system(size: 12))
  290. .foregroundColor(.secondary)
  291. .padding(.trailing, 8)
  292. }
  293. if state.closedLoop, state.settingsManager.preferences.maxIOB == 0 {
  294. Text("Max IOB: 0").font(.callout).foregroundColor(.orange).padding(.trailing, 20)
  295. }
  296. if let progress = state.bolusProgress {
  297. HStack {
  298. Text("Bolusing")
  299. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  300. ProgressView(value: Double(progress))
  301. .progressViewStyle(BolusProgressViewStyle())
  302. .padding(.trailing, 8)
  303. }
  304. .onTapGesture {
  305. state.cancelBolus()
  306. }
  307. }
  308. }
  309. .frame(maxWidth: .infinity, maxHeight: 30)
  310. }
  311. var timeInterval: some View {
  312. HStack(alignment: .center) {
  313. ForEach(timeButtons) { button in
  314. Text(button.active ? NSLocalizedString(button.label, comment: "") : button.number).onTapGesture {
  315. state.hours = button.hours
  316. }
  317. .foregroundStyle(button.active ? .primary : .secondary)
  318. .frame(maxHeight: 20).padding(.horizontal)
  319. .background(button.active ? Color(.systemGray5) : .clear, in: .capsule(style: .circular))
  320. }
  321. Image(systemName: "ellipsis.circle.fill")
  322. .foregroundStyle(.secondary)
  323. .padding(.leading)
  324. .onTapGesture {
  325. state.showModal(for: .statisticsConfig)
  326. }
  327. }
  328. .font(buttonFont)
  329. .padding(.top, 20)
  330. .padding(.bottom, 40)
  331. }
  332. var legendPanel: some View {
  333. ZStack {
  334. HStack(alignment: .center) {
  335. Group {
  336. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  337. Text("BG")
  338. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  339. }
  340. Group {
  341. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  342. .padding(.leading, 8)
  343. Text("IOB")
  344. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  345. }
  346. Group {
  347. Circle().fill(Color.zt).frame(width: 8, height: 8)
  348. .padding(.leading, 8)
  349. Text("ZT")
  350. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  351. }
  352. Group {
  353. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  354. .padding(.leading, 8)
  355. Text("COB")
  356. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  357. }
  358. Group {
  359. Circle().fill(Color.uam).frame(width: 8, height: 8)
  360. .padding(.leading, 8)
  361. Text("UAM")
  362. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  363. }
  364. if let eventualBG = state.eventualBG {
  365. Text(
  366. "⇢ " + numberFormatter.string(
  367. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  368. )!
  369. )
  370. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  371. }
  372. }
  373. .frame(maxWidth: .infinity)
  374. .padding([.bottom], 20)
  375. }
  376. }
  377. var mainChart: some View {
  378. ZStack {
  379. if state.animatedBackground {
  380. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  381. .ignoresSafeArea()
  382. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  383. }
  384. MainChartView(
  385. glucose: $state.glucose,
  386. isManual: $state.isManual,
  387. suggestion: $state.suggestion,
  388. tempBasals: $state.tempBasals,
  389. boluses: $state.boluses,
  390. suspensions: $state.suspensions,
  391. announcement: $state.announcement,
  392. hours: .constant(state.filteredHours),
  393. maxBasal: $state.maxBasal,
  394. autotunedBasalProfile: $state.autotunedBasalProfile,
  395. basalProfile: $state.basalProfile,
  396. tempTargets: $state.tempTargets,
  397. carbs: $state.carbs,
  398. timerDate: $state.timerDate,
  399. units: $state.units,
  400. smooth: $state.smooth,
  401. highGlucose: $state.highGlucose,
  402. lowGlucose: $state.lowGlucose,
  403. screenHours: $state.hours,
  404. displayXgridLines: $state.displayXgridLines,
  405. displayYgridLines: $state.displayYgridLines,
  406. thresholdLines: $state.thresholdLines
  407. )
  408. }
  409. // .padding(.bottom)
  410. .modal(for: .dataTable, from: self)
  411. }
  412. @ViewBuilder private func profiles(_: GeometryProxy) -> some View {
  413. let colour: Color = colorScheme == .dark ? .black : .white
  414. // Rectangle().fill(colour).frame(maxHeight: 1)
  415. ZStack {
  416. Rectangle().fill(Color.gray.opacity(0.3)).frame(maxHeight: 40)
  417. let cancel = fetchedPercent.first?.enabled ?? false
  418. HStack(spacing: cancel ? 25 : 15) {
  419. Text(selectedProfile().name).foregroundColor(.secondary)
  420. if cancel, selectedProfile().isOn {
  421. Button { showCancelAlert.toggle() }
  422. label: {
  423. Image(systemName: "xmark")
  424. .foregroundStyle(.secondary)
  425. }
  426. }
  427. Button { state.showModal(for: .overrideProfilesConfig) }
  428. label: {
  429. Image(systemName: "person.3.sequence.fill")
  430. .symbolRenderingMode(.palette)
  431. .foregroundStyle(
  432. !(fetchedPercent.first?.enabled ?? false) ? .green : .cyan,
  433. !(fetchedPercent.first?.enabled ?? false) ? .cyan : .green,
  434. .purple
  435. )
  436. }
  437. }
  438. }
  439. .alert(
  440. "Return to Normal?", isPresented: $showCancelAlert,
  441. actions: {
  442. Button("No", role: .cancel) {}
  443. Button("Yes", role: .destructive) {
  444. state.cancelProfile()
  445. }
  446. }, message: { Text("This will change settings back to your normal profile.") }
  447. )
  448. Rectangle().fill(colour).frame(maxHeight: 1)
  449. }
  450. private func selectedProfile() -> (name: String, isOn: Bool) {
  451. var profileString = ""
  452. var display: Bool = false
  453. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  454. let indefinite = fetchedPercent.first?.indefinite ?? false
  455. let addedMinutes = Int(duration)
  456. let date = fetchedPercent.first?.date ?? Date()
  457. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  458. display.toggle()
  459. }
  460. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  461. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  462. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  463. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  464. } else {
  465. let id_ = fetchedPercent.first?.id ?? ""
  466. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  467. if profile != nil {
  468. profileString = profile?.name?.description ?? ""
  469. }
  470. }
  471. return (name: profileString, isOn: display)
  472. }
  473. func highlightButtons() {
  474. for i in 0 ..< timeButtons.count {
  475. timeButtons[i].active = timeButtons[i].hours == state.hours
  476. }
  477. }
  478. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  479. ZStack {
  480. Rectangle().fill(Color.gray.opacity(0.3)).frame(height: 50 + geo.safeAreaInsets.bottom)
  481. HStack {
  482. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  483. label: {
  484. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  485. Image("carbs")
  486. .renderingMode(.template)
  487. .resizable()
  488. .frame(width: 24, height: 24)
  489. .foregroundColor(.loopYellow)
  490. .padding(8)
  491. if let carbsReq = state.carbsRequired {
  492. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  493. .font(.caption)
  494. .foregroundColor(.white)
  495. .padding(4)
  496. .background(Capsule().fill(Color.red))
  497. }
  498. }
  499. }.buttonStyle(.borderless)
  500. Spacer()
  501. Button { state.showModal(for: .addTempTarget) }
  502. label: {
  503. Image("target")
  504. .renderingMode(.template)
  505. .resizable()
  506. .frame(width: 24, height: 24)
  507. .padding(8)
  508. }
  509. .foregroundColor(.loopGreen)
  510. .buttonStyle(.borderless)
  511. Spacer()
  512. Button {
  513. state.showModal(for: .bolus(
  514. waitForSuggestion: true,
  515. fetch: false
  516. ))
  517. }
  518. label: {
  519. Image("bolus")
  520. .renderingMode(.template)
  521. .resizable()
  522. .frame(width: 24, height: 24)
  523. .padding(8)
  524. }
  525. .foregroundColor(.insulin)
  526. .buttonStyle(.borderless)
  527. Spacer()
  528. if state.allowManualTemp {
  529. Button { state.showModal(for: .manualTempBasal) }
  530. label: {
  531. Image("bolus1")
  532. .renderingMode(.template)
  533. .resizable()
  534. .frame(width: 24, height: 24)
  535. .padding(8)
  536. }
  537. .foregroundColor(.insulin)
  538. .buttonStyle(.borderless)
  539. Spacer()
  540. }
  541. Button { state.showModal(for: .statistics)
  542. }
  543. label: {
  544. Image(systemName: "chart.xyaxis.line")
  545. .renderingMode(.template)
  546. .resizable()
  547. .frame(width: 24, height: 24)
  548. .padding(8)
  549. }
  550. .foregroundColor(.purple)
  551. .buttonStyle(.borderless)
  552. Spacer()
  553. Button { state.showModal(for: .settings) }
  554. label: {
  555. Image("settings1")
  556. .renderingMode(.template)
  557. .resizable()
  558. .frame(width: 24, height: 24)
  559. .padding(8)
  560. }
  561. .foregroundColor(.loopGray)
  562. .buttonStyle(.borderless)
  563. }
  564. .padding(.horizontal, 24)
  565. .padding(.bottom, geo.safeAreaInsets.bottom)
  566. }
  567. }
  568. var body: some View {
  569. GeometryReader { geo in
  570. VStack(spacing: 0) {
  571. header(geo)
  572. infoPanel
  573. mainChart
  574. timeInterval
  575. legendPanel
  576. profiles(geo)
  577. bottomPanel(geo)
  578. }
  579. .edgesIgnoringSafeArea(.vertical)
  580. }
  581. .onChange(of: state.hours) { _ in
  582. highlightButtons()
  583. }
  584. .onAppear {
  585. configureView {
  586. highlightButtons()
  587. }
  588. }
  589. .navigationTitle("Home")
  590. .navigationBarHidden(true)
  591. .ignoresSafeArea(.keyboard)
  592. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  593. popup
  594. .padding()
  595. .background(
  596. RoundedRectangle(cornerRadius: 8, style: .continuous)
  597. .fill(Color(UIColor.darkGray))
  598. )
  599. .onTapGesture {
  600. isStatusPopupPresented = false
  601. }
  602. .gesture(
  603. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  604. .onEnded { value in
  605. if value.translation.height < 0 {
  606. isStatusPopupPresented = false
  607. }
  608. }
  609. )
  610. }
  611. }
  612. private var popup: some View {
  613. VStack(alignment: .leading, spacing: 4) {
  614. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  615. .padding(.bottom, 4)
  616. if let suggestion = state.suggestion {
  617. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  618. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  619. } else {
  620. Text("No sugestion found").font(.body).foregroundColor(.white)
  621. }
  622. if let errorMessage = state.errorMessage, let date = state.errorDate {
  623. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  624. .foregroundColor(.white)
  625. .font(.headline)
  626. .padding(.bottom, 4)
  627. .padding(.top, 8)
  628. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  629. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  630. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  631. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  632. }
  633. }
  634. }
  635. }
  636. }