TreatmentsRootView.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. import Charts
  2. import CoreData
  3. import LoopKitUI
  4. import SwiftUI
  5. import Swinject
  6. extension Treatments {
  7. struct RootView: BaseView {
  8. enum FocusedField {
  9. case carbs
  10. case fat
  11. case protein
  12. case bolus
  13. }
  14. @FocusState private var focusedField: FocusedField?
  15. let resolver: Resolver
  16. @State var state = StateModel()
  17. @State private var showPresetSheet = false
  18. @State private var autofocus: Bool = true
  19. @State private var calculatorDetent = PresentationDetent.large
  20. @State private var pushed: Bool = false
  21. @State private var debounce: DispatchWorkItem?
  22. @State private var showFatProteinOrderBanner = false
  23. private enum Config {
  24. static let dividerHeight: CGFloat = 2
  25. static let spacing: CGFloat = 3
  26. }
  27. @Environment(\.colorScheme) var colorScheme
  28. @Environment(AppState.self) var appState
  29. private var formatter: NumberFormatter {
  30. let formatter = NumberFormatter()
  31. formatter.numberStyle = .decimal
  32. formatter.maximumIntegerDigits = 2
  33. formatter.maximumFractionDigits = 3
  34. return formatter
  35. }
  36. private var bolusProgressFormatter: NumberFormatter {
  37. let fractionDigits: Int = switch state.settingsManager.preferences.bolusIncrement {
  38. case 0.1: 1
  39. case 0.025: 3
  40. default: 2
  41. }
  42. let formatter = NumberFormatter()
  43. formatter.numberStyle = .decimal
  44. formatter.minimum = 0
  45. formatter.maximumFractionDigits = fractionDigits
  46. formatter.minimumFractionDigits = fractionDigits
  47. formatter.allowsFloats = true
  48. formatter.roundingIncrement = Double(state.settingsManager.preferences.bolusIncrement) as NSNumber
  49. return formatter
  50. }
  51. private var mealFormatter: NumberFormatter {
  52. let formatter = NumberFormatter()
  53. formatter.numberStyle = .decimal
  54. formatter.maximumIntegerDigits = 3
  55. formatter.maximumFractionDigits = 0
  56. return formatter
  57. }
  58. private var gluoseFormatter: NumberFormatter {
  59. let formatter = NumberFormatter()
  60. formatter.numberStyle = .decimal
  61. if state.units == .mmolL {
  62. formatter.maximumIntegerDigits = 2
  63. formatter.maximumFractionDigits = 1
  64. } else {
  65. formatter.maximumIntegerDigits = 3
  66. formatter.maximumFractionDigits = 0
  67. }
  68. return formatter
  69. }
  70. private var fractionDigits: Int {
  71. if state.units == .mmolL {
  72. return 1
  73. } else { return 0 }
  74. }
  75. /// Handles macro input (carb, fat, protein) in a debounced fashion.
  76. func handleDebouncedInput() {
  77. debounce?.cancel()
  78. debounce = DispatchWorkItem { [self] in
  79. Task {
  80. await state.updateForecasts()
  81. state.insulinCalculated = await state.calculateInsulin()
  82. }
  83. }
  84. if let debounce = debounce {
  85. DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
  86. }
  87. }
  88. @ViewBuilder private func proteinAndFat() -> some View {
  89. HStack {
  90. HStack {
  91. Text("Fat")
  92. TextFieldWithToolBar(
  93. text: $state.fat,
  94. placeholder: "0",
  95. keyboardType: .numberPad,
  96. numberFormatter: mealFormatter,
  97. showArrows: true,
  98. previousTextField: { focusedField = previousField(from: .fat) },
  99. nextTextField: { focusedField = nextField(from: .fat) },
  100. unitsText: String(localized: "g", comment: "Units for carbs")
  101. )
  102. .focused($focusedField, equals: .fat)
  103. }
  104. Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  105. HStack {
  106. Text("Protein")
  107. TextFieldWithToolBar(
  108. text: $state.protein,
  109. placeholder: "0",
  110. keyboardType: .numberPad,
  111. numberFormatter: mealFormatter,
  112. showArrows: true,
  113. previousTextField: { focusedField = previousField(from: .protein) },
  114. nextTextField: { focusedField = nextField(from: .protein) },
  115. unitsText: String(localized: "g", comment: "Units for carbs")
  116. )
  117. .focused($focusedField, equals: .protein)
  118. }
  119. }
  120. }
  121. @ViewBuilder private func carbsTextField() -> some View {
  122. HStack {
  123. Text("Carbs")
  124. Spacer()
  125. TextFieldWithToolBar(
  126. text: $state.carbs,
  127. placeholder: "0",
  128. keyboardType: .numberPad,
  129. numberFormatter: mealFormatter,
  130. showArrows: true,
  131. previousTextField: { focusedField = previousField(from: .carbs) },
  132. nextTextField: { focusedField = nextField(from: .carbs) },
  133. unitsText: String(localized: "g", comment: "Units for carbs")
  134. )
  135. .focused($focusedField, equals: .carbs)
  136. .onChange(of: state.carbs) {
  137. handleDebouncedInput()
  138. }
  139. }
  140. }
  141. /// Determines the next field to focus on based on the current focused field.
  142. ///
  143. /// This function handles the tab order navigation between input fields,
  144. /// taking into account whether fat/protein fields are visible based on user settings.
  145. ///
  146. /// - Parameter current: The currently focused field
  147. /// - Returns: The next field that should receive focus, or nil if there is no next field
  148. private func nextField(from current: FocusedField) -> FocusedField? {
  149. // If fat/protein fields are hidden, skip them in navigation
  150. let showFPU = state.useFPUconversion
  151. switch current {
  152. case .fat:
  153. return .bolus
  154. case .protein:
  155. return .fat
  156. case .carbs:
  157. return showFPU ? .protein : .bolus
  158. case .bolus:
  159. return .carbs
  160. }
  161. }
  162. /// Determines the previous field to focus on based on the current focused field.
  163. ///
  164. /// This function handles the reverse tab order navigation between input fields,
  165. /// taking into account whether fat/protein fields are visible based on user settings.
  166. ///
  167. /// - Parameter current: The currently focused field
  168. /// - Returns: The previous field that should receive focus, or nil if there is no previous field
  169. private func previousField(from current: FocusedField) -> FocusedField? {
  170. let showFPU = state.useFPUconversion
  171. switch current {
  172. case .fat:
  173. return .protein
  174. case .protein:
  175. return .carbs
  176. case .carbs:
  177. return .bolus
  178. case .bolus:
  179. return showFPU ? .fat : .carbs
  180. }
  181. }
  182. var body: some View {
  183. ZStack(alignment: .center) {
  184. VStack {
  185. List {
  186. Section {
  187. ForecastChart(state: state)
  188. .padding(.vertical)
  189. }.listRowBackground(Color.chart)
  190. Section {
  191. carbsTextField()
  192. if state.useFPUconversion {
  193. proteinAndFat()
  194. if showFatProteinOrderBanner {
  195. HStack {
  196. Image(systemName: "arrow.left.arrow.right")
  197. Text("The order of Fat and Protein inputs has changed.").font(.callout)
  198. Spacer()
  199. Button {
  200. PropertyPersistentFlags.shared.hasSeenFatProteinOrderChange = true
  201. withAnimation { showFatProteinOrderBanner = false }
  202. } label: {
  203. Image(systemName: "xmark.circle.fill")
  204. }
  205. .buttonStyle(.plain)
  206. }
  207. .listRowBackground(Color.orange.opacity(0.75))
  208. .transition(.opacity)
  209. }
  210. }
  211. // Time
  212. HStack {
  213. // Semi-hacky workaround to make sure the List renders the horizontal divider properly between the `Time` and `Note` rows within the Section
  214. HStack {
  215. Text("")
  216. Image(systemName: "clock").padding(.leading, -7)
  217. }
  218. Spacer()
  219. if !pushed {
  220. Button {
  221. pushed = true
  222. } label: { Text("Now") }.buttonStyle(.borderless).foregroundColor(.secondary)
  223. .padding(.trailing, 5)
  224. } else {
  225. Button { state.date = state.date.addingTimeInterval(-15.minutes.timeInterval) }
  226. label: { Image(systemName: "minus.circle") }.tint(.blue).buttonStyle(.borderless)
  227. DatePicker(
  228. "Time",
  229. selection: $state.date,
  230. displayedComponents: [.hourAndMinute]
  231. ).controlSize(.mini)
  232. .labelsHidden()
  233. .onChange(of: state.date) { _, _ in
  234. // Trigger simulation when date changes to update forecasts for backdated carbs
  235. Task {
  236. // `updateForecasts()` does update the `simulatedDetermination` of type `Determination?` var on the main thread, so I can use this to pass its cob value into the bolus calc manager
  237. await state.updateForecasts()
  238. state.insulinCalculated = await state.calculateInsulin()
  239. }
  240. }
  241. Button {
  242. state.date = state.date.addingTimeInterval(15.minutes.timeInterval)
  243. }
  244. label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
  245. }
  246. }
  247. // Notes
  248. HStack {
  249. Image(systemName: "square.and.pencil")
  250. TextFieldWithToolBarString(
  251. text: $state.note,
  252. placeholder: String(localized: "Note..."),
  253. maxLength: 25
  254. )
  255. }
  256. }.listRowBackground(Color.chart)
  257. Section {
  258. if state.fattyMeals || state.sweetMeals {
  259. HStack(spacing: 10) {
  260. if state.fattyMeals {
  261. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  262. Text("Reduced Bolus")
  263. }
  264. .toggleStyle(RadioButtonToggleStyle())
  265. .font(.footnote)
  266. .onChange(of: state.useFattyMealCorrectionFactor) {
  267. Task {
  268. state.insulinCalculated = await state.calculateInsulin()
  269. if state.useFattyMealCorrectionFactor {
  270. state.useSuperBolus = false
  271. }
  272. }
  273. }
  274. }
  275. if state.sweetMeals {
  276. Toggle(isOn: $state.useSuperBolus) {
  277. Text("Super Bolus")
  278. }
  279. .toggleStyle(RadioButtonToggleStyle())
  280. .font(.footnote)
  281. .onChange(of: state.useSuperBolus) {
  282. Task {
  283. state.insulinCalculated = await state.calculateInsulin()
  284. if state.useSuperBolus {
  285. state.useFattyMealCorrectionFactor = false
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. HStack {
  293. HStack {
  294. Text("Recommendation")
  295. Button(action: {
  296. state.showInfo.toggle()
  297. }, label: {
  298. Image(systemName: "info.circle")
  299. })
  300. .foregroundStyle(.blue)
  301. .buttonStyle(PlainButtonStyle())
  302. }
  303. Spacer()
  304. Button {
  305. state.amount = state.insulinCalculated
  306. } label: {
  307. HStack {
  308. Text(
  309. formatter
  310. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  311. )
  312. Text(
  313. String(
  314. localized:
  315. " U",
  316. comment: "Unit in number of units delivered (keep the space character!)"
  317. )
  318. ).foregroundColor(.secondary)
  319. }
  320. }
  321. .disabled(state.insulinCalculated == 0 || state.amount == state.insulinCalculated)
  322. .buttonStyle(.bordered).padding(.trailing, -10)
  323. }
  324. HStack {
  325. Text("Bolus")
  326. Spacer()
  327. TextFieldWithToolBar(
  328. text: $state.amount,
  329. placeholder: "0",
  330. textColor: colorScheme == .dark ? .white : .blue,
  331. maxLength: 5,
  332. numberFormatter: formatter,
  333. showArrows: true,
  334. previousTextField: { focusedField = previousField(from: .bolus) },
  335. nextTextField: { focusedField = nextField(from: .bolus) },
  336. unitsText: String(localized: "U", comment: "Units for bolus amount")
  337. ).focused($focusedField, equals: .bolus)
  338. .onChange(of: state.amount) {
  339. Task {
  340. await state.updateForecasts()
  341. }
  342. }
  343. }
  344. HStack {
  345. Text("External Insulin")
  346. Spacer()
  347. Toggle("", isOn: $state.externalInsulin).toggleStyle(CheckboxToggleStyle())
  348. }
  349. }.listRowBackground(Color.chart)
  350. treatmentButton
  351. }
  352. .listSectionSpacing(sectionSpacing)
  353. }
  354. .blur(radius: state.isAwaitingDeterminationResult ? 5 : 0)
  355. if state.isAwaitingDeterminationResult {
  356. CustomProgressView(text: progressText.displayName)
  357. }
  358. }
  359. .padding(.top)
  360. .ignoresSafeArea(edges: .top)
  361. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  362. .blur(radius: state.showInfo ? 3 : 0)
  363. .navigationTitle("Treatments")
  364. .navigationBarTitleDisplayMode(.inline)
  365. .toolbar(content: {
  366. ToolbarItem(placement: .topBarLeading) {
  367. Button {
  368. state.hideModal()
  369. } label: {
  370. Text("Close")
  371. }
  372. }
  373. if state.displayPresets {
  374. ToolbarItem(placement: .topBarTrailing) {
  375. Button(action: {
  376. showPresetSheet = true
  377. }, label: {
  378. HStack {
  379. Text("Presets")
  380. Image(systemName: "plus")
  381. }
  382. })
  383. }
  384. }
  385. })
  386. .onAppear {
  387. configureView {
  388. state.isActive = true
  389. Task { @MainActor in
  390. state.insulinCalculated = await state.calculateInsulin()
  391. }
  392. if PropertyPersistentFlags.shared.hasSeenFatProteinOrderChange != true {
  393. showFatProteinOrderBanner = true
  394. }
  395. }
  396. }
  397. .onDisappear {
  398. state.isActive = false
  399. state.addButtonPressed = false
  400. // Cancel all Combine subscriptions and unregister State from broadcaster
  401. state.cleanupTreatmentState()
  402. }
  403. .sheet(isPresented: $state.showInfo) {
  404. PopupView(state: state)
  405. }
  406. .sheet(isPresented: $showPresetSheet, onDismiss: {
  407. showPresetSheet = false
  408. }) {
  409. MealPresetView(state: state)
  410. }
  411. .alert("Error while processing Treatment", isPresented: $state.showDeterminationFailureAlert) {
  412. Button("OK", role: .cancel) {
  413. state.hideModal()
  414. }
  415. } message: {
  416. Text("\(state.determinationFailureMessage)")
  417. }
  418. }
  419. var progressText: ProgressText {
  420. switch (state.amount > 0, state.carbs > 0) {
  421. case (true, true):
  422. return .updatingIOBandCOB
  423. case (false, true):
  424. return .updatingCOB
  425. case (true, false):
  426. return .updatingIOB
  427. default:
  428. return .updatingTreatments
  429. }
  430. }
  431. @State private var showConfirmDialogForBolusing = false
  432. private var bolusWarning: (shouldConfirm: Bool, warningMessage: String, color: Color) {
  433. let isGlucoseVeryLow = state.currentBG < 54
  434. let isForecastVeryLow = state.minPredBG < 54
  435. // Only warn when enacting a bolus via pump
  436. guard !state.externalInsulin, state.amount > 0 else {
  437. return (false, "", .primary)
  438. }
  439. let warningMessage = isGlucoseVeryLow ? String(localized: "Glucose is very low.") :
  440. isForecastVeryLow ? String(localized: "Glucose forecast is very low.") :
  441. ""
  442. let warningColor: Color = isGlucoseVeryLow ? .red : colorScheme == .dark ? .orange : .accentColor
  443. let shouldConfirm = state.confirmBolus && (isGlucoseVeryLow || isForecastVeryLow)
  444. return (shouldConfirm, warningMessage, warningColor)
  445. }
  446. var treatmentButton: some View {
  447. let shouldDisplayBolusProgress = state.isBolusInProgress && state.amount > 0 &&
  448. !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
  449. var treatmentButtonBackground = Color(.systemBlue)
  450. if limitExceeded {
  451. treatmentButtonBackground = Color(.systemRed)
  452. } else if disableTaskButton {
  453. treatmentButtonBackground = Color(.systemGray)
  454. }
  455. return Section {
  456. if shouldDisplayBolusProgress {
  457. bolusInProgressView
  458. .listRowBackground(Color.clear)
  459. .listRowInsets(EdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0))
  460. } else {
  461. Button {
  462. if bolusWarning.shouldConfirm {
  463. showConfirmDialogForBolusing = true
  464. } else {
  465. state.invokeTreatmentsTask()
  466. }
  467. } label: {
  468. HStack {
  469. taskButtonLabel
  470. }
  471. .font(.headline)
  472. .foregroundStyle(Color.white)
  473. .frame(maxWidth: .infinity, alignment: .center)
  474. .frame(height: 35)
  475. }
  476. .disabled(disableTaskButton)
  477. .listRowBackground(treatmentButtonBackground)
  478. .shadow(radius: 3)
  479. .clipShape(RoundedRectangle(cornerRadius: 8))
  480. .confirmationDialog(
  481. bolusWarning.warningMessage + " Bolus \(state.amount.description) U?",
  482. isPresented: $showConfirmDialogForBolusing,
  483. titleVisibility: .visible
  484. ) {
  485. Button("Cancel", role: .cancel) {}
  486. Button(
  487. bolusWarning.warningMessage
  488. .isEmpty ? String(localized: "Enact Bolus") : String(localized: "Ignore Warning and Enact Bolus"),
  489. role: bolusWarning.warningMessage.isEmpty ? nil : .destructive
  490. ) {
  491. state.invokeTreatmentsTask()
  492. }
  493. }
  494. }
  495. } header: {
  496. if !bolusWarning.warningMessage.isEmpty {
  497. Text(bolusWarning.warningMessage)
  498. .textCase(nil)
  499. .font(.subheadline)
  500. .foregroundColor(bolusWarning.color)
  501. .frame(maxWidth: .infinity, alignment: .center)
  502. .padding(.top, -22)
  503. }
  504. }
  505. }
  506. /// Card-style in-progress visualizer matching Home's `bolusView` look:
  507. /// insulin-tinted background, cross.vial.fill icon, "Bolusing" + "X of Y U" text,
  508. /// xmark.app cancel, gradient progress bar overlaid at the bottom.
  509. @ViewBuilder private var bolusInProgressView: some View {
  510. let progress = state.bolusProgress ?? 0
  511. let bolusTotal = state.lastPumpBolus?.bolus?.amount as Decimal?
  512. let bolusFraction = (bolusTotal ?? 0) * progress
  513. let bolusString: String = {
  514. guard let bolusTotal = bolusTotal else { return String(localized: "Bolus In Progress...") }
  515. return (bolusProgressFormatter.string(from: bolusFraction as NSNumber) ?? "0")
  516. + String(localized: " of ", comment: "Bolus string partial message: 'x U of y U' in home view")
  517. + (Formatter.decimalFormatterWithThreeFractionDigits.string(from: bolusTotal as NSNumber) ?? "0")
  518. + String(localized: " U", comment: "Insulin unit")
  519. }()
  520. ZStack {
  521. // background card
  522. RoundedRectangle(cornerRadius: 15)
  523. .fill(
  524. colorScheme == .dark
  525. ? Color(red: 0.03921568627, green: 0.133333333, blue: 0.2156862745)
  526. : Color.insulin.opacity(0.2)
  527. )
  528. .frame(height: 56)
  529. .shadow(
  530. color: colorScheme == .dark
  531. ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706)
  532. : Color.black.opacity(0.33),
  533. radius: 3
  534. )
  535. // bolus content
  536. HStack {
  537. Image(systemName: "cross.vial.fill")
  538. .font(.system(size: 25))
  539. Spacer()
  540. VStack {
  541. Text("Bolusing")
  542. .font(.subheadline)
  543. .frame(maxWidth: .infinity, alignment: .leading)
  544. Text(bolusString)
  545. .font(.caption)
  546. .frame(maxWidth: .infinity, alignment: .leading)
  547. }
  548. .padding(.leading, 5)
  549. Spacer()
  550. Button { state.cancelBolus() } label: {
  551. Image(systemName: "xmark.app")
  552. .font(.system(size: 25))
  553. }.tint(Color.tabBar)
  554. .buttonStyle(.borderless)
  555. .accessibilityLabel("Cancel bolus")
  556. }
  557. .padding(.horizontal, 10)
  558. .padding(.trailing, 8)
  559. }
  560. .padding(.horizontal, 10)
  561. .overlay(alignment: .bottom) {
  562. BolusProgressBar(progress: progress)
  563. .padding(.horizontal, 18)
  564. .padding(.bottom, 1)
  565. }
  566. .clipShape(RoundedRectangle(cornerRadius: 15))
  567. }
  568. private var taskButtonLabel: some View {
  569. if pumpBolusLimitExceeded {
  570. return Text("Max Bolus of \(state.maxBolus.description) U Exceeded")
  571. } else if externalBolusLimitExceeded {
  572. return Text("Max External Bolus of \(state.maxExternal.description) U Exceeded")
  573. } else if carbLimitExceeded {
  574. return Text("Max Carbs of \(state.maxCarbs.description) g Exceeded")
  575. } else if fatLimitExceeded {
  576. return Text("Max Fat of \(state.maxFat.description) g Exceeded")
  577. } else if proteinLimitExceeded {
  578. return Text("Max Protein of \(state.maxProtein.description) g Exceeded")
  579. }
  580. let hasInsulin = state.amount > 0
  581. let hasCarbs = state.carbs > 0
  582. let hasFatOrProtein = state.fat > 0 || state.protein > 0
  583. let bolusString = state.externalInsulin ? String(localized: "External Insulin") : String(localized: "Enact Bolus")
  584. // Note: when a pump bolus is in progress, the row is rendered by `bolusInProgressView`
  585. // (Home-style card), so this label's in-progress branch is intentionally absent.
  586. switch (hasInsulin, hasCarbs, hasFatOrProtein) {
  587. case (true, true, true):
  588. return Text("Log Meal and \(bolusString)")
  589. case (true, true, false):
  590. return Text("Log Carbs and \(bolusString)")
  591. case (true, false, true):
  592. return Text("Log FPU and \(bolusString)")
  593. case (true, false, false):
  594. return Text(state.externalInsulin ? String(localized: "Log External Insulin") : String(localized: "Enact Bolus"))
  595. case (false, true, true):
  596. return Text("Log Meal")
  597. case (false, true, false):
  598. return Text("Log Carbs")
  599. case (false, false, true):
  600. return Text("Log FPU")
  601. default:
  602. return Text("Continue Without Treatment")
  603. }
  604. }
  605. private var pumpBolusLimitExceeded: Bool {
  606. !state.externalInsulin && state.amount > state.maxBolus
  607. }
  608. private var externalBolusLimitExceeded: Bool {
  609. state.externalInsulin && state.amount > state.maxExternal
  610. }
  611. private var carbLimitExceeded: Bool {
  612. state.carbs > state.maxCarbs
  613. }
  614. private var fatLimitExceeded: Bool {
  615. state.fat > state.maxFat
  616. }
  617. private var proteinLimitExceeded: Bool {
  618. state.protein > state.maxProtein
  619. }
  620. private var limitExceeded: Bool {
  621. pumpBolusLimitExceeded || externalBolusLimitExceeded || carbLimitExceeded || fatLimitExceeded || proteinLimitExceeded
  622. }
  623. private var disableTaskButton: Bool {
  624. (
  625. state.isBolusInProgress && state
  626. .amount > 0 && !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
  627. ) || state
  628. .addButtonPressed || limitExceeded
  629. }
  630. }
  631. struct DividerDouble: View {
  632. var body: some View {
  633. VStack(spacing: 2) {
  634. Rectangle()
  635. .frame(height: 1)
  636. .foregroundColor(.gray.opacity(0.65))
  637. Rectangle()
  638. .frame(height: 1)
  639. .foregroundColor(.gray.opacity(0.65))
  640. }
  641. .frame(height: 4)
  642. .padding(.vertical)
  643. }
  644. }
  645. struct DividerCustom: View {
  646. var body: some View {
  647. Rectangle()
  648. .frame(height: 1)
  649. .foregroundColor(.gray.opacity(0.65))
  650. .padding(.vertical)
  651. }
  652. }
  653. }