AlternativeBolusCalcRootView.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import SwiftUI
  2. import Swinject
  3. extension Bolus {
  4. // alternative bolus calc
  5. struct AlternativeBolusCalcRootView: BaseView {
  6. let resolver: Resolver
  7. let waitForSuggestion: Bool
  8. @ObservedObject var state: StateModel
  9. @State private var showInfo = false
  10. @State private var exceededMaxBolus = false
  11. @State var insulinCalculated: Decimal = 0
  12. @Environment(\.colorScheme) var colorScheme
  13. private var formatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 2
  17. return formatter
  18. }
  19. private var gluoseFormatter: NumberFormatter {
  20. let formatter = NumberFormatter()
  21. formatter.numberStyle = .decimal
  22. if state.units == .mmolL {
  23. formatter.maximumFractionDigits = 1
  24. } else { formatter.maximumFractionDigits = 0 }
  25. return formatter
  26. }
  27. private var fractionDigits: Int {
  28. if state.units == .mmolL {
  29. return 1
  30. } else { return 0 }
  31. }
  32. var body: some View {
  33. Form {
  34. Section {
  35. HStack {
  36. Text("Glucose")
  37. DecimalTextField(
  38. "0",
  39. value: Binding(
  40. get: {
  41. if state.units == .mmolL {
  42. return state.currentBG.asMmolL
  43. } else {
  44. return state.currentBG
  45. }
  46. },
  47. set: { newValue in
  48. if state.units == .mmolL {
  49. state.currentBG = newValue.asMmolL
  50. } else {
  51. state.currentBG = newValue
  52. }
  53. }
  54. ),
  55. formatter: gluoseFormatter,
  56. autofocus: false,
  57. cleanInput: true
  58. )
  59. .onChange(of: state.currentBG) { newValue in
  60. if newValue > 500 {
  61. state.currentBG = 500 // ensure that user can not input more than 500 mg/dL
  62. }
  63. insulinCalculated = state.calculateInsulin()
  64. }
  65. Text(state.units.rawValue)
  66. .foregroundColor(.secondary)
  67. }
  68. .contentShape(Rectangle())
  69. HStack {
  70. Button(action: {
  71. showInfo.toggle()
  72. insulinCalculated = state.calculateInsulin()
  73. }, label: {
  74. Image(systemName: "info.circle")
  75. Text("Calculations")
  76. })
  77. .foregroundStyle(.blue)
  78. .font(.footnote)
  79. .buttonStyle(PlainButtonStyle())
  80. .frame(maxWidth: .infinity, alignment: .leading)
  81. if state.fattyMeals {
  82. Spacer()
  83. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  84. Text("Fatty Meal")
  85. }
  86. .toggleStyle(CheckboxToggleStyle())
  87. .font(.footnote)
  88. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  89. insulinCalculated = state.calculateInsulin()
  90. }
  91. }
  92. }
  93. }
  94. header: { Text("Values") }
  95. Section {
  96. if state.waitForSuggestion {
  97. HStack {
  98. Text("Wait please").foregroundColor(.secondary)
  99. Spacer()
  100. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  101. }
  102. } else {
  103. HStack {
  104. Text("Recommended Bolus")
  105. Spacer()
  106. Text(
  107. formatter
  108. .string(from: Double(insulinCalculated) as NSNumber)!
  109. )
  110. let unit = NSLocalizedString(
  111. " U",
  112. comment: "Unit in number of units delivered (keep the space character!)"
  113. )
  114. Text(unit).foregroundColor(.secondary)
  115. }.contentShape(Rectangle())
  116. .onTapGesture {
  117. state.amount = insulinCalculated
  118. }
  119. if !state.waitForSuggestion {
  120. HStack {
  121. Text("Bolus")
  122. Spacer()
  123. DecimalTextField(
  124. "0",
  125. value: $state.amount,
  126. formatter: formatter,
  127. autofocus: false,
  128. cleanInput: true
  129. )
  130. Text(exceededMaxBolus ? "😵" : " U").foregroundColor(.secondary)
  131. }
  132. .onChange(of: state.amount) { newValue in
  133. if newValue > state.maxBolus {
  134. exceededMaxBolus = true
  135. } else {
  136. exceededMaxBolus = false
  137. }
  138. }
  139. }
  140. }
  141. }
  142. header: { Text("Bolus") }
  143. Section {
  144. if state.amount == 0 {
  145. Button { state.showModal(for: nil) }
  146. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  147. } else {
  148. Button(action: {
  149. state.add()
  150. }) {
  151. Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus")
  152. .frame(maxWidth: .infinity, alignment: .center)
  153. }
  154. .foregroundColor(exceededMaxBolus ? .loopRed : .accentColor)
  155. .disabled(
  156. state.amount <= 0 || state.amount > state.maxBolus
  157. )
  158. }
  159. }
  160. .onAppear {
  161. configureView {
  162. state.waitForSuggestionInitial = waitForSuggestion
  163. state.waitForSuggestion = waitForSuggestion
  164. }
  165. }
  166. .navigationTitle("Enact Bolus")
  167. .navigationBarTitleDisplayMode(.inline)
  168. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  169. }
  170. .blur(radius: showInfo ? 3 : 0)
  171. .popup(isPresented: showInfo) {
  172. bolusInfoAlternativeCalculator
  173. }
  174. }
  175. // calculation showed in popup
  176. var bolusInfoAlternativeCalculator: some View {
  177. let unit = NSLocalizedString(
  178. " U",
  179. comment: "Unit in number of units delivered (keep the space character!)"
  180. )
  181. return VStack {
  182. VStack {
  183. VStack {
  184. HStack {
  185. Text("Calculations")
  186. .font(.title3)
  187. .fontWeight(.semibold)
  188. Spacer()
  189. }
  190. .padding(.vertical, 10)
  191. HStack {
  192. Text("Carb Ratio")
  193. .foregroundColor(.secondary)
  194. Spacer()
  195. Text(state.carbRatio.formatted())
  196. Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
  197. .foregroundColor(.secondary)
  198. }
  199. HStack {
  200. Text("ISF")
  201. .foregroundColor(.secondary)
  202. Spacer()
  203. let isf = state.isf
  204. Text(isf.formatted())
  205. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  206. .foregroundColor(.secondary)
  207. }
  208. HStack {
  209. Text("Target Glucose")
  210. .foregroundColor(.secondary)
  211. Spacer()
  212. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  213. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  214. Text(state.units.rawValue)
  215. .foregroundColor(.secondary)
  216. }
  217. HStack {
  218. Text("Basal")
  219. .foregroundColor(.secondary)
  220. Spacer()
  221. let basal = state.basal
  222. Text(basal.formatted())
  223. Text(NSLocalizedString(" U/h", comment: " Units per hour"))
  224. .foregroundColor(.secondary)
  225. }
  226. HStack {
  227. Text("Fraction")
  228. .foregroundColor(.secondary)
  229. Spacer()
  230. let fraction = state.fraction
  231. Text(fraction.formatted())
  232. }
  233. if state.useFattyMealCorrectionFactor {
  234. HStack {
  235. Text("Fatty Meal Factor")
  236. .foregroundColor(.orange)
  237. Spacer()
  238. let fraction = state.fattyMealFactor
  239. Text(fraction.formatted())
  240. .foregroundColor(.orange)
  241. }
  242. }
  243. }
  244. .padding()
  245. VStack {
  246. HStack {
  247. Text("Glucose")
  248. .foregroundColor(.secondary)
  249. Spacer()
  250. let glucose = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  251. Text(glucose.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  252. Text(state.units.rawValue)
  253. .foregroundColor(.secondary)
  254. Spacer()
  255. Image(systemName: "arrow.right")
  256. Spacer()
  257. let targetDifferenceInsulin = state.targetDifferenceInsulin
  258. // rounding
  259. let targetDifferenceInsulinAsDouble = NSDecimalNumber(decimal: targetDifferenceInsulin).doubleValue
  260. let roundedTargetDifferenceInsulin = Decimal(round(100 * targetDifferenceInsulinAsDouble) / 100)
  261. Text(roundedTargetDifferenceInsulin.formatted())
  262. Text(unit)
  263. .foregroundColor(.secondary)
  264. }
  265. HStack {
  266. Text("IOB")
  267. .foregroundColor(.secondary)
  268. Spacer()
  269. let iob = state.iob
  270. // rounding
  271. let iobAsDouble = NSDecimalNumber(decimal: iob).doubleValue
  272. let roundedIob = Decimal(round(100 * iobAsDouble) / 100)
  273. Text(roundedIob.formatted())
  274. Text(unit)
  275. .foregroundColor(.secondary)
  276. Spacer()
  277. Image(systemName: "arrow.right")
  278. Spacer()
  279. let iobCalc = state.iobInsulinReduction
  280. // rounding
  281. let iobCalcAsDouble = NSDecimalNumber(decimal: iobCalc).doubleValue
  282. let roundedIobCalc = Decimal(round(100 * iobCalcAsDouble) / 100)
  283. Text(roundedIobCalc.formatted())
  284. Text(unit).foregroundColor(.secondary)
  285. }
  286. HStack {
  287. Text("Trend")
  288. .foregroundColor(.secondary)
  289. Spacer()
  290. let trend = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  291. Text(trend.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  292. Text(state.units.rawValue).foregroundColor(.secondary)
  293. Spacer()
  294. Image(systemName: "arrow.right")
  295. Spacer()
  296. let trendInsulin = state.fifteenMinInsulin
  297. // rounding
  298. let trendInsulinAsDouble = NSDecimalNumber(decimal: trendInsulin).doubleValue
  299. let roundedTrendInsulin = Decimal(round(100 * trendInsulinAsDouble) / 100)
  300. Text(roundedTrendInsulin.formatted())
  301. Text(unit)
  302. .foregroundColor(.secondary)
  303. }
  304. HStack {
  305. Text("COB")
  306. .foregroundColor(.secondary)
  307. Spacer()
  308. let cob = state.cob
  309. Text(cob.formatted())
  310. let unitGrams = NSLocalizedString(" g", comment: "grams")
  311. Text(unitGrams).foregroundColor(.secondary)
  312. Spacer()
  313. Image(systemName: "arrow.right")
  314. Spacer()
  315. let insulinCob = state.wholeCobInsulin
  316. // rounding
  317. let insulinCobAsDouble = NSDecimalNumber(decimal: insulinCob).doubleValue
  318. let roundedInsulinCob = Decimal(round(100 * insulinCobAsDouble) / 100)
  319. Text(roundedInsulinCob.formatted())
  320. Text(unit)
  321. .foregroundColor(.secondary)
  322. }
  323. }
  324. .padding()
  325. Divider()
  326. .fontWeight(.bold)
  327. HStack {
  328. Text("Full Bolus")
  329. .foregroundColor(.secondary)
  330. Spacer()
  331. let insulin = state.roundedWholeCalc
  332. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  333. Text(unit)
  334. .foregroundColor(.secondary)
  335. }
  336. .padding()
  337. Divider()
  338. .fontWeight(.bold)
  339. HStack {
  340. Text("Result")
  341. .fontWeight(.bold)
  342. Spacer()
  343. let fraction = state.fraction
  344. Text(fraction.formatted())
  345. Text(" x ")
  346. .foregroundColor(.secondary)
  347. // if fatty meal is chosen
  348. if state.useFattyMealCorrectionFactor {
  349. let fattyMealFactor = state.fattyMealFactor
  350. Text(fattyMealFactor.formatted())
  351. .foregroundColor(.orange)
  352. Text(" x ")
  353. .foregroundColor(.secondary)
  354. }
  355. let insulin = state.roundedWholeCalc
  356. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  357. Text(unit)
  358. .foregroundColor(.secondary)
  359. Text(" = ")
  360. .foregroundColor(.secondary)
  361. let result = state.insulinCalculated
  362. // rounding
  363. let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue
  364. let roundedResult = Decimal(round(100 * resultAsDouble) / 100)
  365. Text(roundedResult.formatted())
  366. .fontWeight(.bold)
  367. .font(.system(size: 16))
  368. .foregroundColor(.blue)
  369. Text(unit)
  370. .foregroundColor(.secondary)
  371. }
  372. .padding()
  373. if exceededMaxBolus {
  374. HStack {
  375. let maxBolus = state.maxBolus
  376. let maxBolusFormatted = maxBolus.formatted()
  377. Text("Your entered amount was limited by your max Bolus setting of \(maxBolusFormatted)\(unit)!")
  378. }
  379. .padding()
  380. .fontWeight(.semibold)
  381. .foregroundStyle(Color.loopRed)
  382. }
  383. }
  384. .padding(.top, 10)
  385. .padding(.bottom, 15)
  386. // Hide button
  387. VStack {
  388. Button {
  389. showInfo = false
  390. }
  391. label: {
  392. Text("OK")
  393. }
  394. .frame(maxWidth: .infinity, alignment: .center)
  395. .font(.system(size: 16))
  396. .fontWeight(.semibold)
  397. .foregroundColor(.blue)
  398. }
  399. .padding(.bottom, 20)
  400. }
  401. .font(.footnote)
  402. .background(
  403. RoundedRectangle(cornerRadius: 10, style: .continuous)
  404. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4).opacity(0.9))
  405. )
  406. }
  407. }
  408. }