AlternativeBolusCalcRootView.swift 22 KB

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