AlternativeBolusCalcRootView.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. import Charts
  2. import CoreData
  3. import SwiftUI
  4. import Swinject
  5. extension Bolus {
  6. struct AlternativeBolusCalcRootView: BaseView {
  7. let resolver: Resolver
  8. let waitForSuggestion: Bool
  9. let fetch: Bool
  10. @StateObject var state: StateModel
  11. @State private var showInfo = false
  12. @State private var exceededMaxBolus = false
  13. @State private var keepForNextWiew: Bool = false
  14. @State private var calculatorDetent = PresentationDetent.medium
  15. private enum Config {
  16. static let dividerHeight: CGFloat = 2
  17. static let spacing: CGFloat = 3
  18. }
  19. @Environment(\.colorScheme) var colorScheme
  20. @FetchRequest(
  21. entity: Meals.entity(),
  22. sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
  23. ) var meal: FetchedResults<Meals>
  24. private var formatter: NumberFormatter {
  25. let formatter = NumberFormatter()
  26. formatter.numberStyle = .decimal
  27. formatter.maximumFractionDigits = 2
  28. return formatter
  29. }
  30. private var mealFormatter: NumberFormatter {
  31. let formatter = NumberFormatter()
  32. formatter.numberStyle = .decimal
  33. formatter.maximumFractionDigits = 1
  34. return formatter
  35. }
  36. private var gluoseFormatter: NumberFormatter {
  37. let formatter = NumberFormatter()
  38. formatter.numberStyle = .decimal
  39. if state.units == .mmolL {
  40. formatter.maximumFractionDigits = 1
  41. } else { formatter.maximumFractionDigits = 0 }
  42. return formatter
  43. }
  44. private var fractionDigits: Int {
  45. if state.units == .mmolL {
  46. return 1
  47. } else { return 0 }
  48. }
  49. private var color: LinearGradient {
  50. colorScheme == .dark ? LinearGradient(
  51. gradient: Gradient(colors: [
  52. Color("Background_1"),
  53. Color("Background_1"),
  54. Color("Background_2")
  55. // Color("Background_1")
  56. ]),
  57. startPoint: .top,
  58. endPoint: .bottom
  59. )
  60. :
  61. LinearGradient(
  62. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  63. startPoint: .top,
  64. endPoint: .bottom
  65. )
  66. }
  67. var body: some View {
  68. Form {
  69. Section {
  70. if state.waitForSuggestion {
  71. Text("Please wait")
  72. } else {
  73. predictionChart
  74. }
  75. } header: { Text("Predictions") }
  76. if fetch {
  77. Section {
  78. mealEntries
  79. } header: { Text("Meal Summary") }
  80. }
  81. Section {
  82. HStack {
  83. Button(action: {
  84. showInfo.toggle()
  85. }, label: {
  86. Image(systemName: "info.circle")
  87. Text("Calculations")
  88. })
  89. .foregroundStyle(.blue)
  90. .font(.footnote)
  91. .buttonStyle(PlainButtonStyle())
  92. .frame(maxWidth: .infinity, alignment: .leading)
  93. if state.fattyMeals {
  94. Spacer()
  95. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  96. Text("Fatty Meal")
  97. }
  98. .toggleStyle(CheckboxToggleStyle())
  99. .font(.footnote)
  100. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  101. state.insulinCalculated = state.calculateInsulin()
  102. if state.useFattyMealCorrectionFactor {
  103. state.useSuperBolus = false
  104. }
  105. }
  106. }
  107. if state.sweetMeals {
  108. Spacer()
  109. Toggle(isOn: $state.useSuperBolus) {
  110. Text("Super Bolus")
  111. }
  112. .toggleStyle(CheckboxToggleStyle())
  113. .font(.footnote)
  114. .onChange(of: state.useSuperBolus) { _ in
  115. state.insulinCalculated = state.calculateInsulin()
  116. if state.useSuperBolus {
  117. state.useFattyMealCorrectionFactor = false
  118. }
  119. }
  120. }
  121. }
  122. if state.waitForSuggestion {
  123. HStack {
  124. Text("Wait please").foregroundColor(.secondary)
  125. Spacer()
  126. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  127. }
  128. } else {
  129. HStack {
  130. Text("Recommended Bolus")
  131. Spacer()
  132. Text(
  133. formatter
  134. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  135. )
  136. Text(
  137. NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
  138. ).foregroundColor(.secondary)
  139. }.contentShape(Rectangle())
  140. .onTapGesture { state.amount = state.insulinCalculated }
  141. }
  142. HStack {
  143. Text("Bolus")
  144. Spacer()
  145. DecimalTextField(
  146. "0",
  147. value: $state.amount,
  148. formatter: formatter,
  149. autofocus: false,
  150. cleanInput: true
  151. )
  152. Text(exceededMaxBolus ? "😵" : " U").foregroundColor(.secondary)
  153. }
  154. .onChange(of: state.amount) { newValue in
  155. if newValue > state.maxBolus {
  156. exceededMaxBolus = true
  157. } else {
  158. exceededMaxBolus = false
  159. }
  160. }
  161. } header: { Text("Bolus") }
  162. if state.amount > 0 {
  163. Section {
  164. Button {
  165. keepForNextWiew = true
  166. state.add()
  167. }
  168. label: { Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus") }
  169. .frame(maxWidth: .infinity, alignment: .center)
  170. .disabled(disabled)
  171. .listRowBackground(!disabled ? Color(.systemBlue) : Color(.systemGray4))
  172. .tint(.white)
  173. }
  174. }
  175. if state.amount <= 0 {
  176. Section {
  177. Button {
  178. keepForNextWiew = true
  179. state.showModal(for: nil)
  180. }
  181. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  182. }
  183. }
  184. }.scrollContentBackground(.hidden).background(color)
  185. .blur(radius: showInfo ? 3 : 0)
  186. .navigationTitle("Enact Bolus")
  187. .navigationBarTitleDisplayMode(.inline)
  188. .navigationBarItems(
  189. leading: Button {
  190. carbsView()
  191. }
  192. label: {
  193. HStack {
  194. Image(systemName: "chevron.backward")
  195. Text("Meal")
  196. }
  197. },
  198. trailing: Button { state.hideModal() }
  199. label: { Text("Close") }
  200. )
  201. .onAppear {
  202. configureView {
  203. state.waitForSuggestionInitial = waitForSuggestion
  204. state.waitForSuggestion = waitForSuggestion
  205. state.insulinCalculated = state.calculateInsulin()
  206. }
  207. }
  208. .onDisappear {
  209. if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
  210. state.delete(deleteTwice: true, meal: meal)
  211. } else if fetch, !keepForNextWiew, state.useCalc {
  212. state.delete(deleteTwice: false, meal: meal)
  213. }
  214. }
  215. .sheet(isPresented: $showInfo) {
  216. calculationsDetailView
  217. .presentationDetents(
  218. [fetch ? .large : .fraction(0.9), .large],
  219. selection: $calculatorDetent
  220. )
  221. }
  222. }
  223. var predictionChart: some View {
  224. ZStack {
  225. PredictionView(
  226. predictions: $state.predictions, units: $state.units, eventualBG: $state.evBG, target: $state.target,
  227. displayPredictions: $state.displayPredictions
  228. )
  229. }
  230. }
  231. var calcSettingsFirstRow: some View {
  232. GridRow {
  233. Group {
  234. Text("Carb Ratio:")
  235. .foregroundColor(.secondary)
  236. }.gridCellAnchor(.leading)
  237. Group {
  238. Text("ISF:")
  239. .foregroundColor(.secondary)
  240. }.gridCellAnchor(.leading)
  241. VStack {
  242. Text("Target:")
  243. .foregroundColor(.secondary)
  244. }.gridCellAnchor(.leading)
  245. }
  246. }
  247. var calcSettingsSecondRow: some View {
  248. GridRow {
  249. Text(state.carbRatio.formatted() + " " + NSLocalizedString("g/U", comment: " grams per Unit"))
  250. .gridCellAnchor(.leading)
  251. Text(
  252. state.isf.formatted() + " " + state.units
  253. .rawValue + NSLocalizedString("/U", comment: "/Insulin unit")
  254. ).gridCellAnchor(.leading)
  255. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  256. Text(
  257. target
  258. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  259. " " + state.units.rawValue
  260. ).gridCellAnchor(.leading)
  261. }
  262. }
  263. var calcGlucoseFirstRow: some View {
  264. GridRow(alignment: .center) {
  265. let currentBG = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  266. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  267. Text("Glucose:").foregroundColor(.secondary)
  268. let firstRow = currentBG
  269. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  270. + " - " +
  271. target
  272. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  273. + " = " +
  274. state.targetDifference
  275. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  276. Text(firstRow).frame(minWidth: 0, alignment: .leading).foregroundColor(.secondary)
  277. .gridColumnAlignment(.leading)
  278. HStack {
  279. Text(
  280. self.insulinRounder(state.targetDifferenceInsulin).formatted()
  281. )
  282. Text("U").foregroundColor(.secondary)
  283. }.fontWeight(.bold)
  284. .gridColumnAlignment(.trailing)
  285. }
  286. }
  287. var calcGlucoseSecondRow: some View {
  288. GridRow(alignment: .center) {
  289. let currentBG = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  290. Text(
  291. currentBG
  292. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  293. " " +
  294. state.units.rawValue
  295. )
  296. let secondRow = state.targetDifference
  297. .formatted(
  298. .number.grouping(.never).rounded()
  299. .precision(.fractionLength(fractionDigits))
  300. )
  301. + " / " +
  302. state.isf.formatted()
  303. + " ≈ " +
  304. self.insulinRounder(state.targetDifferenceInsulin).formatted()
  305. Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
  306. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  307. }
  308. }
  309. var calcGlucoseFormulaRow: some View {
  310. GridRow(alignment: .top) {
  311. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  312. Text("(Current - Target) / ISF").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  313. .gridColumnAlignment(.leading)
  314. .gridCellColumns(2)
  315. }
  316. .font(.caption)
  317. }
  318. var calcIOBRow: some View {
  319. GridRow(alignment: .center) {
  320. HStack {
  321. Text("IOB:").foregroundColor(.secondary)
  322. Text(
  323. self.insulinRounder(state.iob).formatted()
  324. )
  325. }
  326. Text("Subtract IOB").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  327. let iobFormatted = self.insulinRounder(state.iob).formatted()
  328. HStack {
  329. Text((state.iob != 0 ? "-" : "") + (state.iob >= 0 ? iobFormatted : "(" + iobFormatted + ")"))
  330. Text("U").foregroundColor(.secondary)
  331. }.fontWeight(.bold)
  332. .gridColumnAlignment(.trailing)
  333. }
  334. }
  335. var calcCOBRow: some View {
  336. GridRow(alignment: .center) {
  337. HStack {
  338. Text("COB:").foregroundColor(.secondary)
  339. Text(
  340. state.cob
  341. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  342. NSLocalizedString(" g", comment: "grams")
  343. )
  344. }
  345. Text(
  346. state.cob
  347. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  348. + " / " +
  349. state.carbRatio.formatted()
  350. + " ≈ " +
  351. self.insulinRounder(state.wholeCobInsulin).formatted()
  352. )
  353. .foregroundColor(.secondary)
  354. .gridColumnAlignment(.leading)
  355. HStack {
  356. Text(
  357. self.insulinRounder(state.wholeCobInsulin).formatted()
  358. )
  359. Text("U").foregroundColor(.secondary)
  360. }.fontWeight(.bold)
  361. .gridColumnAlignment(.trailing)
  362. }
  363. }
  364. var calcCOBFormulaRow: some View {
  365. GridRow(alignment: .center) {
  366. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  367. Text("COB / Carb Ratio").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  368. .gridColumnAlignment(.leading)
  369. .gridCellColumns(2)
  370. }
  371. .font(.caption)
  372. }
  373. var calcDeltaRow: some View {
  374. GridRow(alignment: .center) {
  375. Text("Delta:").foregroundColor(.secondary)
  376. let deltaBG = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  377. Text(
  378. deltaBG
  379. .formatted(
  380. .number.grouping(.never).rounded()
  381. .precision(.fractionLength(fractionDigits))
  382. )
  383. + " / " +
  384. state.isf.formatted()
  385. + " ≈ " +
  386. self.insulinRounder(state.fifteenMinInsulin).formatted()
  387. )
  388. .foregroundColor(.secondary)
  389. .gridColumnAlignment(.leading)
  390. HStack {
  391. Text(
  392. self.insulinRounder(state.fifteenMinInsulin).formatted()
  393. )
  394. Text("U").foregroundColor(.secondary)
  395. }.fontWeight(.bold)
  396. .gridColumnAlignment(.trailing)
  397. }
  398. }
  399. var calcDeltaFormulaRow: some View {
  400. GridRow(alignment: .center) {
  401. let deltaBG = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  402. Text(
  403. deltaBG
  404. .formatted(
  405. .number.grouping(.never).rounded()
  406. .precision(.fractionLength(fractionDigits))
  407. ) + " " +
  408. state.units.rawValue
  409. )
  410. Text("15min Delta / ISF").font(.caption).foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  411. .gridColumnAlignment(.leading)
  412. .gridCellColumns(2).padding(.top, 5)
  413. }
  414. }
  415. var calcFullBolusRow: some View {
  416. GridRow(alignment: .center) {
  417. Text("Full Bolus")
  418. .foregroundColor(.secondary)
  419. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  420. HStack {
  421. Text(self.insulinRounder(state.wholeCalc).formatted())
  422. .foregroundStyle(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  423. Text("U").foregroundColor(.secondary)
  424. }.gridColumnAlignment(.trailing)
  425. .fontWeight(.bold)
  426. }
  427. }
  428. var calcSuperBolusRow: some View {
  429. GridRow(alignment: .center) {
  430. Text("Super Bolus")
  431. .foregroundColor(.secondary)
  432. Text("Added to Result").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  433. HStack {
  434. Text("+" + self.insulinRounder(state.superBolusInsulin).formatted())
  435. .foregroundStyle(Color.loopRed)
  436. Text("U").foregroundColor(.secondary)
  437. }.gridColumnAlignment(.trailing)
  438. .fontWeight(.bold)
  439. }
  440. }
  441. var calcResultRow: some View {
  442. GridRow(alignment: .center) {
  443. Text("Result").fontWeight(.bold)
  444. HStack {
  445. Text(state.useSuperBolus ? "(" : "")
  446. .foregroundColor(.loopRed)
  447. + Text(state.fraction.formatted())
  448. + Text(" x ")
  449. .foregroundColor(.secondary)
  450. // if fatty meal is chosen
  451. + Text(state.useFattyMealCorrectionFactor ? state.fattyMealFactor.formatted() : "")
  452. .foregroundColor(.orange)
  453. + Text(state.useFattyMealCorrectionFactor ? " x " : "")
  454. .foregroundColor(.secondary)
  455. // endif fatty meal is chosen
  456. + Text(self.insulinRounder(state.wholeCalc).formatted())
  457. .foregroundColor(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  458. // if superbolus is chosen
  459. + Text(state.useSuperBolus ? ")" : "")
  460. .foregroundColor(.loopRed)
  461. + Text(state.useSuperBolus ? " + " : "")
  462. .foregroundColor(.secondary)
  463. + Text(state.useSuperBolus ? state.superBolusInsulin.formatted() : "")
  464. .foregroundColor(.loopRed)
  465. // endif superbolus is chosen
  466. + Text(" ≈ ")
  467. .foregroundColor(.secondary)
  468. }
  469. .gridColumnAlignment(.leading)
  470. HStack {
  471. Text(self.insulinRounder(state.insulinCalculated).formatted())
  472. .fontWeight(.bold)
  473. .foregroundColor(.blue)
  474. Text("U").foregroundColor(.secondary)
  475. }
  476. .gridColumnAlignment(.trailing)
  477. .fontWeight(.bold)
  478. }
  479. }
  480. var calcResultFormulaRow: some View {
  481. GridRow(alignment: .bottom) {
  482. if state.useFattyMealCorrectionFactor {
  483. Text("Factor x Fatty Meal Factor x Full Bolus")
  484. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  485. .font(.caption)
  486. .gridCellAnchor(.center)
  487. .gridCellColumns(3)
  488. } else if state.useSuperBolus {
  489. Text("(Factor x Full Bolus) + Super Bolus")
  490. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  491. .font(.caption)
  492. .gridCellAnchor(.center)
  493. .gridCellColumns(3)
  494. } else {
  495. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  496. Text("Factor x Full Bolus")
  497. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  498. .font(.caption)
  499. .padding(.top, 5)
  500. .gridCellAnchor(.leading)
  501. .gridCellColumns(2)
  502. }
  503. }
  504. }
  505. var calculationsDetailView: some View {
  506. NavigationStack {
  507. ScrollView {
  508. Grid(alignment: .topLeading, horizontalSpacing: 3, verticalSpacing: 0) {
  509. GridRow {
  510. Text("Calculations").fontWeight(.bold).gridCellColumns(3).gridCellAnchor(.center).padding(.vertical)
  511. }
  512. calcSettingsFirstRow
  513. calcSettingsSecondRow
  514. DividerCustom()
  515. if fetch {
  516. // meal entries as grid rows
  517. GridRow {
  518. if let carbs = meal.first?.carbs, carbs > 0 {
  519. Text("Carbs").foregroundColor(.secondary)
  520. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  521. HStack {
  522. Text(carbs.formatted())
  523. Text("g").foregroundColor(.secondary)
  524. }.gridCellAnchor(.trailing)
  525. }
  526. }
  527. GridRow {
  528. if let fat = meal.first?.fat, fat > 0 {
  529. Text("Fat").foregroundColor(.secondary)
  530. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  531. HStack {
  532. Text(fat.formatted())
  533. Text("g").foregroundColor(.secondary)
  534. }.gridCellAnchor(.trailing)
  535. }
  536. }
  537. GridRow {
  538. if let protein = meal.first?.protein, protein > 0 {
  539. Text("Protein").foregroundColor(.secondary)
  540. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  541. HStack {
  542. Text(protein.formatted())
  543. Text("g").foregroundColor(.secondary)
  544. }.gridCellAnchor(.trailing)
  545. }
  546. }
  547. GridRow {
  548. if let note = meal.first?.note, note != "" {
  549. Text("Note").foregroundColor(.secondary)
  550. Text(note).foregroundColor(.secondary).gridCellColumns(2).gridCellAnchor(.trailing)
  551. }
  552. }
  553. DividerCustom()
  554. }
  555. GridRow {
  556. Text("Detailed Calculation Steps").gridCellColumns(3).gridCellAnchor(.center)
  557. .padding(.bottom, 10)
  558. }
  559. calcGlucoseFirstRow
  560. calcGlucoseSecondRow.padding(.bottom, 5)
  561. calcGlucoseFormulaRow
  562. DividerCustom()
  563. calcIOBRow
  564. DividerCustom()
  565. calcCOBRow.padding(.bottom, 5)
  566. calcCOBFormulaRow
  567. DividerCustom()
  568. calcDeltaRow
  569. calcDeltaFormulaRow
  570. DividerCustom()
  571. calcFullBolusRow
  572. if state.useSuperBolus {
  573. DividerCustom()
  574. calcSuperBolusRow
  575. }
  576. DividerDouble()
  577. calcResultRow
  578. calcResultFormulaRow
  579. }
  580. Spacer()
  581. Button { showInfo = false }
  582. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  583. .buttonStyle(.bordered)
  584. .padding(.top)
  585. }
  586. .padding([.horizontal, .bottom])
  587. .font(.system(size: 15))
  588. }
  589. }
  590. private func insulinRounder(_ value: Decimal) -> Decimal {
  591. let toRound = NSDecimalNumber(decimal: value).doubleValue
  592. return Decimal(floor(100 * toRound) / 100)
  593. }
  594. private var disabled: Bool {
  595. state.amount <= 0 || state.amount > state.maxBolus
  596. }
  597. var changed: Bool {
  598. ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  599. }
  600. var hasFatOrProtein: Bool {
  601. ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  602. }
  603. func carbsView() {
  604. if fetch {
  605. keepForNextWiew = true
  606. state.backToCarbsView(complexEntry: true, meal, override: false)
  607. } else {
  608. state.backToCarbsView(complexEntry: false, meal, override: true)
  609. }
  610. }
  611. var mealEntries: some View {
  612. VStack {
  613. if let carbs = meal.first?.carbs, carbs > 0 {
  614. HStack {
  615. Text("Carbs").foregroundColor(.secondary)
  616. Spacer()
  617. Text(carbs.formatted())
  618. Text("g").foregroundColor(.secondary)
  619. }
  620. }
  621. if let fat = meal.first?.fat, fat > 0 {
  622. HStack {
  623. Text("Fat").foregroundColor(.secondary)
  624. Spacer()
  625. Text(fat.formatted())
  626. Text("g").foregroundColor(.secondary)
  627. }
  628. }
  629. if let protein = meal.first?.protein, protein > 0 {
  630. HStack {
  631. Text("Protein").foregroundColor(.secondary)
  632. Spacer()
  633. Text(protein.formatted())
  634. Text("g").foregroundColor(.secondary)
  635. }
  636. }
  637. if let note = meal.first?.note, note != "" {
  638. HStack {
  639. Text("Note").foregroundColor(.secondary)
  640. Spacer()
  641. Text(note).foregroundColor(.secondary)
  642. }
  643. }
  644. }
  645. }
  646. }
  647. struct DividerDouble: View {
  648. var body: some View {
  649. VStack(spacing: 2) {
  650. Rectangle()
  651. .frame(height: 1)
  652. .foregroundColor(.gray.opacity(0.65))
  653. Rectangle()
  654. .frame(height: 1)
  655. .foregroundColor(.gray.opacity(0.65))
  656. }
  657. .frame(height: 4)
  658. .padding(.vertical)
  659. }
  660. }
  661. struct DividerCustom: View {
  662. var body: some View {
  663. Rectangle()
  664. .frame(height: 1)
  665. .foregroundColor(.gray.opacity(0.65))
  666. .padding(.vertical)
  667. }
  668. }
  669. }