BolusRootView.swift 14 KB

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