BolusRootView.swift 14 KB

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