AlternativeBolusCalcRootView.swift 23 KB

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