AlternativeBolusCalcRootView.swift 23 KB

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