AlternativeBolusCalcRootView.swift 30 KB

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