AlternativeBolusCalcRootView.swift 21 KB

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