DefaultBolusCalcRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import SwiftUI
  2. import Swinject
  3. extension Bolus {
  4. struct DefaultBolusCalcRootView: BaseView {
  5. let resolver: Resolver
  6. let waitForSuggestion: Bool
  7. let fetch: Bool
  8. @StateObject var state = StateModel()
  9. @State private var isAddInsulinAlertPresented = false
  10. @State private var presentInfo = false
  11. @State private var displayError = false
  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 fractionDigits: Int {
  20. if state.units == .mmolL {
  21. return 1
  22. } else { return 0 }
  23. }
  24. var body: some View {
  25. Form {
  26. Section {
  27. if state.waitForSuggestion {
  28. HStack {
  29. Text("Wait please").foregroundColor(.secondary)
  30. Spacer()
  31. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  32. }
  33. } else {
  34. HStack {
  35. Text("Insulin recommended")
  36. Image(systemName: "info.bubble")
  37. .symbolRenderingMode(.palette)
  38. .foregroundStyle(.primary, .blue)
  39. .onTapGesture {
  40. presentInfo.toggle()
  41. }
  42. Spacer()
  43. Text(
  44. formatter
  45. .string(from: state.insulinRecommended as NSNumber)! +
  46. NSLocalizedString(" U", comment: "Insulin unit")
  47. ).foregroundColor((state.error && state.insulinRecommended > 0) ? .red : .secondary)
  48. .onTapGesture {
  49. if state.error, state.insulinRecommended > 0 { displayError = true }
  50. else { state.amount = state.insulinRecommended }
  51. }
  52. }.contentShape(Rectangle())
  53. }
  54. }
  55. header: { Text("Recommendation") }
  56. if !state.waitForSuggestion {
  57. Section {
  58. HStack {
  59. Text("Amount")
  60. Spacer()
  61. DecimalTextField(
  62. "0",
  63. value: $state.amount,
  64. formatter: formatter,
  65. autofocus: true,
  66. cleanInput: true
  67. )
  68. Text(!(state.amount > state.maxBolus) ? "U" : "😵").foregroundColor(.secondary)
  69. }
  70. }
  71. header: { Text("Bolus") }
  72. Section {
  73. Button { state.add() }
  74. label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
  75. .frame(maxWidth: .infinity, alignment: .center)
  76. .disabled(
  77. state.amount <= 0 || state.amount > state.maxBolus
  78. )
  79. }
  80. if waitForSuggestion {
  81. Section {
  82. Button { state.showModal(for: nil) }
  83. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  84. }
  85. }
  86. }
  87. }
  88. .alert(isPresented: $displayError) {
  89. Alert(
  90. title: Text("Warning!"),
  91. message: Text("\n" + alertString() + "\n"),
  92. primaryButton: .destructive(
  93. Text("Add"),
  94. action: {
  95. state.amount = state.insulinRecommended
  96. displayError = false
  97. }
  98. ),
  99. secondaryButton: .cancel()
  100. )
  101. }.onAppear {
  102. configureView {
  103. state.waitForSuggestionInitial = waitForSuggestion
  104. state.waitForSuggestion = waitForSuggestion
  105. }
  106. }
  107. .navigationTitle("Enact Bolus")
  108. .navigationBarTitleDisplayMode(.inline)
  109. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  110. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  111. bolusInfo
  112. }
  113. }
  114. var bolusInfo: some View {
  115. VStack {
  116. // Variables
  117. VStack(spacing: 3) {
  118. HStack {
  119. Text("Eventual Glucose").foregroundColor(.secondary)
  120. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  121. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  122. Text(state.units.rawValue).foregroundColor(.secondary)
  123. }
  124. HStack {
  125. Text("Target Glucose").foregroundColor(.secondary)
  126. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  127. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  128. Text(state.units.rawValue).foregroundColor(.secondary)
  129. }
  130. HStack {
  131. Text("ISF").foregroundColor(.secondary)
  132. let isf = state.isf
  133. Text(isf.formatted())
  134. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  135. .foregroundColor(.secondary)
  136. }
  137. HStack {
  138. Text("ISF:")
  139. Text("Insulin Sensitivity")
  140. }.foregroundColor(.secondary).italic()
  141. if state.percentage != 100 {
  142. HStack {
  143. Text("Percentage setting").foregroundColor(.secondary)
  144. let percentage = state.percentage
  145. Text(percentage.formatted())
  146. Text("%").foregroundColor(.secondary)
  147. }
  148. }
  149. HStack {
  150. Text("Formula:")
  151. Text("(Eventual Glucose - Target) / ISF")
  152. }.foregroundColor(.secondary).italic().padding(.top, 5)
  153. }
  154. .font(.footnote)
  155. .padding(.top, 10)
  156. Divider()
  157. // Formula
  158. VStack(spacing: 5) {
  159. let unit = NSLocalizedString(
  160. " U",
  161. comment: "Unit in number of units delivered (keep the space character!)"
  162. )
  163. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  164. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  165. HStack {
  166. Text(NSLocalizedString("Insulin recommended", comment: "") + ":").font(.callout)
  167. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  168. }
  169. if state.percentage != 100, state.insulin > 0 {
  170. Divider()
  171. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  172. Text(
  173. state.insulinRecommended.formatted() + unit
  174. ).font(.callout).foregroundColor(.blue).bold()
  175. }
  176. }
  177. }
  178. // Warning
  179. if state.error, state.insulinRecommended > 0 {
  180. VStack(spacing: 5) {
  181. Divider()
  182. Text("Warning!").font(.callout).bold().foregroundColor(.orange)
  183. Text(alertString()).font(.footnote)
  184. Divider()
  185. }.padding(.horizontal, 10)
  186. }
  187. // Footer
  188. if !(state.error && state.insulinRecommended > 0) {
  189. VStack {
  190. Text(
  191. "Carbs and previous insulin are included in the glucose prediction, but if the Eventual Glucose is lower than the Target Glucose, a bolus will not be recommended."
  192. ).font(.caption2).foregroundColor(.secondary)
  193. }.padding(20)
  194. }
  195. // Hide button
  196. VStack {
  197. Button { presentInfo = false }
  198. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  199. .foregroundColor(.blue)
  200. }.padding(.bottom, 10)
  201. }
  202. .background(
  203. RoundedRectangle(cornerRadius: 8, style: .continuous)
  204. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  205. )
  206. }
  207. // Localize the Oref0 error/warning strings. The default should never be returned
  208. private func alertString() -> String {
  209. switch state.errorString {
  210. case 1,
  211. 2:
  212. return NSLocalizedString(
  213. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  214. comment: "Bolus pop-up / Alert string. Make translations concise!"
  215. ) + state.minGuardBG
  216. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  217. .rawValue + ", " +
  218. NSLocalizedString(
  219. "which is below your Threshold (",
  220. comment: "Bolus pop-up / Alert string. Make translations concise!"
  221. ) + state
  222. .threshold.formatted() + " " + state.units.rawValue + ")"
  223. case 3:
  224. return NSLocalizedString(
  225. "Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ",
  226. comment: "Bolus pop-up / Alert string. Make translations concise!"
  227. ) +
  228. state.expectedDelta
  229. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  230. NSLocalizedString(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  231. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  232. case 4:
  233. return NSLocalizedString(
  234. "Eventual Glucose > Target Glucose, but glucose is falling faster than expected. Expected: ",
  235. comment: "Bolus pop-up / Alert string. Make translations concise!"
  236. ) +
  237. state.expectedDelta
  238. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  239. NSLocalizedString(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  240. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  241. case 5:
  242. return NSLocalizedString(
  243. "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
  244. comment: "Bolus pop-up / Alert string. Make translations concise!"
  245. ) +
  246. state.expectedDelta
  247. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  248. NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  249. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  250. case 6:
  251. return NSLocalizedString(
  252. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  253. comment: "Bolus pop-up / Alert string. Make translations concise!"
  254. ) + state
  255. .minPredBG
  256. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  257. .units
  258. .rawValue
  259. default:
  260. return "Ignore Warning..."
  261. }
  262. }
  263. }
  264. }