AlternativeBolusCalcRootView.swift 25 KB

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