HomeRootView.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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: true, 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 {
  313. ForEach(timeButtons) { button in
  314. Text(button.active ? NSLocalizedString(button.label, comment: "") : button.number).onTapGesture {
  315. let index = timeButtons.firstIndex(where: { $0.label == button.label }) ?? 0
  316. highlightButtons(index)
  317. state.hours = button.hours
  318. }
  319. .foregroundStyle(button.active ? .primary : .secondary)
  320. .frame(maxHeight: 20).padding(.horizontal, button.active ? 20 : 10)
  321. .background(button.active ? Color(.systemGray5) : .clear, in: .capsule(style: .circular))
  322. }
  323. Image(systemName: "ellipsis.circle.fill")
  324. .foregroundStyle(.secondary)
  325. .padding(.leading)
  326. .onTapGesture {
  327. state.showModal(for: .statisticsConfig)
  328. }
  329. }
  330. .font(buttonFont)
  331. .padding(.top, 20)
  332. .padding(.bottom, 40)
  333. }
  334. var legendPanel: some View {
  335. ZStack {
  336. HStack(alignment: .center) {
  337. Group {
  338. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  339. Text("BG")
  340. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  341. }
  342. Group {
  343. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  344. .padding(.leading, 8)
  345. Text("IOB")
  346. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  347. }
  348. Group {
  349. Circle().fill(Color.zt).frame(width: 8, height: 8)
  350. .padding(.leading, 8)
  351. Text("ZT")
  352. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  353. }
  354. Group {
  355. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  356. .padding(.leading, 8)
  357. Text("COB")
  358. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  359. }
  360. Group {
  361. Circle().fill(Color.uam).frame(width: 8, height: 8)
  362. .padding(.leading, 8)
  363. Text("UAM")
  364. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  365. }
  366. if let eventualBG = state.eventualBG {
  367. Text(
  368. "⇢ " + numberFormatter.string(
  369. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  370. )!
  371. )
  372. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  373. }
  374. }
  375. .frame(maxWidth: .infinity)
  376. .padding([.bottom], 20)
  377. }
  378. }
  379. var mainChart: some View {
  380. ZStack {
  381. if state.animatedBackground {
  382. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  383. .ignoresSafeArea()
  384. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  385. }
  386. MainChartView(
  387. glucose: $state.glucose,
  388. isManual: $state.isManual,
  389. suggestion: $state.suggestion,
  390. tempBasals: $state.tempBasals,
  391. boluses: $state.boluses,
  392. suspensions: $state.suspensions,
  393. announcement: $state.announcement,
  394. hours: .constant(state.filteredHours),
  395. maxBasal: $state.maxBasal,
  396. autotunedBasalProfile: $state.autotunedBasalProfile,
  397. basalProfile: $state.basalProfile,
  398. tempTargets: $state.tempTargets,
  399. carbs: $state.carbs,
  400. timerDate: $state.timerDate,
  401. units: $state.units,
  402. smooth: $state.smooth,
  403. highGlucose: $state.highGlucose,
  404. lowGlucose: $state.lowGlucose,
  405. screenHours: $state.hours,
  406. displayXgridLines: $state.displayXgridLines,
  407. displayYgridLines: $state.displayYgridLines,
  408. thresholdLines: $state.thresholdLines
  409. )
  410. }
  411. // .padding(.bottom)
  412. .modal(for: .dataTable, from: self)
  413. }
  414. @ViewBuilder private func profiles(_: GeometryProxy) -> some View {
  415. let colour: Color = colorScheme == .dark ? .black : .white
  416. // Rectangle().fill(colour).frame(maxHeight: 1)
  417. ZStack {
  418. Rectangle().fill(Color.gray.opacity(0.3)).frame(maxHeight: 40)
  419. let cancel = fetchedPercent.first?.enabled ?? false
  420. HStack(spacing: cancel ? 25 : 15) {
  421. Text(selectedProfile().name).foregroundColor(.secondary)
  422. if cancel, selectedProfile().isOn {
  423. Button { showCancelAlert.toggle() }
  424. label: {
  425. Image(systemName: "xmark")
  426. .foregroundStyle(.secondary)
  427. }
  428. }
  429. Button { state.showModal(for: .overrideProfilesConfig) }
  430. label: {
  431. Image(systemName: "person.3.sequence.fill")
  432. .symbolRenderingMode(.palette)
  433. .foregroundStyle(
  434. !(fetchedPercent.first?.enabled ?? false) ? .green : .cyan,
  435. !(fetchedPercent.first?.enabled ?? false) ? .cyan : .green,
  436. .purple
  437. )
  438. }
  439. }
  440. }
  441. .alert(
  442. "Return to Normal?", isPresented: $showCancelAlert,
  443. actions: {
  444. Button("No", role: .cancel) {}
  445. Button("Yes", role: .destructive) {
  446. state.cancelProfile()
  447. }
  448. }, message: { Text("This will change settings back to your normal profile.") }
  449. )
  450. Rectangle().fill(colour).frame(maxHeight: 1)
  451. }
  452. private func selectedProfile() -> (name: String, isOn: Bool) {
  453. var profileString = ""
  454. var display: Bool = false
  455. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  456. let indefinite = fetchedPercent.first?.indefinite ?? false
  457. let addedMinutes = Int(duration)
  458. let date = fetchedPercent.first?.date ?? Date()
  459. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  460. display.toggle()
  461. }
  462. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  463. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  464. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  465. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  466. } else {
  467. let id_ = fetchedPercent.first?.id ?? ""
  468. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  469. if profile != nil {
  470. profileString = profile?.name?.description ?? ""
  471. }
  472. }
  473. return (name: profileString, isOn: display)
  474. }
  475. func highlightButtons(_ int: Int) {
  476. var index = 0
  477. repeat {
  478. if index == int {
  479. timeButtons[index].active = true
  480. } else {
  481. timeButtons[index].active = false
  482. }
  483. index += 1
  484. } while index < timeButtons.count
  485. }
  486. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  487. ZStack {
  488. Rectangle().fill(Color.gray.opacity(0.3)).frame(height: 50 + geo.safeAreaInsets.bottom)
  489. HStack {
  490. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  491. label: {
  492. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  493. Image("carbs")
  494. .renderingMode(.template)
  495. .resizable()
  496. .frame(width: 24, height: 24)
  497. .foregroundColor(.loopYellow)
  498. .padding(8)
  499. if let carbsReq = state.carbsRequired {
  500. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  501. .font(.caption)
  502. .foregroundColor(.white)
  503. .padding(4)
  504. .background(Capsule().fill(Color.red))
  505. }
  506. }
  507. }.buttonStyle(.borderless)
  508. Spacer()
  509. Button { state.showModal(for: .addTempTarget) }
  510. label: {
  511. Image("target")
  512. .renderingMode(.template)
  513. .resizable()
  514. .frame(width: 24, height: 24)
  515. .padding(8)
  516. }
  517. .foregroundColor(.loopGreen)
  518. .buttonStyle(.borderless)
  519. Spacer()
  520. Button {
  521. state.showModal(for: .bolus(
  522. waitForSuggestion: true,
  523. fetch: false
  524. ))
  525. }
  526. label: {
  527. Image("bolus")
  528. .renderingMode(.template)
  529. .resizable()
  530. .frame(width: 24, height: 24)
  531. .padding(8)
  532. }
  533. .foregroundColor(.insulin)
  534. .buttonStyle(.borderless)
  535. Spacer()
  536. if state.allowManualTemp {
  537. Button { state.showModal(for: .manualTempBasal) }
  538. label: {
  539. Image("bolus1")
  540. .renderingMode(.template)
  541. .resizable()
  542. .frame(width: 24, height: 24)
  543. .padding(8)
  544. }
  545. .foregroundColor(.insulin)
  546. .buttonStyle(.borderless)
  547. Spacer()
  548. }
  549. Button { state.showModal(for: .statistics)
  550. }
  551. label: {
  552. Image(systemName: "chart.xyaxis.line")
  553. .renderingMode(.template)
  554. .resizable()
  555. .frame(width: 24, height: 24)
  556. .padding(8)
  557. }
  558. .foregroundColor(.purple)
  559. .buttonStyle(.borderless)
  560. Spacer()
  561. Button { state.showModal(for: .settings) }
  562. label: {
  563. Image("settings1")
  564. .renderingMode(.template)
  565. .resizable()
  566. .frame(width: 24, height: 24)
  567. .padding(8)
  568. }
  569. .foregroundColor(.loopGray)
  570. .buttonStyle(.borderless)
  571. }
  572. .padding(.horizontal, 24)
  573. .padding(.bottom, geo.safeAreaInsets.bottom)
  574. }
  575. }
  576. var body: some View {
  577. GeometryReader { geo in
  578. VStack(spacing: 0) {
  579. header(geo)
  580. infoPanel
  581. mainChart
  582. timeInterval
  583. legendPanel
  584. profiles(geo)
  585. bottomPanel(geo)
  586. }
  587. .edgesIgnoringSafeArea(.vertical)
  588. }
  589. .onAppear(perform: configureView)
  590. .navigationTitle("Home")
  591. .navigationBarHidden(true)
  592. .ignoresSafeArea(.keyboard)
  593. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  594. popup
  595. .padding()
  596. .background(
  597. RoundedRectangle(cornerRadius: 8, style: .continuous)
  598. .fill(Color(UIColor.darkGray))
  599. )
  600. .onTapGesture {
  601. isStatusPopupPresented = false
  602. }
  603. .gesture(
  604. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  605. .onEnded { value in
  606. if value.translation.height < 0 {
  607. isStatusPopupPresented = false
  608. }
  609. }
  610. )
  611. }
  612. }
  613. private var popup: some View {
  614. VStack(alignment: .leading, spacing: 4) {
  615. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  616. .padding(.bottom, 4)
  617. if let suggestion = state.suggestion {
  618. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  619. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  620. } else {
  621. Text("No sugestion found").font(.body).foregroundColor(.white)
  622. }
  623. if let errorMessage = state.errorMessage, let date = state.errorDate {
  624. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  625. .foregroundColor(.white)
  626. .font(.headline)
  627. .padding(.bottom, 4)
  628. .padding(.top, 8)
  629. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  630. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  631. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  632. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  633. }
  634. }
  635. }
  636. }
  637. }