HomeRootView.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. import SpriteKit
  2. import SwiftDate
  3. import SwiftUI
  4. import Swinject
  5. extension Home {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State var isStatusPopupPresented = false
  10. @State var selectedState: durationState
  11. private var numberFormatter: NumberFormatter {
  12. let formatter = NumberFormatter()
  13. formatter.numberStyle = .decimal
  14. formatter.maximumFractionDigits = 2
  15. return formatter
  16. }
  17. private var targetFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 1
  21. return formatter
  22. }
  23. private var tirFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 0
  27. return formatter
  28. }
  29. private var dateFormatter: DateFormatter {
  30. let dateFormatter = DateFormatter()
  31. dateFormatter.timeStyle = .short
  32. return dateFormatter
  33. }
  34. private var spriteScene: SKScene {
  35. let scene = SnowScene()
  36. scene.scaleMode = .resizeFill
  37. scene.backgroundColor = .clear
  38. return scene
  39. }
  40. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  41. HStack(alignment: .bottom) {
  42. Spacer()
  43. cobIobView
  44. Spacer()
  45. glucoseView
  46. Spacer()
  47. pumpView
  48. Spacer()
  49. loopView
  50. Spacer()
  51. }
  52. .frame(maxWidth: .infinity)
  53. .frame(maxHeight: 70)
  54. .padding(.top, geo.safeAreaInsets.top)
  55. .background(Color.gray.opacity(0.2))
  56. }
  57. var cobIobView: some View {
  58. VStack(alignment: .leading, spacing: 12) {
  59. HStack {
  60. Text("IOB").font(.caption2).foregroundColor(.secondary)
  61. Text(
  62. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  63. NSLocalizedString(" U", comment: "Insulin unit")
  64. )
  65. .font(.system(size: 12, weight: .bold))
  66. }
  67. HStack {
  68. Text("COB").font(.caption2).foregroundColor(.secondary)
  69. Text(
  70. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  71. NSLocalizedString(" g", comment: "gram of carbs")
  72. )
  73. .font(.system(size: 12, weight: .bold))
  74. }
  75. }
  76. }
  77. var glucoseView: some View {
  78. CurrentGlucoseView(
  79. recentGlucose: $state.recentGlucose,
  80. delta: $state.glucoseDelta,
  81. units: $state.units,
  82. alarm: $state.alarm
  83. )
  84. .onTapGesture {
  85. if state.alarm == nil {
  86. state.openCGM()
  87. } else {
  88. state.showModal(for: .snooze)
  89. }
  90. }
  91. .onLongPressGesture {
  92. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  93. impactHeavy.impactOccurred()
  94. if state.alarm == nil {
  95. state.showModal(for: .snooze)
  96. } else {
  97. state.openCGM()
  98. }
  99. }
  100. }
  101. var pumpView: some View {
  102. PumpView(
  103. reservoir: $state.reservoir,
  104. battery: $state.battery,
  105. name: $state.pumpName,
  106. expiresAtDate: $state.pumpExpiresAtDate,
  107. timerDate: $state.timerDate
  108. )
  109. .onTapGesture {
  110. if state.pumpDisplayState != nil {
  111. state.setupPump = true
  112. }
  113. }
  114. }
  115. var loopView: some View {
  116. LoopView(
  117. suggestion: $state.suggestion,
  118. enactedSuggestion: $state.enactedSuggestion,
  119. closedLoop: $state.closedLoop,
  120. timerDate: $state.timerDate,
  121. isLooping: $state.isLooping,
  122. lastLoopDate: $state.lastLoopDate,
  123. manualTempBasal: $state.manualTempBasal
  124. ).onTapGesture {
  125. isStatusPopupPresented = true
  126. }.onLongPressGesture {
  127. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  128. impactHeavy.impactOccurred()
  129. state.runLoop()
  130. }
  131. }
  132. var infoPanel: some View {
  133. HStack(alignment: .center) {
  134. if state.pumpSuspended {
  135. Text("Pump suspended")
  136. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  137. .padding(.leading, 8)
  138. } else if let tempRate = state.tempRate {
  139. if state.apsManager.isManualTempBasal {
  140. Text(
  141. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  142. NSLocalizedString(" U/hr", comment: "Unit per hour with space") +
  143. NSLocalizedString(" - Manual Basal ⚠️", comment: "Manual Temp basal")
  144. )
  145. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  146. .padding(.leading, 8)
  147. } else {
  148. Text(
  149. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  150. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  151. )
  152. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  153. .padding(.leading, 8)
  154. }
  155. }
  156. if let tempTarget = state.tempTarget {
  157. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  158. if state.units == .mmolL {
  159. Text(
  160. targetFormatter
  161. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  162. )
  163. .font(.caption)
  164. .foregroundColor(.secondary)
  165. if tempTarget.targetBottom != tempTarget.targetTop {
  166. Text("-").font(.caption)
  167. .foregroundColor(.secondary)
  168. Text(
  169. targetFormatter
  170. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  171. " \(state.units.rawValue)"
  172. )
  173. .font(.caption)
  174. .foregroundColor(.secondary)
  175. } else {
  176. Text(state.units.rawValue).font(.caption)
  177. .foregroundColor(.secondary)
  178. }
  179. } else {
  180. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  181. .font(.caption)
  182. .foregroundColor(.secondary)
  183. if tempTarget.targetBottom != tempTarget.targetTop {
  184. Text("-").font(.caption)
  185. .foregroundColor(.secondary)
  186. Text(
  187. targetFormatter
  188. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  189. )
  190. .font(.caption)
  191. .foregroundColor(.secondary)
  192. } else {
  193. Text(state.units.rawValue).font(.caption)
  194. .foregroundColor(.secondary)
  195. }
  196. }
  197. }
  198. Spacer()
  199. if let progress = state.bolusProgress {
  200. Text("Bolusing")
  201. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  202. ProgressView(value: Double(progress))
  203. .progressViewStyle(BolusProgressViewStyle())
  204. .padding(.trailing, 8)
  205. .onTapGesture {
  206. state.cancelBolus()
  207. }
  208. }
  209. }
  210. .frame(maxWidth: .infinity, maxHeight: 30)
  211. }
  212. @ViewBuilder private func statPanel() -> some View {
  213. if state.settingsManager.settings.displayStatistics {
  214. VStack(alignment: .center, spacing: 8) {
  215. VStack(alignment: .center, spacing: 4) {
  216. // stateButton(states: durationState.allCases, selectedState: $selectedState)
  217. HStack {
  218. Group {
  219. durationButton(states: durationState.allCases, selectedState: $selectedState)
  220. Text("Updated").font(.caption2)
  221. .foregroundColor(.secondary)
  222. Text(
  223. dateFormatter.string(from: state.statistics?.created_at ?? Date())
  224. ).font(.system(size: 12))
  225. }
  226. }
  227. var hba1c_all = getString(state.statistics?.Statistics.HbA1c, .total)
  228. var average_ = getString(state.statistics?.Statistics.Glucose.Average.day, true)
  229. var median_ = getString(state.statistics?.Statistics.Glucose.Median.day, true)
  230. var tir_low = getString(state.statistics?.Statistics.Distribution.Hypos.day, false)
  231. var tir_high = getString(state.statistics?.Statistics.Distribution.Hypers.day, false)
  232. var tir_ = getString(state.statistics?.Statistics.Distribution.TIR.day, false)
  233. var hba1c_ = getString(state.statistics?.Statistics.HbA1c.day, false)
  234. var sd_ = getString(state.statistics?.Statistics.Variance.SD.day, true)
  235. var cv_ = getString(state.statistics?.Statistics.Variance.CV.day, false)
  236. switch selectedState {
  237. case .day:
  238. continue
  239. case .week:
  240. average_ = getString(state.statistics?.Statistics.Glucose.Average.week, true)
  241. median_ = getString(state.statistics?.Statistics.Glucose.Median.week, true)
  242. tir_low = getString(state.statistics?.Statistics.Distribution.Hypos.week, false)
  243. tir_high = getString(state.statistics?.Statistics.Distribution.Hypers.week, false)
  244. tir_ = getString(state.statistics?.Statistics.Distribution.TIR.week, false)
  245. hba1c_ = getString(state.statistics?.Statistics.HbA1c.week, false)
  246. sd_ = getString(state.statistics?.Statistics.Variance.SD.week, true)
  247. cv_ = getString(state.statistics?.Statistics.Variance.CV.week, false)
  248. case .month:
  249. average_ = getString(state.statistics?.Statistics.Glucose.Average.month, true)
  250. median_ = getString(state.statistics?.Statistics.Glucose.Median.month, true)
  251. tir_low = getString(state.statistics?.Statistics.Distribution.Hypos.month, false)
  252. tir_high = getString(state.statistics?.Statistics.Distribution.Hypers.month, false)
  253. tir_ = getString(state.statistics?.Statistics.Distribution.TIR.month, false)
  254. hba1c_ = getString(state.statistics?.Statistics.HbA1c.month, false)
  255. sd_ = getString(state.statistics?.Statistics.Variance.SD.month, true)
  256. cv_ = getString(state.statistics?.Statistics.Variance.CV.month, false)
  257. case .ninetyDays:
  258. average_ = getString(state.statistics?.Statistics.Glucose.Average.ninetyDays, true)
  259. median_ = getString(state.statistics?.Statistics.Glucose.Median.ninetyDays, true)
  260. tir_low = getString(state.statistics?.Statistics.Distribution.Hypos.ninetyDays, false)
  261. tir_high = getString(state.statistics?.Statistics.Distribution.Hypers.ninetyDays, false)
  262. tir_ = getString(state.statistics?.Statistics.Distribution.TIR.ninetyDays, false)
  263. hba1c_ = getString(state.statistics?.Statistics.HbA1c.ninetyDays, false)
  264. sd_ = getString(state.statistics?.Statistics.Variance.SD.ninetyDays, true)
  265. cv_ = getString(state.statistics?.Statistics.Variance.CV.ninetyDays, false)
  266. case .total:
  267. average_ = getString(state.statistics?.Statistics.Glucose.Average.total, true)
  268. median_ = getString(state.statistics?.Statistics.Glucose.Median.total, true)
  269. tir_low = getString(state.statistics?.Statistics.Distribution.Hypos.total, false)
  270. tir_high = getString(state.statistics?.Statistics.Distribution.Hypers.total, false)
  271. tir_ = getString(state.statistics?.Statistics.Distribution.TIR.total, false)
  272. hba1c_ = getString(state.statistics?.Statistics.HbA1c.total, false)
  273. sd_ = getString(state.statistics?.Statistics.Variance.SD.total, true)
  274. cv_ = getString(state.statistics?.Statistics.Variance.CV.total, false)
  275. }
  276. HStack {
  277. Group {
  278. Text(
  279. NSLocalizedString("Average", comment: "")
  280. ).font(.caption2).foregroundColor(.secondary)
  281. Text(average_).font(.system(size: 12))
  282. Text("Median")
  283. .font(.caption2).foregroundColor(.secondary)
  284. Text(median_).font(.system(size: 12))
  285. }
  286. }
  287. HStack {
  288. Group {
  289. Text(
  290. NSLocalizedString("Low (<", comment: " ") +
  291. (
  292. targetFormatter
  293. .string(from: state.settingsManager.preferences.low as NSNumber) ?? ""
  294. ) + ")"
  295. ).font(.caption2).foregroundColor(.secondary)
  296. Text(tir_low + " %").font(.system(size: 12)).foregroundColor(.loopRed)
  297. Text("Normal").font(.caption2).foregroundColor(.secondary)
  298. Text(tir_ + " %").font(.system(size: 12)).foregroundColor(.loopGreen)
  299. Text(
  300. NSLocalizedString("High (>", comment: " ") +
  301. (targetFormatter.string(from: state.settingsManager.preferences.high as NSNumber) ?? "") +
  302. ")"
  303. ).font(.caption2).foregroundColor(.secondary)
  304. Text(tir_high + " %").font(.system(size: 12)).foregroundColor(.loopYellow)
  305. }
  306. }
  307. HStack {
  308. Group {
  309. Text("HbA1c").font(.caption2).foregroundColor(.secondary)
  310. Text(hba1c_).font(.system(size: 12))
  311. if !state.settingsManager.preferences.displaySD {
  312. Text(
  313. NSLocalizedString("CV", comment: "CV")
  314. ).font(.caption2).foregroundColor(.secondary)
  315. Text(cv_).font(.system(size: 12))
  316. } else {
  317. Text(
  318. NSLocalizedString("SD", comment: "SD")
  319. ).font(.caption2).foregroundColor(.secondary)
  320. Text(sd_).font(.system(size: 12))
  321. }
  322. Text(
  323. NSLocalizedString("All ", comment: "") +
  324. (
  325. targetFormatter
  326. .string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? ""
  327. ) +
  328. NSLocalizedString(" days", comment: "")
  329. ).font(.caption2).foregroundColor(.secondary)
  330. Text(hba1c_all).font(.system(size: 12))
  331. }
  332. }
  333. }
  334. if state.settingsManager.preferences.displayLoops {
  335. HStack {
  336. Group {
  337. Text("Loops").font(.caption2)
  338. .foregroundColor(.secondary)
  339. Text(tirFormatter.string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ?? "")
  340. Text("Average Interval").font(.caption2)
  341. .foregroundColor(.secondary)
  342. Text(targetFormatter.string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ?? "")
  343. Text("Median Duration").font(.caption2)
  344. .foregroundColor(.secondary)
  345. Text(numberFormatter.string(from: (state.statistics?.Statistics.LoopCycles.median_duration ?? 0) as NSNumber) ?? "")
  346. }
  347. }
  348. }
  349. }
  350. .frame(maxWidth: .infinity, maxHeight: 120, alignment: .center)
  351. }
  352. }
  353. var legendPanel: some View {
  354. ZStack {
  355. HStack(alignment: .center) {
  356. Group {
  357. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  358. Text("BG")
  359. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  360. }
  361. Group {
  362. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  363. .padding(.leading, 8)
  364. Text("IOB")
  365. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  366. }
  367. Group {
  368. Circle().fill(Color.zt).frame(width: 8, height: 8)
  369. .padding(.leading, 8)
  370. Text("ZT")
  371. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  372. }
  373. Group {
  374. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  375. .padding(.leading, 8)
  376. Text("COB")
  377. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  378. }
  379. Group {
  380. Circle().fill(Color.uam).frame(width: 8, height: 8)
  381. .padding(.leading, 8)
  382. Text("UAM")
  383. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  384. }
  385. if let eventualBG = state.eventualBG {
  386. Text(
  387. "⇢ " + numberFormatter.string(
  388. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  389. )!
  390. )
  391. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  392. }
  393. }
  394. .frame(maxWidth: .infinity)
  395. }
  396. }
  397. var mainChart: some View {
  398. ZStack {
  399. if state.animatedBackground {
  400. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  401. .ignoresSafeArea()
  402. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  403. }
  404. MainChartView(
  405. glucose: $state.glucose,
  406. suggestion: $state.suggestion,
  407. statistcs: $state.statistics,
  408. tempBasals: $state.tempBasals,
  409. boluses: $state.boluses,
  410. suspensions: $state.suspensions,
  411. hours: .constant(state.filteredHours),
  412. maxBasal: $state.maxBasal,
  413. autotunedBasalProfile: $state.autotunedBasalProfile,
  414. basalProfile: $state.basalProfile,
  415. tempTargets: $state.tempTargets,
  416. carbs: $state.carbs,
  417. timerDate: $state.timerDate,
  418. units: $state.units
  419. )
  420. }
  421. // .padding(.bottom)
  422. .modal(for: .dataTable, from: self)
  423. }
  424. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  425. ZStack {
  426. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  427. HStack {
  428. Button { state.showModal(for: .addCarbs) }
  429. label: {
  430. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  431. Image("carbs")
  432. .renderingMode(.template)
  433. .resizable()
  434. .frame(width: 24, height: 24)
  435. .foregroundColor(.loopGreen)
  436. .padding(8)
  437. if let carbsReq = state.carbsRequired {
  438. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  439. .font(.caption)
  440. .foregroundColor(.white)
  441. .padding(4)
  442. .background(Capsule().fill(Color.red))
  443. }
  444. }
  445. }
  446. Spacer()
  447. Button { state.showModal(for: .addTempTarget) }
  448. label: {
  449. Image("target")
  450. .renderingMode(.template)
  451. .resizable()
  452. .frame(width: 24, height: 24)
  453. .padding(8)
  454. }.foregroundColor(.loopYellow)
  455. Spacer()
  456. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  457. label: {
  458. Image("bolus")
  459. .renderingMode(.template)
  460. .resizable()
  461. .frame(width: 24, height: 24)
  462. .padding(8)
  463. }.foregroundColor(.insulin)
  464. Spacer()
  465. if state.allowManualTemp {
  466. Button { state.showModal(for: .manualTempBasal) }
  467. label: {
  468. Image("bolus1")
  469. .renderingMode(.template)
  470. .resizable()
  471. .frame(width: 24, height: 24)
  472. .padding(8)
  473. }.foregroundColor(.insulin)
  474. Spacer()
  475. }
  476. Button { state.showModal(for: .settings) }
  477. label: {
  478. Image("settings1")
  479. .renderingMode(.template)
  480. .resizable()
  481. .frame(width: 24, height: 24)
  482. .padding(8)
  483. }.foregroundColor(.loopGray)
  484. }
  485. .padding(.horizontal, 24)
  486. .padding(.bottom, geo.safeAreaInsets.bottom)
  487. }
  488. }
  489. var body: some View {
  490. GeometryReader { geo in
  491. VStack(spacing: 0) {
  492. header(geo)
  493. infoPanel
  494. mainChart
  495. legendPanel
  496. statPanel()
  497. bottomPanel(geo)
  498. }
  499. .edgesIgnoringSafeArea(.vertical)
  500. }
  501. .onAppear(perform: configureView)
  502. .navigationTitle("Home")
  503. .navigationBarHidden(true)
  504. .ignoresSafeArea(.keyboard)
  505. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  506. popup
  507. .padding()
  508. .background(
  509. RoundedRectangle(cornerRadius: 8, style: .continuous)
  510. .fill(Color(UIColor.darkGray))
  511. )
  512. .onTapGesture {
  513. isStatusPopupPresented = false
  514. }
  515. .gesture(
  516. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  517. .onEnded { value in
  518. if value.translation.height < 0 {
  519. isStatusPopupPresented = false
  520. }
  521. }
  522. )
  523. }
  524. }
  525. private var popup: some View {
  526. VStack(alignment: .leading, spacing: 4) {
  527. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  528. .padding(.bottom, 4)
  529. if let suggestion = state.suggestion {
  530. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  531. /*
  532. Text(
  533. "Total insulin past 24 hours: " + numberFormatter
  534. .string(from: (state.statistics?.Statistics.Insulin.TDD ?? 0) as NSNumber) +
  535. " U ,bolus insulin: " + numberFormatter
  536. .string(from: (state.statistics?.Statistics.Insulin.bolus ?? 0) as NSNumber) +
  537. " U, temp. basal insulin: " + numberFormatter
  538. .string(from: (state.statistics?.Statistics.Insulin.temp_basal ?? 0) as NSNumber) +
  539. " U, non-looping scheduled basal insulin: " + numberFormatter
  540. .string(from: (state.statistics?.Statistics.Insulin.scheduled_basal ?? 0) as NSNumber) + " U."
  541. )
  542. .font(.caption).foregroundColor(.white)
  543. */
  544. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  545. } else {
  546. Text("No sugestion found").font(.body).foregroundColor(.white)
  547. }
  548. if let errorMessage = state.errorMessage, let date = state.errorDate {
  549. Text("Error at \(dateFormatter.string(from: date))")
  550. .foregroundColor(.white)
  551. .font(.headline)
  552. .padding(.bottom, 4)
  553. .padding(.top, 8)
  554. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  555. }
  556. }
  557. }
  558. private func colorOfGlucose(_ glucose: Decimal) -> Color {
  559. switch glucose {
  560. case 4 ... 8,
  561. 30 ... 46,
  562. 72 ... 144:
  563. return .loopGreen
  564. case 0 ... 4,
  565. 20 ... 71:
  566. return .loopRed
  567. default:
  568. return .loopYellow
  569. }
  570. }
  571. private func getString(_ stat: Decimal?,_ test: Bool) -> String {
  572. var string: String = targetFormatter.string(from: (stat ?? 0) as NSNumber) ?? ""
  573. if state.units != .mmolL, test == true {
  574. string = tirFormatter.string(from: (stat ?? 0) as NSNumber) ?? ""
  575. }
  576. return string
  577. }
  578. }
  579. }