AlternativeBolusCalcRootView.swift 21 KB

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