AlternativeBolusCalcRootView.swift 20 KB

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