AlternativeBolusCalcRootView.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. private enum Config {
  15. static let dividerHeight: CGFloat = 2
  16. static let overlayColour: Color = .white // Currently commented out
  17. static let spacing: CGFloat = 3
  18. }
  19. @Environment(\.colorScheme) var colorScheme
  20. @FetchRequest(
  21. entity: Meals.entity(),
  22. sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
  23. ) var meal: FetchedResults<Meals>
  24. private var formatter: NumberFormatter {
  25. let formatter = NumberFormatter()
  26. formatter.numberStyle = .decimal
  27. formatter.maximumFractionDigits = 2
  28. return formatter
  29. }
  30. private var mealFormatter: NumberFormatter {
  31. let formatter = NumberFormatter()
  32. formatter.numberStyle = .decimal
  33. formatter.maximumFractionDigits = 1
  34. return formatter
  35. }
  36. private var gluoseFormatter: NumberFormatter {
  37. let formatter = NumberFormatter()
  38. formatter.numberStyle = .decimal
  39. if state.units == .mmolL {
  40. formatter.maximumFractionDigits = 1
  41. } else { formatter.maximumFractionDigits = 0 }
  42. return formatter
  43. }
  44. private var fractionDigits: Int {
  45. if state.units == .mmolL {
  46. return 1
  47. } else { return 0 }
  48. }
  49. var body: some View {
  50. Form {
  51. Section {
  52. if state.waitForSuggestion {
  53. Text("Please wait")
  54. } else {
  55. predictionChart
  56. }
  57. } header: { Text("Predictions") }
  58. Section {}
  59. if fetch {
  60. Section {
  61. mealEntries
  62. } header: { Text("Meal Summary") }
  63. }
  64. Section {
  65. HStack {
  66. Button(action: {
  67. showInfo.toggle()
  68. }, label: {
  69. Image(systemName: "info.circle")
  70. Text("Calculations")
  71. })
  72. .foregroundStyle(.blue)
  73. .font(.footnote)
  74. .buttonStyle(PlainButtonStyle())
  75. .frame(maxWidth: .infinity, alignment: .leading)
  76. if state.fattyMeals {
  77. Spacer()
  78. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  79. Text("Fatty Meal")
  80. }
  81. .toggleStyle(CheckboxToggleStyle())
  82. .font(.footnote)
  83. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  84. state.insulinCalculated = state.calculateInsulin()
  85. if state.useFattyMealCorrectionFactor {
  86. state.useSuperBolus = false
  87. }
  88. }
  89. }
  90. if state.sweetMeals {
  91. Spacer()
  92. Toggle(isOn: $state.useSuperBolus) {
  93. Text("Super Bolus")
  94. }
  95. .toggleStyle(CheckboxToggleStyle())
  96. .font(.footnote)
  97. .onChange(of: state.useSuperBolus) { _ in
  98. state.insulinCalculated = state.calculateInsulin()
  99. if state.useSuperBolus {
  100. state.useFattyMealCorrectionFactor = false
  101. }
  102. }
  103. }
  104. }
  105. if state.waitForSuggestion {
  106. HStack {
  107. Text("Wait please").foregroundColor(.secondary)
  108. Spacer()
  109. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  110. }
  111. } else {
  112. HStack {
  113. Text("Recommended Bolus")
  114. Spacer()
  115. Text(
  116. formatter
  117. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  118. )
  119. Text(
  120. NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
  121. ).foregroundColor(.secondary)
  122. }.contentShape(Rectangle())
  123. .onTapGesture { state.amount = state.insulinCalculated }
  124. }
  125. HStack {
  126. Text("Bolus")
  127. Spacer()
  128. DecimalTextField(
  129. "0",
  130. value: $state.amount,
  131. formatter: formatter,
  132. autofocus: false,
  133. cleanInput: true
  134. )
  135. Text(exceededMaxBolus ? "😵" : " U").foregroundColor(.secondary)
  136. }
  137. .onChange(of: state.amount) { newValue in
  138. if newValue > state.maxBolus {
  139. exceededMaxBolus = true
  140. } else {
  141. exceededMaxBolus = false
  142. }
  143. }
  144. } header: { Text("Bolus") }
  145. if state.amount > 0 {
  146. Section {
  147. Button {
  148. keepForNextWiew = true
  149. state.add()
  150. }
  151. label: { Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus") }
  152. .frame(maxWidth: .infinity, alignment: .center)
  153. .disabled(disabled)
  154. .listRowBackground(!disabled ? Color(.systemBlue) : Color(.systemGray4))
  155. .tint(.white)
  156. }
  157. }
  158. if state.amount <= 0 {
  159. Section {
  160. Button {
  161. keepForNextWiew = true
  162. state.showModal(for: nil)
  163. }
  164. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  165. }
  166. }
  167. }.scrollContentBackground(.hidden).background(color)
  168. .blur(radius: showInfo ? 3 : 0)
  169. .navigationTitle("Enact Bolus")
  170. .navigationBarTitleDisplayMode(.inline)
  171. .navigationBarItems(
  172. leading: Button {
  173. carbsView()
  174. }
  175. label: {
  176. HStack {
  177. Image(systemName: "chevron.backward")
  178. Text("Meal")
  179. }
  180. },
  181. trailing: Button { state.hideModal() }
  182. label: { Text("Close") }
  183. )
  184. .onAppear {
  185. configureView {
  186. state.waitForSuggestionInitial = waitForSuggestion
  187. state.waitForSuggestion = waitForSuggestion
  188. state.insulinCalculated = state.calculateInsulin()
  189. }
  190. }
  191. .onDisappear {
  192. if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
  193. state.delete(deleteTwice: true, meal: meal)
  194. } else if fetch, !keepForNextWiew, state.useCalc {
  195. state.delete(deleteTwice: false, meal: meal)
  196. }
  197. }
  198. .popup(isPresented: showInfo) {
  199. bolusInfoAlternativeCalculator
  200. }
  201. }
  202. private var color: LinearGradient {
  203. colorScheme == .dark ? LinearGradient(
  204. gradient: Gradient(colors: [
  205. // RGB(10, 34, 55)
  206. Color(red: 0.03921568627, green: 0.1333333333, blue: 0.2156862745),
  207. // RGB(3, 15, 28)
  208. Color(red: 0.011, green: 0.058, blue: 0.109),
  209. // RGB(10, 34, 55)
  210. Color(red: 0.03921568627, green: 0.1333333333, blue: 0.2156862745)
  211. ]),
  212. startPoint: .top,
  213. endPoint: .bottom
  214. )
  215. :
  216. LinearGradient(gradient: Gradient(colors: [Color.gray.opacity(0.1)]), startPoint: .top, endPoint: .bottom)
  217. }
  218. var predictionChart: some View {
  219. ZStack {
  220. PredictionView(
  221. predictions: $state.predictions, units: $state.units, eventualBG: $state.evBG, target: $state.target,
  222. displayPredictions: $state.displayPredictions
  223. )
  224. }
  225. }
  226. // Pop-up
  227. var bolusInfoAlternativeCalculator: some View {
  228. VStack {
  229. VStack {
  230. VStack(spacing: Config.spacing) {
  231. HStack {
  232. Text("Calculations")
  233. .font(.title3).frame(maxWidth: .infinity, alignment: .center)
  234. }.padding(10)
  235. if fetch {
  236. mealEntries.padding()
  237. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  238. }
  239. settings.padding()
  240. }
  241. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  242. insulinParts.padding()
  243. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  244. VStack {
  245. HStack {
  246. Text("Full Bolus")
  247. .foregroundColor(.secondary)
  248. Spacer()
  249. let insulin = state.roundedWholeCalc
  250. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  251. Text(" U")
  252. .foregroundColor(.secondary)
  253. }
  254. }.padding(.horizontal)
  255. Divider().frame(height: Config.dividerHeight)
  256. results.padding()
  257. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  258. if exceededMaxBolus {
  259. HStack {
  260. let maxBolus = state.maxBolus
  261. let maxBolusFormatted = maxBolus.formatted()
  262. Text("Your entered amount was limited by your max Bolus setting of \(maxBolusFormatted)\(" U")")
  263. }
  264. .padding()
  265. .fontWeight(.semibold)
  266. .foregroundStyle(Color.loopRed)
  267. }
  268. }
  269. .padding(.top, 10)
  270. .padding(.bottom, 15)
  271. // Hide pop-up
  272. VStack {
  273. Button { showInfo = false }
  274. label: { Text("OK") }
  275. .frame(maxWidth: .infinity, alignment: .center)
  276. .font(.system(size: 16))
  277. .fontWeight(.semibold)
  278. .foregroundColor(.blue)
  279. }
  280. .padding(.bottom, 20)
  281. }
  282. .font(.footnote)
  283. .background(
  284. RoundedRectangle(cornerRadius: 10, style: .continuous)
  285. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4).opacity(0.9))
  286. )
  287. }
  288. private var disabled: Bool {
  289. state.amount <= 0 || state.amount > state.maxBolus
  290. }
  291. var changed: Bool {
  292. ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  293. }
  294. var hasFatOrProtein: Bool {
  295. ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  296. }
  297. func carbsView() {
  298. if fetch {
  299. keepForNextWiew = true
  300. state.backToCarbsView(complexEntry: true, meal, override: false)
  301. } else {
  302. state.backToCarbsView(complexEntry: false, meal, override: true)
  303. }
  304. }
  305. var mealEntries: some View {
  306. VStack {
  307. if let carbs = meal.first?.carbs, carbs > 0 {
  308. HStack {
  309. Text("Carbs")
  310. Spacer()
  311. Text(carbs.formatted())
  312. Text("g")
  313. }.foregroundColor(.secondary)
  314. }
  315. if let fat = meal.first?.fat, fat > 0 {
  316. HStack {
  317. Text("Fat")
  318. Spacer()
  319. Text(fat.formatted())
  320. Text("g")
  321. }.foregroundColor(.secondary)
  322. }
  323. if let protein = meal.first?.protein, protein > 0 {
  324. HStack {
  325. Text("Protein")
  326. Spacer()
  327. Text(protein.formatted())
  328. Text("g")
  329. }.foregroundColor(.secondary)
  330. }
  331. if let note = meal.first?.note, note != "" {
  332. HStack {
  333. Text("Note")
  334. Spacer()
  335. Text(note)
  336. }.foregroundColor(.secondary)
  337. }
  338. }
  339. }
  340. var settings: some View {
  341. VStack {
  342. HStack {
  343. Text("Carb Ratio")
  344. .foregroundColor(.secondary)
  345. Spacer()
  346. Text(state.carbRatio.formatted())
  347. Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
  348. .foregroundColor(.secondary)
  349. }
  350. HStack {
  351. Text("ISF")
  352. .foregroundColor(.secondary)
  353. Spacer()
  354. let isf = state.isf
  355. Text(isf.formatted())
  356. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  357. .foregroundColor(.secondary)
  358. }
  359. HStack {
  360. Text("Target Glucose")
  361. .foregroundColor(.secondary)
  362. Spacer()
  363. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  364. Text(
  365. target
  366. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  367. )
  368. Text(state.units.rawValue)
  369. .foregroundColor(.secondary)
  370. }
  371. HStack {
  372. Text("Basal")
  373. .foregroundColor(.secondary)
  374. Spacer()
  375. let basal = state.basal
  376. Text(basal.formatted())
  377. Text(NSLocalizedString(" U/h", comment: " Units per hour"))
  378. .foregroundColor(.secondary)
  379. }
  380. HStack {
  381. Text("Fraction")
  382. .foregroundColor(.secondary)
  383. Spacer()
  384. let fraction = state.fraction
  385. Text(fraction.formatted())
  386. }
  387. if state.useFattyMealCorrectionFactor {
  388. HStack {
  389. Text("Fatty Meal Factor")
  390. .foregroundColor(.orange)
  391. Spacer()
  392. let fraction = state.fattyMealFactor
  393. Text(fraction.formatted())
  394. .foregroundColor(.orange)
  395. }
  396. }
  397. if state.useSuperBolus {
  398. HStack {
  399. Text("Super Bolus")
  400. .foregroundColor(.red)
  401. Spacer()
  402. let superBolusInsulin = state.superBolusInsulin
  403. Text(superBolusInsulin.formatted()).foregroundColor(.red)
  404. Text(" U")
  405. .foregroundColor(.secondary)
  406. }
  407. }
  408. }
  409. }
  410. var insulinParts: some View {
  411. VStack(spacing: Config.spacing) {
  412. HStack {
  413. Text("Glucose")
  414. .foregroundColor(.secondary)
  415. Spacer()
  416. let glucose = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  417. Text(glucose.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  418. Text(state.units.rawValue)
  419. .foregroundColor(.secondary)
  420. Spacer()
  421. Image(systemName: "arrow.right")
  422. Spacer()
  423. let targetDifferenceInsulin = state.targetDifferenceInsulin
  424. // rounding
  425. let targetDifferenceInsulinAsDouble = NSDecimalNumber(decimal: targetDifferenceInsulin).doubleValue
  426. let roundedTargetDifferenceInsulin = Decimal(round(100 * targetDifferenceInsulinAsDouble) / 100)
  427. Text(roundedTargetDifferenceInsulin.formatted())
  428. Text(" U")
  429. .foregroundColor(.secondary)
  430. }
  431. HStack {
  432. Text("IOB")
  433. .foregroundColor(.secondary)
  434. Spacer()
  435. let iob = state.iob
  436. // rounding
  437. let iobAsDouble = NSDecimalNumber(decimal: iob).doubleValue
  438. let roundedIob = Decimal(round(100 * iobAsDouble) / 100)
  439. Text(roundedIob.formatted())
  440. Text(" U")
  441. .foregroundColor(.secondary)
  442. Spacer()
  443. Image(systemName: "arrow.right")
  444. Spacer()
  445. let iobCalc = state.iobInsulinReduction
  446. // rounding
  447. let iobCalcAsDouble = NSDecimalNumber(decimal: iobCalc).doubleValue
  448. let roundedIobCalc = Decimal(round(100 * iobCalcAsDouble) / 100)
  449. Text(roundedIobCalc.formatted())
  450. Text(" U").foregroundColor(.secondary)
  451. }
  452. HStack {
  453. Text("Trend")
  454. .foregroundColor(.secondary)
  455. Spacer()
  456. let trend = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  457. Text(trend.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  458. Text(state.units.rawValue).foregroundColor(.secondary)
  459. Spacer()
  460. Image(systemName: "arrow.right")
  461. Spacer()
  462. let trendInsulin = state.fifteenMinInsulin
  463. // rounding
  464. let trendInsulinAsDouble = NSDecimalNumber(decimal: trendInsulin).doubleValue
  465. let roundedTrendInsulin = Decimal(round(100 * trendInsulinAsDouble) / 100)
  466. Text(roundedTrendInsulin.formatted())
  467. Text(" U")
  468. .foregroundColor(.secondary)
  469. }
  470. HStack {
  471. Text("COB")
  472. .foregroundColor(.secondary)
  473. Spacer()
  474. let cob = state.cob
  475. Text(cob.formatted())
  476. let unitGrams = NSLocalizedString(" g", comment: "grams")
  477. Text(unitGrams).foregroundColor(.secondary)
  478. Spacer()
  479. Image(systemName: "arrow.right")
  480. Spacer()
  481. let insulinCob = state.wholeCobInsulin
  482. // rounding
  483. let insulinCobAsDouble = NSDecimalNumber(decimal: insulinCob).doubleValue
  484. let roundedInsulinCob = Decimal(round(100 * insulinCobAsDouble) / 100)
  485. Text(roundedInsulinCob.formatted())
  486. Text(" U")
  487. .foregroundColor(.secondary)
  488. }
  489. }
  490. }
  491. var results: some View {
  492. VStack {
  493. HStack {
  494. Text("Result")
  495. .fontWeight(.bold)
  496. Spacer()
  497. if !state.useSuperBolus {
  498. let fraction = state.fraction
  499. Text(fraction.formatted())
  500. Text(" x ")
  501. .foregroundColor(.secondary)
  502. // if fatty meal is chosen
  503. if state.useFattyMealCorrectionFactor {
  504. let fattyMealFactor = state.fattyMealFactor
  505. Text(fattyMealFactor.formatted())
  506. .foregroundColor(.orange)
  507. Text(" x ")
  508. .foregroundColor(.secondary)
  509. }
  510. let insulin = state.roundedWholeCalc
  511. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  512. Text(" U")
  513. .foregroundColor(.secondary)
  514. } else {
  515. // roundedWholeCalc
  516. let insulin = state.roundedWholeCalc
  517. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  518. Text(" U")
  519. // plus
  520. Text(" + ")
  521. .foregroundColor(.secondary)
  522. // superBolusInsulin
  523. let superBolusInsulin = state.superBolusInsulin
  524. Text(superBolusInsulin.formatted()).foregroundColor(.red)
  525. Text(" U")
  526. .foregroundColor(.secondary)
  527. }
  528. Text(" = ")
  529. .foregroundColor(.secondary)
  530. let result = state.insulinCalculated
  531. // rounding
  532. let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue
  533. let roundedResult = Decimal(round(100 * resultAsDouble) / 100)
  534. Text(roundedResult.formatted())
  535. .fontWeight(.bold)
  536. .font(.system(size: 16))
  537. .foregroundColor(.blue)
  538. Text(" U")
  539. .foregroundColor(.secondary)
  540. }
  541. }
  542. }
  543. }
  544. }