BolusRootView.swift 14 KB

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