HomeRootView.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 ? (colorScheme == .dark ? Color.white : Color.black).opacity(0.9) : .secondary)
  385. .frame(maxHeight: 30).padding(.horizontal, 8)
  386. .background(
  387. button.active ?
  388. // RGB(30, 60, 95)
  389. (
  390. colorScheme == .dark ? Color(red: 0.1176470588, green: 0.2352941176, blue: 0.3725490196) :
  391. Color.white
  392. ) :
  393. Color
  394. .clear
  395. )
  396. .shadow(
  397. color: colorScheme == .dark ? Color(
  398. red: 0.02745098039,
  399. green: 0.1098039216,
  400. blue: 0.1411764706
  401. ) : Color
  402. .black.opacity(0.33),
  403. radius: 3
  404. )
  405. .cornerRadius(20)
  406. }
  407. }
  408. .font(buttonFont)
  409. }
  410. var mainChart: some View {
  411. ZStack {
  412. if state.animatedBackground {
  413. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  414. .ignoresSafeArea()
  415. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  416. }
  417. MainChartView(
  418. glucose: $state.glucose,
  419. isManual: $state.isManual,
  420. suggestion: $state.suggestion,
  421. tempBasals: $state.tempBasals,
  422. boluses: $state.boluses,
  423. suspensions: $state.suspensions,
  424. announcement: $state.announcement,
  425. hours: .constant(state.filteredHours),
  426. maxBasal: $state.maxBasal,
  427. autotunedBasalProfile: $state.autotunedBasalProfile,
  428. basalProfile: $state.basalProfile,
  429. tempTargets: $state.tempTargets,
  430. carbs: $state.carbs,
  431. timerDate: $state.timerDate,
  432. units: $state.units,
  433. smooth: $state.smooth,
  434. highGlucose: $state.highGlucose,
  435. lowGlucose: $state.lowGlucose,
  436. screenHours: $state.hours,
  437. displayXgridLines: $state.displayXgridLines,
  438. displayYgridLines: $state.displayYgridLines,
  439. thresholdLines: $state.thresholdLines
  440. )
  441. }
  442. .padding(.bottom)
  443. .modal(for: .dataTable, from: self)
  444. }
  445. private func selectedProfile() -> (name: String, isOn: Bool) {
  446. var profileString = ""
  447. var display: Bool = false
  448. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  449. let indefinite = fetchedPercent.first?.indefinite ?? false
  450. let addedMinutes = Int(duration)
  451. let date = fetchedPercent.first?.date ?? Date()
  452. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  453. display.toggle()
  454. }
  455. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  456. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  457. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  458. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  459. } else {
  460. let id_ = fetchedPercent.first?.id ?? ""
  461. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  462. if profile != nil {
  463. profileString = profile?.name?.description ?? ""
  464. }
  465. }
  466. return (name: profileString, isOn: display)
  467. }
  468. func highlightButtons(_ int: Int?, onAppear: Bool) {
  469. var index = 0
  470. if let integer = int, !onAppear {
  471. repeat {
  472. if index == integer {
  473. timeButtons[index].active = true
  474. } else {
  475. timeButtons[index].active = false
  476. }
  477. index += 1
  478. } while index < timeButtons.count
  479. } else if onAppear {
  480. let i = timeButtons.firstIndex(where: { $0.hours == (fetchedSettings.first?.hours ?? 6) }) ?? 2
  481. index = 0
  482. repeat {
  483. if index == i {
  484. timeButtons[index].active = true
  485. } else {
  486. timeButtons[index].active = false
  487. }
  488. index += 1
  489. } while index < timeButtons.count
  490. }
  491. }
  492. @ViewBuilder private func bottomPanel(_: GeometryProxy) -> some View {
  493. let colorRectangle: Color = colorScheme == .dark ? Color.black.opacity(0.8) : Color.white
  494. let colorIcon: Color = (colorScheme == .dark ? Color.white : Color.black).opacity(0.9)
  495. ZStack {
  496. Rectangle()
  497. .fill(colorRectangle)
  498. .frame(height: UIScreen.main.bounds.height / 13)
  499. .cornerRadius(15)
  500. .shadow(
  501. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) : Color
  502. .black.opacity(0.33),
  503. radius: 3
  504. )
  505. .padding([.leading, .trailing], 10)
  506. HStack {
  507. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  508. label: {
  509. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  510. Image("carbs")
  511. .renderingMode(.template)
  512. .resizable()
  513. .frame(width: 24, height: 24)
  514. .foregroundColor(colorIcon)
  515. .padding(8)
  516. if let carbsReq = state.carbsRequired {
  517. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  518. .font(.caption)
  519. .foregroundColor(.white)
  520. .padding(4)
  521. .background(Capsule().fill(Color.red))
  522. }
  523. }
  524. }.buttonStyle(.borderless)
  525. Spacer()
  526. Button { state.showModal(for: .addTempTarget) }
  527. label: {
  528. Image("target")
  529. .renderingMode(.template)
  530. .resizable()
  531. .frame(width: 24, height: 24)
  532. .padding(8)
  533. }
  534. .foregroundColor(colorIcon)
  535. .buttonStyle(.borderless)
  536. Spacer()
  537. Button {
  538. state.showModal(for: .bolus(
  539. waitForSuggestion: true,
  540. fetch: false
  541. ))
  542. }
  543. label: {
  544. Image("bolus")
  545. .renderingMode(.template)
  546. .resizable()
  547. .frame(width: 24, height: 24)
  548. .padding(8)
  549. }
  550. .foregroundColor(colorIcon)
  551. .buttonStyle(.borderless)
  552. Spacer()
  553. if state.allowManualTemp {
  554. Button { state.showModal(for: .manualTempBasal) }
  555. label: {
  556. Image("bolus1")
  557. .renderingMode(.template)
  558. .resizable()
  559. .frame(width: 24, height: 24)
  560. .padding(8)
  561. }
  562. .foregroundColor(colorIcon)
  563. .buttonStyle(.borderless)
  564. Spacer()
  565. }
  566. // MARK: CANCEL OF PROFILE HAS TO BE IMPLEMENTED
  567. // MAYBE WITH A SMALL INDICATOR AT THE SYMBOL
  568. Button {
  569. state.showModal(for: .overrideProfilesConfig)
  570. } label: {
  571. Image(systemName: "person")
  572. .renderingMode(.template)
  573. .resizable()
  574. .frame(width: 24, height: 24)
  575. .padding(8)
  576. }
  577. .foregroundColor(colorIcon)
  578. .buttonStyle(.borderless)
  579. Spacer()
  580. Button { state.showModal(for: .statistics)
  581. }
  582. label: {
  583. Image(systemName: "chart.xyaxis.line")
  584. .renderingMode(.template)
  585. .resizable()
  586. .frame(width: 24, height: 24)
  587. .padding(8)
  588. }
  589. .foregroundColor(colorIcon)
  590. .buttonStyle(.borderless)
  591. Spacer()
  592. Button { state.showModal(for: .settings) }
  593. label: {
  594. Image("settings1")
  595. .renderingMode(.template)
  596. .resizable()
  597. .frame(width: 24, height: 24)
  598. .padding(8)
  599. }
  600. .foregroundColor(colorIcon)
  601. .buttonStyle(.borderless)
  602. }
  603. .padding(.horizontal, 24)
  604. .padding(.bottom, 16)
  605. }
  606. }
  607. var body: some View {
  608. let colorBackground = colorScheme == .dark ? LinearGradient(
  609. gradient: Gradient(colors: [
  610. // RGB(3, 15, 28)
  611. Color(red: 0.011, green: 0.058, blue: 0.109),
  612. Color(red: 0.011, green: 0.058, blue: 0.109),
  613. // RGB(1, 3, 8)
  614. Color(red: 0.003, green: 0.011, blue: 0.031)
  615. ]),
  616. startPoint: .bottom,
  617. endPoint: .top
  618. )
  619. :
  620. LinearGradient(gradient: Gradient(colors: [Color.gray.opacity(0.1)]), startPoint: .top, endPoint: .bottom)
  621. let colourChart: Color = colorScheme == .dark ? .black.opacity(0.8) : .white
  622. GeometryReader { geo in
  623. VStack(spacing: 0) {
  624. Spacer()
  625. // ZStack {
  626. glucoseView.padding(.top, 75)
  627. // loopView
  628. // /// circles width is 110, loops width is 35 -> (110/2) - (35/2) = 55 - 17.5 = 37.5
  629. // .offset(x: UIScreen.main.bounds.width * 0.43, y: -37.5)
  630. // .padding(.trailing, 10)
  631. // }
  632. // .padding(.top, 75)
  633. Spacer()
  634. header(geo)
  635. .padding(.top, 15)
  636. .padding(.horizontal, 10)
  637. Spacer()
  638. infoPanel
  639. .padding(.horizontal, 10)
  640. RoundedRectangle(cornerRadius: 15)
  641. .fill(colourChart)
  642. .overlay(mainChart)
  643. .clipShape(RoundedRectangle(cornerRadius: 15))
  644. .shadow(
  645. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  646. Color.black.opacity(0.33),
  647. radius: 3
  648. )
  649. .padding(.horizontal, 10)
  650. .frame(maxHeight: UIScreen.main.bounds.height / 2.2)
  651. Spacer()
  652. timeInterval
  653. legendPanel
  654. Spacer()
  655. bottomPanel(geo)
  656. }
  657. .background(colorBackground)
  658. .edgesIgnoringSafeArea(.all)
  659. }
  660. .onAppear(perform: configureView)
  661. .navigationTitle("Home")
  662. .navigationBarHidden(true)
  663. .ignoresSafeArea(.keyboard)
  664. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  665. popup
  666. .padding()
  667. .background(
  668. RoundedRectangle(cornerRadius: 8, style: .continuous)
  669. .fill(Color(UIColor.darkGray))
  670. )
  671. .onTapGesture {
  672. isStatusPopupPresented = false
  673. }
  674. .gesture(
  675. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  676. .onEnded { value in
  677. if value.translation.height < 0 {
  678. isStatusPopupPresented = false
  679. }
  680. }
  681. )
  682. }
  683. }
  684. private var popup: some View {
  685. VStack(alignment: .leading, spacing: 4) {
  686. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  687. .padding(.bottom, 4)
  688. if let suggestion = state.suggestion {
  689. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  690. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  691. } else {
  692. Text("No sugestion found").font(.body).foregroundColor(.white)
  693. }
  694. if let errorMessage = state.errorMessage, let date = state.errorDate {
  695. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  696. .foregroundColor(.white)
  697. .font(.headline)
  698. .padding(.bottom, 4)
  699. .padding(.top, 8)
  700. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  701. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  702. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  703. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  704. }
  705. }
  706. }
  707. }
  708. }