AlternativeBolusCalcRootView.swift 20 KB

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