BolusRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. .alert(isPresented: $displayError) {
  126. Alert(
  127. title: Text("Warning!"),
  128. message: Text("\n" + NSLocalizedString(state.errorString, comment: "") + NSLocalizedString(
  129. "\n\nTap 'Add' to continue with selected amount.",
  130. comment: "Alert text to confirm bolus amount to add"
  131. )),
  132. primaryButton: .destructive(
  133. Text("Add"),
  134. action: {
  135. state.amount = state.insulinRecommended
  136. displayError = false
  137. }
  138. ),
  139. secondaryButton: .cancel()
  140. )
  141. }
  142. .onAppear {
  143. configureView {
  144. state.waitForSuggestionInitial = waitForSuggestion
  145. state.waitForSuggestion = waitForSuggestion
  146. state.manual = manualBolus
  147. }
  148. }
  149. .navigationTitle("Enact Bolus")
  150. .navigationBarTitleDisplayMode(.automatic)
  151. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  152. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  153. bolusInfo
  154. }
  155. }
  156. var bolusInfo: some View {
  157. VStack {
  158. // Variables
  159. VStack(spacing: 3) {
  160. HStack {
  161. Text("Eventual Glucose").foregroundColor(.secondary)
  162. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  163. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  164. Text(state.units.rawValue).foregroundColor(.secondary)
  165. }
  166. HStack {
  167. Text("Target Glucose").foregroundColor(.secondary)
  168. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  169. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  170. Text(state.units.rawValue).foregroundColor(.secondary)
  171. }
  172. HStack {
  173. Text("ISF").foregroundColor(.secondary)
  174. let isf = state.isf
  175. Text(isf.formatted())
  176. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  177. .foregroundColor(.secondary)
  178. }
  179. HStack {
  180. Text("ISF:")
  181. Text("Insulin Sensitivity")
  182. }.foregroundColor(.secondary).italic()
  183. if state.percentage != 100 {
  184. HStack {
  185. Text("Percentage setting").foregroundColor(.secondary)
  186. let percentage = state.percentage
  187. Text(percentage.formatted())
  188. Text("%").foregroundColor(.secondary)
  189. }
  190. }
  191. }
  192. .font(.footnote)
  193. .padding(.top, 10)
  194. Divider()
  195. // Formula
  196. VStack(spacing: 5) {
  197. let unit = NSLocalizedString(
  198. " U",
  199. comment: "Unit in number of units delivered (keep the space character!)"
  200. )
  201. Text("(Eventual Glucose - Target) / ISF =").font(.callout).italic()
  202. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  203. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  204. HStack {
  205. Text(" = ").font(.callout)
  206. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  207. }
  208. if state.percentage != 100, state.insulin > 0 {
  209. Divider()
  210. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  211. Text(
  212. state.insulinRecommended.formatted() + unit
  213. ).font(.callout).foregroundColor(.blue).bold()
  214. }
  215. }
  216. }
  217. // Warning
  218. if state.error, state.insulinRecommended > 0 {
  219. VStack(spacing: 5) {
  220. Divider()
  221. Text("Warning!").font(.callout).bold().foregroundColor(.orange)
  222. Text(alertString()).font(.footnote)
  223. Divider()
  224. }.padding(.horizontal, 10)
  225. }
  226. if !(state.error && state.insulinRecommended > 0) {
  227. VStack {
  228. Text(
  229. "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."
  230. ).font(.caption2).foregroundColor(.secondary)
  231. }.padding(20)
  232. }
  233. // Hide button
  234. VStack {
  235. Button { presentInfo = false }
  236. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  237. .foregroundColor(.blue)
  238. }.padding(.bottom, 10)
  239. }
  240. .background(
  241. RoundedRectangle(cornerRadius: 8, style: .continuous)
  242. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  243. // .fill(Color(.systemGray).gradient) // A more prominent pop-up, but harder to read
  244. )
  245. }
  246. // Localize the Oref0 error/warning strings
  247. private func alertString() -> String {
  248. switch state.errorString {
  249. case 1:
  250. return NSLocalizedString(
  251. "Predicted Glucose, ",
  252. comment: "Bolus pop-up / Alert string. Make translations concise!"
  253. ) + state.minGuardBG
  254. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  255. .rawValue + ", " +
  256. NSLocalizedString(
  257. "is predicted below threshold ",
  258. comment: "Bolus pop-up / Alert string. Make translations concise!"
  259. ) + state
  260. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + "!"
  261. case 2:
  262. return NSLocalizedString(
  263. "Predicted Glucose, ",
  264. comment: "Bolus pop-up / Alert string. Make translations concise!"
  265. ) + state.minGuardBG
  266. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  267. .rawValue + ", " +
  268. NSLocalizedString(
  269. "is below Threshold of ",
  270. comment: "Bolus pop-up / Alert string. Make translations concise!"
  271. ) + state
  272. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  273. case 3:
  274. return NSLocalizedString(
  275. "Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ",
  276. comment: "Bolus pop-up / Alert string. Make translations concise!"
  277. ) +
  278. state.expectedDelta
  279. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  280. NSLocalizedString(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  281. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  282. case 4:
  283. return NSLocalizedString(
  284. "Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: ",
  285. comment: "Bolus pop-up / Alert string. Make translations concise!"
  286. ) +
  287. state.expectedDelta
  288. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  289. NSLocalizedString(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  290. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  291. case 5:
  292. return NSLocalizedString(
  293. "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
  294. comment: "Bolus pop-up / Alert string. Make translations concise!"
  295. ) +
  296. state.expectedDelta
  297. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  298. NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  299. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  300. case 6:
  301. return NSLocalizedString(
  302. "Minimum predicted Glucose is ",
  303. comment: "Bolus pop-up / Alert string. Make translations concise!"
  304. ) + state
  305. .minPredBG
  306. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  307. .units
  308. .rawValue
  309. default:
  310. return "Ignore Warning..."
  311. }
  312. }
  313. }
  314. }
  315. struct ActivityIndicator: UIViewRepresentable {
  316. @Binding var isAnimating: Bool
  317. let style: UIActivityIndicatorView.Style
  318. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  319. UIActivityIndicatorView(style: style)
  320. }
  321. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  322. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  323. }
  324. }