HomeRootView.swift 31 KB

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