HomeRootView.swift 31 KB

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