BolusRootView.swift 14 KB

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