AlternativeBolusCalcRootView.swift 30 KB

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