HomeRootView.swift 32 KB

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