HomeRootView.swift 27 KB

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