AlternativeBolusCalcRootView.swift 21 KB

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