AlternativeBolusCalcRootView.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. .toolbar {
  189. ToolbarItem(placement: .topBarLeading) {
  190. if !fetch {
  191. Button("Close") {
  192. state.hideModal()
  193. }
  194. } else {
  195. Button {
  196. carbsView()
  197. } label: {
  198. HStack {
  199. Image(systemName: "chevron.backward")
  200. Text("Meal")
  201. }
  202. }
  203. }
  204. }
  205. }
  206. .onAppear {
  207. configureView {
  208. state.waitForSuggestionInitial = waitForSuggestion
  209. state.waitForSuggestion = waitForSuggestion
  210. state.insulinCalculated = state.calculateInsulin()
  211. }
  212. }
  213. .onDisappear {
  214. if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
  215. state.delete(deleteTwice: true, meal: meal)
  216. } else if fetch, !keepForNextWiew, state.useCalc {
  217. state.delete(deleteTwice: false, meal: meal)
  218. }
  219. }
  220. .sheet(isPresented: $showInfo) {
  221. calculationsDetailView
  222. .presentationDetents(
  223. [fetch ? .large : .fraction(0.9), .large],
  224. selection: $calculatorDetent
  225. )
  226. }
  227. }
  228. var predictionChart: some View {
  229. ZStack {
  230. PredictionView(
  231. predictions: $state.predictions, units: $state.units, eventualBG: $state.evBG, target: $state.target,
  232. displayPredictions: $state.displayPredictions
  233. )
  234. }
  235. }
  236. var calcSettingsFirstRow: some View {
  237. GridRow {
  238. Group {
  239. Text("Carb Ratio:")
  240. .foregroundColor(.secondary)
  241. }.gridCellAnchor(.leading)
  242. Group {
  243. Text("ISF:")
  244. .foregroundColor(.secondary)
  245. }.gridCellAnchor(.leading)
  246. VStack {
  247. Text("Target:")
  248. .foregroundColor(.secondary)
  249. }.gridCellAnchor(.leading)
  250. }
  251. }
  252. var calcSettingsSecondRow: some View {
  253. GridRow {
  254. Text(state.carbRatio.formatted() + " " + NSLocalizedString("g/U", comment: " grams per Unit"))
  255. .gridCellAnchor(.leading)
  256. Text(
  257. state.isf.formatted() + " " + state.units
  258. .rawValue + NSLocalizedString("/U", comment: "/Insulin unit")
  259. ).gridCellAnchor(.leading)
  260. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  261. Text(
  262. target
  263. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  264. " " + state.units.rawValue
  265. ).gridCellAnchor(.leading)
  266. }
  267. }
  268. var calcGlucoseFirstRow: some View {
  269. GridRow(alignment: .center) {
  270. let currentBG = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  271. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  272. Text("Glucose:").foregroundColor(.secondary)
  273. let firstRow = currentBG
  274. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  275. + " - " +
  276. target
  277. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  278. + " = " +
  279. state.targetDifference
  280. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  281. Text(firstRow).frame(minWidth: 0, alignment: .leading).foregroundColor(.secondary)
  282. .gridColumnAlignment(.leading)
  283. HStack {
  284. Text(
  285. self.insulinRounder(state.targetDifferenceInsulin).formatted()
  286. )
  287. Text("U").foregroundColor(.secondary)
  288. }.fontWeight(.bold)
  289. .gridColumnAlignment(.trailing)
  290. }
  291. }
  292. var calcGlucoseSecondRow: some View {
  293. GridRow(alignment: .center) {
  294. let currentBG = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  295. Text(
  296. currentBG
  297. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  298. " " +
  299. state.units.rawValue
  300. )
  301. let secondRow = state.targetDifference
  302. .formatted(
  303. .number.grouping(.never).rounded()
  304. .precision(.fractionLength(fractionDigits))
  305. )
  306. + " / " +
  307. state.isf.formatted()
  308. + " ≈ " +
  309. self.insulinRounder(state.targetDifferenceInsulin).formatted()
  310. Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
  311. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  312. }
  313. }
  314. var calcGlucoseFormulaRow: some View {
  315. GridRow(alignment: .top) {
  316. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  317. Text("(Current - Target) / ISF").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  318. .gridColumnAlignment(.leading)
  319. .gridCellColumns(2)
  320. }
  321. .font(.caption)
  322. }
  323. var calcIOBRow: some View {
  324. GridRow(alignment: .center) {
  325. HStack {
  326. Text("IOB:").foregroundColor(.secondary)
  327. Text(
  328. self.insulinRounder(state.iob).formatted()
  329. )
  330. }
  331. Text("Subtract IOB").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  332. let iobFormatted = self.insulinRounder(state.iob).formatted()
  333. HStack {
  334. Text((state.iob != 0 ? "-" : "") + (state.iob >= 0 ? iobFormatted : "(" + iobFormatted + ")"))
  335. Text("U").foregroundColor(.secondary)
  336. }.fontWeight(.bold)
  337. .gridColumnAlignment(.trailing)
  338. }
  339. }
  340. var calcCOBRow: some View {
  341. GridRow(alignment: .center) {
  342. HStack {
  343. Text("COB:").foregroundColor(.secondary)
  344. Text(
  345. state.cob
  346. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  347. NSLocalizedString(" g", comment: "grams")
  348. )
  349. }
  350. Text(
  351. state.cob
  352. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  353. + " / " +
  354. state.carbRatio.formatted()
  355. + " ≈ " +
  356. self.insulinRounder(state.wholeCobInsulin).formatted()
  357. )
  358. .foregroundColor(.secondary)
  359. .gridColumnAlignment(.leading)
  360. HStack {
  361. Text(
  362. self.insulinRounder(state.wholeCobInsulin).formatted()
  363. )
  364. Text("U").foregroundColor(.secondary)
  365. }.fontWeight(.bold)
  366. .gridColumnAlignment(.trailing)
  367. }
  368. }
  369. var calcCOBFormulaRow: some View {
  370. GridRow(alignment: .center) {
  371. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  372. Text("COB / Carb Ratio").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  373. .gridColumnAlignment(.leading)
  374. .gridCellColumns(2)
  375. }
  376. .font(.caption)
  377. }
  378. var calcDeltaRow: some View {
  379. GridRow(alignment: .center) {
  380. Text("Delta:").foregroundColor(.secondary)
  381. let deltaBG = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  382. Text(
  383. deltaBG
  384. .formatted(
  385. .number.grouping(.never).rounded()
  386. .precision(.fractionLength(fractionDigits))
  387. )
  388. + " / " +
  389. state.isf.formatted()
  390. + " ≈ " +
  391. self.insulinRounder(state.fifteenMinInsulin).formatted()
  392. )
  393. .foregroundColor(.secondary)
  394. .gridColumnAlignment(.leading)
  395. HStack {
  396. Text(
  397. self.insulinRounder(state.fifteenMinInsulin).formatted()
  398. )
  399. Text("U").foregroundColor(.secondary)
  400. }.fontWeight(.bold)
  401. .gridColumnAlignment(.trailing)
  402. }
  403. }
  404. var calcDeltaFormulaRow: some View {
  405. GridRow(alignment: .center) {
  406. let deltaBG = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  407. Text(
  408. deltaBG
  409. .formatted(
  410. .number.grouping(.never).rounded()
  411. .precision(.fractionLength(fractionDigits))
  412. ) + " " +
  413. state.units.rawValue
  414. )
  415. Text("15min Delta / ISF").font(.caption).foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  416. .gridColumnAlignment(.leading)
  417. .gridCellColumns(2).padding(.top, 5)
  418. }
  419. }
  420. var calcFullBolusRow: some View {
  421. GridRow(alignment: .center) {
  422. Text("Full Bolus")
  423. .foregroundColor(.secondary)
  424. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  425. HStack {
  426. Text(self.insulinRounder(state.wholeCalc).formatted())
  427. .foregroundStyle(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  428. Text("U").foregroundColor(.secondary)
  429. }.gridColumnAlignment(.trailing)
  430. .fontWeight(.bold)
  431. }
  432. }
  433. var calcSuperBolusRow: some View {
  434. GridRow(alignment: .center) {
  435. Text("Super Bolus")
  436. .foregroundColor(.secondary)
  437. Text("Added to Result").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  438. HStack {
  439. Text("+" + self.insulinRounder(state.superBolusInsulin).formatted())
  440. .foregroundStyle(Color.loopRed)
  441. Text("U").foregroundColor(.secondary)
  442. }.gridColumnAlignment(.trailing)
  443. .fontWeight(.bold)
  444. }
  445. }
  446. var calcResultRow: some View {
  447. GridRow(alignment: .center) {
  448. Text("Result").fontWeight(.bold)
  449. HStack {
  450. Text(state.useSuperBolus ? "(" : "")
  451. .foregroundColor(.loopRed)
  452. + Text(state.fraction.formatted())
  453. + Text(" x ")
  454. .foregroundColor(.secondary)
  455. // if fatty meal is chosen
  456. + Text(state.useFattyMealCorrectionFactor ? state.fattyMealFactor.formatted() : "")
  457. .foregroundColor(.orange)
  458. + Text(state.useFattyMealCorrectionFactor ? " x " : "")
  459. .foregroundColor(.secondary)
  460. // endif fatty meal is chosen
  461. + Text(self.insulinRounder(state.wholeCalc).formatted())
  462. .foregroundColor(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  463. // if superbolus is chosen
  464. + Text(state.useSuperBolus ? ")" : "")
  465. .foregroundColor(.loopRed)
  466. + Text(state.useSuperBolus ? " + " : "")
  467. .foregroundColor(.secondary)
  468. + Text(state.useSuperBolus ? state.superBolusInsulin.formatted() : "")
  469. .foregroundColor(.loopRed)
  470. // endif superbolus is chosen
  471. + Text(" ≈ ")
  472. .foregroundColor(.secondary)
  473. }
  474. .gridColumnAlignment(.leading)
  475. HStack {
  476. Text(self.insulinRounder(state.insulinCalculated).formatted())
  477. .fontWeight(.bold)
  478. .foregroundColor(.blue)
  479. Text("U").foregroundColor(.secondary)
  480. }
  481. .gridColumnAlignment(.trailing)
  482. .fontWeight(.bold)
  483. }
  484. }
  485. var calcResultFormulaRow: some View {
  486. GridRow(alignment: .bottom) {
  487. if state.useFattyMealCorrectionFactor {
  488. Text("Factor x Fatty Meal Factor x Full Bolus")
  489. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  490. .font(.caption)
  491. .gridCellAnchor(.center)
  492. .gridCellColumns(3)
  493. } else if state.useSuperBolus {
  494. Text("(Factor x Full Bolus) + Super Bolus")
  495. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  496. .font(.caption)
  497. .gridCellAnchor(.center)
  498. .gridCellColumns(3)
  499. } else {
  500. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  501. Text("Factor x Full Bolus")
  502. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  503. .font(.caption)
  504. .padding(.top, 5)
  505. .gridCellAnchor(.leading)
  506. .gridCellColumns(2)
  507. }
  508. }
  509. }
  510. var calculationsDetailView: some View {
  511. NavigationStack {
  512. ScrollView {
  513. Grid(alignment: .topLeading, horizontalSpacing: 3, verticalSpacing: 0) {
  514. GridRow {
  515. Text("Calculations").fontWeight(.bold).gridCellColumns(3).gridCellAnchor(.center).padding(.vertical)
  516. }
  517. calcSettingsFirstRow
  518. calcSettingsSecondRow
  519. DividerCustom()
  520. if fetch {
  521. // meal entries as grid rows
  522. GridRow {
  523. if let carbs = meal.first?.carbs, carbs > 0 {
  524. Text("Carbs").foregroundColor(.secondary)
  525. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  526. HStack {
  527. Text(carbs.formatted())
  528. Text("g").foregroundColor(.secondary)
  529. }.gridCellAnchor(.trailing)
  530. }
  531. }
  532. GridRow {
  533. if let fat = meal.first?.fat, fat > 0 {
  534. Text("Fat").foregroundColor(.secondary)
  535. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  536. HStack {
  537. Text(fat.formatted())
  538. Text("g").foregroundColor(.secondary)
  539. }.gridCellAnchor(.trailing)
  540. }
  541. }
  542. GridRow {
  543. if let protein = meal.first?.protein, protein > 0 {
  544. Text("Protein").foregroundColor(.secondary)
  545. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  546. HStack {
  547. Text(protein.formatted())
  548. Text("g").foregroundColor(.secondary)
  549. }.gridCellAnchor(.trailing)
  550. }
  551. }
  552. GridRow {
  553. if let note = meal.first?.note, note != "" {
  554. Text("Note").foregroundColor(.secondary)
  555. Text(note).foregroundColor(.secondary).gridCellColumns(2).gridCellAnchor(.trailing)
  556. }
  557. }
  558. DividerCustom()
  559. }
  560. GridRow {
  561. Text("Detailed Calculation Steps").gridCellColumns(3).gridCellAnchor(.center)
  562. .padding(.bottom, 10)
  563. }
  564. calcGlucoseFirstRow
  565. calcGlucoseSecondRow.padding(.bottom, 5)
  566. calcGlucoseFormulaRow
  567. DividerCustom()
  568. calcIOBRow
  569. DividerCustom()
  570. calcCOBRow.padding(.bottom, 5)
  571. calcCOBFormulaRow
  572. DividerCustom()
  573. calcDeltaRow
  574. calcDeltaFormulaRow
  575. DividerCustom()
  576. calcFullBolusRow
  577. if state.useSuperBolus {
  578. DividerCustom()
  579. calcSuperBolusRow
  580. }
  581. DividerDouble()
  582. calcResultRow
  583. calcResultFormulaRow
  584. }
  585. Spacer()
  586. Button { showInfo = false }
  587. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  588. .buttonStyle(.bordered)
  589. .padding(.top)
  590. }
  591. .padding([.horizontal, .bottom])
  592. .font(.system(size: 15))
  593. }
  594. }
  595. private func insulinRounder(_ value: Decimal) -> Decimal {
  596. let toRound = NSDecimalNumber(decimal: value).doubleValue
  597. return Decimal(floor(100 * toRound) / 100)
  598. }
  599. private var disabled: Bool {
  600. state.amount <= 0 || state.amount > state.maxBolus
  601. }
  602. var changed: Bool {
  603. ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  604. }
  605. var hasFatOrProtein: Bool {
  606. ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  607. }
  608. func carbsView() {
  609. keepForNextWiew = true
  610. state.backToCarbsView(complexEntry: true, meal, override: false)
  611. }
  612. var mealEntries: some View {
  613. VStack {
  614. if let carbs = meal.first?.carbs, carbs > 0 {
  615. HStack {
  616. Text("Carbs").foregroundColor(.secondary)
  617. Spacer()
  618. Text(carbs.formatted())
  619. Text("g").foregroundColor(.secondary)
  620. }
  621. }
  622. if let fat = meal.first?.fat, fat > 0 {
  623. HStack {
  624. Text("Fat").foregroundColor(.secondary)
  625. Spacer()
  626. Text(fat.formatted())
  627. Text("g").foregroundColor(.secondary)
  628. }
  629. }
  630. if let protein = meal.first?.protein, protein > 0 {
  631. HStack {
  632. Text("Protein").foregroundColor(.secondary)
  633. Spacer()
  634. Text(protein.formatted())
  635. Text("g").foregroundColor(.secondary)
  636. }
  637. }
  638. if let note = meal.first?.note, note != "" {
  639. HStack {
  640. Text("Note").foregroundColor(.secondary)
  641. Spacer()
  642. Text(note).foregroundColor(.secondary)
  643. }
  644. }
  645. }
  646. }
  647. }
  648. struct DividerDouble: View {
  649. var body: some View {
  650. VStack(spacing: 2) {
  651. Rectangle()
  652. .frame(height: 1)
  653. .foregroundColor(.gray.opacity(0.65))
  654. Rectangle()
  655. .frame(height: 1)
  656. .foregroundColor(.gray.opacity(0.65))
  657. }
  658. .frame(height: 4)
  659. .padding(.vertical)
  660. }
  661. }
  662. struct DividerCustom: View {
  663. var body: some View {
  664. Rectangle()
  665. .frame(height: 1)
  666. .foregroundColor(.gray.opacity(0.65))
  667. .padding(.vertical)
  668. }
  669. }
  670. }