BolusRootView.swift 15 KB

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