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