BolusRootView.swift 15 KB

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