AlternativeBolusCalcRootView.swift 30 KB

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