AlternativeBolusCalcRootView.swift 18 KB

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