BolusRootView.swift 14 KB

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