BolusRootView.swift 14 KB

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