HomeRootView.swift 30 KB

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