BolusRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // Footer
  227. if !(state.error && state.insulinRecommended > 0) {
  228. VStack {
  229. Text(
  230. "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."
  231. ).font(.caption2).foregroundColor(.secondary)
  232. }.padding(20)
  233. }
  234. // Hide button
  235. VStack {
  236. Button { presentInfo = false }
  237. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  238. .foregroundColor(.blue)
  239. }.padding(.bottom, 10)
  240. }
  241. .background(
  242. RoundedRectangle(cornerRadius: 8, style: .continuous)
  243. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  244. // .fill(Color(.systemGray).gradient) // A more prominent pop-up, but harder to read
  245. )
  246. }
  247. // Localize the Oref0 error/warning strings
  248. private func alertString() -> String {
  249. switch state.errorString {
  250. case 1:
  251. return NSLocalizedString(
  252. "Predicted Glucose, ",
  253. comment: "Bolus pop-up / Alert string. Make translations concise!"
  254. ) + state.minGuardBG
  255. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  256. .rawValue + ", " +
  257. NSLocalizedString(
  258. "is predicted below threshold ",
  259. comment: "Bolus pop-up / Alert string. Make translations concise!"
  260. ) + state
  261. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + "!"
  262. case 2:
  263. return NSLocalizedString(
  264. "Predicted Glucose, ",
  265. comment: "Bolus pop-up / Alert string. Make translations concise!"
  266. ) + state.minGuardBG
  267. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  268. .rawValue + ", " +
  269. NSLocalizedString(
  270. "is below Threshold of ",
  271. comment: "Bolus pop-up / Alert string. Make translations concise!"
  272. ) + state
  273. .threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  274. case 3:
  275. return NSLocalizedString(
  276. "Eventual Glucose > Target Glucose, but glucose is climbing slower 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(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  282. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  283. case 4:
  284. return NSLocalizedString(
  285. "Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: ",
  286. comment: "Bolus pop-up / Alert string. Make translations concise!"
  287. ) +
  288. state.expectedDelta
  289. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  290. NSLocalizedString(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  291. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  292. case 5:
  293. return NSLocalizedString(
  294. "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
  295. comment: "Bolus pop-up / Alert string. Make translations concise!"
  296. ) +
  297. state.expectedDelta
  298. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  299. NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  300. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  301. case 6:
  302. return NSLocalizedString(
  303. "Minimum predicted Glucose is ",
  304. comment: "Bolus pop-up / Alert string. Make translations concise!"
  305. ) + state
  306. .minPredBG
  307. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  308. .units
  309. .rawValue
  310. default:
  311. return "Ignore Warning..."
  312. }
  313. }
  314. }
  315. }
  316. struct ActivityIndicator: UIViewRepresentable {
  317. @Binding var isAnimating: Bool
  318. let style: UIActivityIndicatorView.Style
  319. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  320. UIActivityIndicatorView(style: style)
  321. }
  322. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  323. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  324. }
  325. }