AlternativeBolusCalcRootView.swift 30 KB

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