BolusRootView.swift 14 KB

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