AlternativeBolusCalcRootView.swift 25 KB

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