BolusRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import SwiftUI
  2. import Swinject
  3. extension Bolus {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. let waitForSuggestion: Bool
  7. let manualBolus: Bool
  8. @StateObject var state = StateModel()
  9. @State private var isAddInsulinAlertPresented = false
  10. @State private var presentInfo = false
  11. @State private var displayError = false
  12. @Environment(\.colorScheme) var colorScheme
  13. private var formatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 2
  17. return formatter
  18. }
  19. private var fractionDigits: Int {
  20. if state.units == .mmolL {
  21. return 1
  22. } else { return 0 }
  23. }
  24. var body: some View {
  25. Form {
  26. Section {
  27. if state.waitForSuggestion {
  28. HStack {
  29. Text("Wait please").foregroundColor(.secondary)
  30. Spacer()
  31. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  32. }
  33. } else {
  34. HStack {
  35. Text("Insulin recommended")
  36. Spacer()
  37. Text(
  38. formatter
  39. .string(from: state.insulinRecommended as NSNumber)! +
  40. NSLocalizedString(" U", comment: "Insulin unit")
  41. ).foregroundColor((state.error && state.insulinRecommended > 0) ? .red : .secondary)
  42. }.contentShape(Rectangle())
  43. .onTapGesture {
  44. if state.error, state.insulinRecommended > 0 { displayError = true }
  45. else { state.amount = state.insulinRecommended }
  46. }
  47. HStack {
  48. Image(systemName: "info.bubble").symbolRenderingMode(.palette).foregroundStyle(
  49. .primary, .blue
  50. )
  51. }.onTapGesture {
  52. presentInfo.toggle()
  53. }
  54. }
  55. }
  56. header: { Text("Recommendation") }
  57. if !state.waitForSuggestion {
  58. Section {
  59. HStack {
  60. Text("Amount")
  61. Spacer()
  62. DecimalTextField(
  63. "0",
  64. value: $state.amount,
  65. formatter: formatter,
  66. autofocus: true,
  67. cleanInput: true
  68. )
  69. Text("U").foregroundColor(.secondary)
  70. }
  71. }
  72. header: { Text("Bolus") }
  73. Section {
  74. Button { state.add() }
  75. label: { Text("Enact bolus") }
  76. .disabled(state.amount <= 0)
  77. }
  78. Section {
  79. if waitForSuggestion {
  80. Button { state.showModal(for: nil) }
  81. label: { Text("Continue without bolus") }
  82. } else {
  83. Button { isAddInsulinAlertPresented = true }
  84. label: { Text("Add insulin without actually bolusing") }
  85. .disabled(state.amount <= 0)
  86. }
  87. }
  88. .alert(isPresented: $isAddInsulinAlertPresented) {
  89. Alert(
  90. title: Text("Are you sure?"),
  91. message: Text(
  92. "Add " + formatter
  93. .string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") +
  94. NSLocalizedString(" without bolusing", comment: "Add insulin without bolusing alert")
  95. ),
  96. primaryButton: .destructive(
  97. Text("Add"),
  98. action: {
  99. state.addWithoutBolus()
  100. isAddInsulinAlertPresented = false
  101. }
  102. ),
  103. secondaryButton: .cancel()
  104. )
  105. }
  106. }
  107. }
  108. .alert(isPresented: $displayError) {
  109. Alert(
  110. title: Text("Warning!"),
  111. message: Text("\n" + alertString() + NSLocalizedString(
  112. "\n\nTap 'Add' to continue with selected amount.",
  113. comment: "Alert text to confirm bolus amount to add"
  114. )),
  115. primaryButton: .destructive(
  116. Text("Add"),
  117. action: {
  118. state.amount = state.insulinRecommended
  119. displayError = false
  120. }
  121. ),
  122. secondaryButton: .cancel()
  123. )
  124. }
  125. .onAppear {
  126. configureView {
  127. state.waitForSuggestionInitial = waitForSuggestion
  128. state.waitForSuggestion = waitForSuggestion
  129. }
  130. }
  131. .navigationTitle("Enact Bolus")
  132. .navigationBarTitleDisplayMode(.automatic)
  133. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  134. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  135. bolusInfo
  136. }
  137. }
  138. var bolusInfo: some View {
  139. VStack {
  140. // Variables
  141. VStack(spacing: 3) {
  142. HStack {
  143. Text("Eventual Glucose").foregroundColor(.secondary)
  144. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  145. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  146. Text(state.units.rawValue).foregroundColor(.secondary)
  147. }
  148. HStack {
  149. Text("Target Glucose").foregroundColor(.secondary)
  150. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  151. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  152. Text(state.units.rawValue).foregroundColor(.secondary)
  153. }
  154. HStack {
  155. Text("ISF").foregroundColor(.secondary)
  156. let isf = state.isf
  157. Text(isf.formatted())
  158. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  159. .foregroundColor(.secondary)
  160. }
  161. HStack {
  162. Text("ISF:")
  163. Text("Insulin Sensitivity")
  164. }.foregroundColor(.secondary).italic()
  165. if state.percentage != 100 {
  166. HStack {
  167. Text("Percentage setting").foregroundColor(.secondary)
  168. let percentage = state.percentage
  169. Text(percentage.formatted())
  170. Text("%").foregroundColor(.secondary)
  171. }
  172. }
  173. }
  174. .font(.footnote)
  175. .padding(.top, 10)
  176. Divider()
  177. // Formula
  178. VStack(spacing: 5) {
  179. let unit = NSLocalizedString(
  180. " U",
  181. comment: "Unit in number of units delivered (keep the space character!)"
  182. )
  183. Text("(Eventual Glucose - Target) / ISF =").font(.callout).italic()
  184. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  185. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  186. HStack {
  187. Text(" = ").font(.callout)
  188. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  189. }
  190. if state.percentage != 100, state.insulin > 0 {
  191. Divider()
  192. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  193. Text(
  194. state.insulinRecommended.formatted() + unit
  195. ).font(.callout).foregroundColor(.blue).bold()
  196. }
  197. }
  198. }
  199. // Warning
  200. if state.error, state.insulinRecommended > 0 {
  201. VStack(spacing: 5) {
  202. Divider()
  203. Text("Warning!").font(.callout).bold().foregroundColor(.orange)
  204. Text(alertString()).font(.footnote)
  205. Divider()
  206. }.padding(.horizontal, 10)
  207. }
  208. // Footer.
  209. if !(state.error && state.insulinRecommended > 0) {
  210. VStack {
  211. Text(
  212. "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."
  213. ).font(.caption2).foregroundColor(.secondary)
  214. }.padding(20)
  215. }
  216. // Hide button
  217. VStack {
  218. Button { presentInfo = false }
  219. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  220. .foregroundColor(.blue)
  221. }.padding(.bottom, 10)
  222. }
  223. .background(
  224. RoundedRectangle(cornerRadius: 8, style: .continuous)
  225. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  226. // .fill(Color(.systemGray).gradient) // A more prominent pop-up, but harder to read
  227. )
  228. }
  229. // Localize the Oref0 error/warning strings
  230. private func alertString() -> String {
  231. switch state.errorString {
  232. case 1:
  233. return NSLocalizedString(
  234. "Predicted Glucose, ",
  235. comment: "Bolus pop-up / Alert string. Make translations concise!"
  236. ) + state.minGuardBG
  237. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  238. .rawValue + ", " +
  239. NSLocalizedString(
  240. "is predicted below threshold ",
  241. comment: "Bolus pop-up / Alert string. Make translations concise!"
  242. ) + state
  243. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + "!"
  244. case 2:
  245. return NSLocalizedString(
  246. "Predicted Glucose, ",
  247. comment: "Bolus pop-up / Alert string. Make translations concise!"
  248. ) + state.minGuardBG
  249. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  250. .rawValue + ", " +
  251. NSLocalizedString(
  252. "is below Threshold of ",
  253. comment: "Bolus pop-up / Alert string. Make translations concise!"
  254. ) + state
  255. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  256. case 3:
  257. return NSLocalizedString(
  258. "Eventual Glucose > Target Glucose, but glucose is climbing slower 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(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  264. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  265. case 4:
  266. return NSLocalizedString(
  267. "Eventual Glucose > Target Glucose, but glucose is falling slower 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(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  273. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  274. case 5:
  275. return NSLocalizedString(
  276. "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
  277. comment: "Bolus pop-up / Alert string. Make translations concise!"
  278. ) +
  279. state.expectedDelta
  280. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  281. NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  282. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  283. case 6:
  284. return NSLocalizedString(
  285. "Minimum predicted Glucose is ",
  286. comment: "Bolus pop-up / Alert string. Make translations concise!"
  287. ) + state
  288. .minPredBG
  289. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  290. .units
  291. .rawValue
  292. default:
  293. return "Ignore Warning..."
  294. }
  295. }
  296. }
  297. }
  298. struct ActivityIndicator: UIViewRepresentable {
  299. @Binding var isAnimating: Bool
  300. let style: UIActivityIndicatorView.Style
  301. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  302. UIActivityIndicatorView(style: style)
  303. }
  304. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  305. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  306. }
  307. }