AlternativeBolusCalcRootView.swift 24 KB

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