BolusRootView.swift 14 KB

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