AlternativeBolusCalcRootView.swift 22 KB

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