BolusRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. Image(systemName: "info.bubble")
  36. .symbolRenderingMode(.palette)
  37. .foregroundStyle(.primary, .blue)
  38. .onTapGesture {
  39. presentInfo.toggle()
  40. }
  41. Spacer()
  42. Text(
  43. formatter
  44. .string(from: state.insulinRecommended as NSNumber)! +
  45. NSLocalizedString(" U", comment: "Insulin unit")
  46. ).foregroundColor((state.error && state.insulinRecommended > 0) ? .red : .secondary)
  47. .onTapGesture {
  48. if state.error, state.insulinRecommended > 0 { displayError = true }
  49. else { state.amount = state.insulinRecommended }
  50. }
  51. }.contentShape(Rectangle())
  52. }
  53. }
  54. header: { Text("Recommendation") }
  55. if !state.waitForSuggestion {
  56. Section {
  57. HStack {
  58. Text("Amount")
  59. Spacer()
  60. DecimalTextField(
  61. "0",
  62. value: $state.amount,
  63. formatter: formatter,
  64. autofocus: true,
  65. cleanInput: true
  66. )
  67. Text(!(state.amount > state.maxBolus) ? "U" : "😵").foregroundColor(.secondary)
  68. }
  69. }
  70. header: { Text("Bolus") }
  71. Section {
  72. Button { state.add() }
  73. label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
  74. .frame(maxWidth: .infinity, alignment: .center)
  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(state.amount <= 0 || state.amount > state.maxBolus * 3)
  87. }
  88. }
  89. .alert(isPresented: $isAddInsulinAlertPresented) {
  90. let isOverMax = state.amount > state.maxBolus ? true : false
  91. let secondParagrap1 = "Add"
  92. let secondParagraph2 = " U"
  93. let secondParagraph3 = " without bolusing"
  94. let insulinAmount = formatter.string(from: state.amount as NSNumber)!
  95. // Actual alert
  96. return Alert(
  97. title: Text(
  98. isOverMax ? "Warning" : "Are you sure?"
  99. ),
  100. message:
  101. Text(
  102. isOverMax ? (
  103. NSLocalizedString(
  104. "\nAmount is more than your Max Bolus setting! \nAre you sure you want to add ",
  105. comment: "Alert"
  106. ) + insulinAmount +
  107. NSLocalizedString(secondParagraph2, comment: "Insulin unit") +
  108. NSLocalizedString(secondParagraph3, comment: "Add insulin without bolusing alert") + "?"
  109. ) :
  110. NSLocalizedString(secondParagrap1, comment: "Add insulin without bolusing alert") +
  111. " " +
  112. insulinAmount +
  113. NSLocalizedString(secondParagraph2, comment: "Insulin unit") +
  114. NSLocalizedString(secondParagraph3, comment: "Add insulin without bolusing alert")
  115. ),
  116. primaryButton: .destructive(
  117. Text("Add"),
  118. action: {
  119. state.addWithoutBolus()
  120. isAddInsulinAlertPresented = false
  121. }
  122. ),
  123. secondaryButton: .cancel()
  124. )
  125. }
  126. }
  127. }
  128. .alert(isPresented: $displayError) {
  129. Alert(
  130. title: Text("Warning!"),
  131. message: Text("\n" + alertString() + "\n"),
  132. primaryButton: .destructive(
  133. Text("Add"),
  134. action: {
  135. state.amount = state.insulinRecommended
  136. displayError = false
  137. }
  138. ),
  139. secondaryButton: .cancel()
  140. )
  141. }.onAppear {
  142. configureView {
  143. state.waitForSuggestionInitial = waitForSuggestion
  144. state.waitForSuggestion = waitForSuggestion
  145. }
  146. }
  147. .navigationTitle("Enact Bolus")
  148. .navigationBarTitleDisplayMode(.inline)
  149. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  150. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  151. bolusInfo
  152. }
  153. }
  154. var bolusInfo: some View {
  155. VStack {
  156. // Variables
  157. VStack(spacing: 3) {
  158. HStack {
  159. Text("Eventual Glucose").foregroundColor(.secondary)
  160. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  161. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  162. Text(state.units.rawValue).foregroundColor(.secondary)
  163. }
  164. HStack {
  165. Text("Target Glucose").foregroundColor(.secondary)
  166. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  167. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  168. Text(state.units.rawValue).foregroundColor(.secondary)
  169. }
  170. HStack {
  171. Text("ISF").foregroundColor(.secondary)
  172. let isf = state.isf
  173. Text(isf.formatted())
  174. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  175. .foregroundColor(.secondary)
  176. }
  177. HStack {
  178. Text("ISF:")
  179. Text("Insulin Sensitivity")
  180. }.foregroundColor(.secondary).italic()
  181. if state.percentage != 100 {
  182. HStack {
  183. Text("Percentage setting").foregroundColor(.secondary)
  184. let percentage = state.percentage
  185. Text(percentage.formatted())
  186. Text("%").foregroundColor(.secondary)
  187. }
  188. }
  189. HStack {
  190. Text("Formula:")
  191. Text("(Eventual Glucose - Target) / ISF")
  192. }.foregroundColor(.secondary).italic().padding(.top, 5)
  193. }
  194. .font(.footnote)
  195. .padding(.top, 10)
  196. Divider()
  197. // Formula
  198. VStack(spacing: 5) {
  199. let unit = NSLocalizedString(
  200. " U",
  201. comment: "Unit in number of units delivered (keep the space character!)"
  202. )
  203. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  204. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  205. HStack {
  206. Text(NSLocalizedString("Insulin recommended", comment: "") + ":").font(.callout)
  207. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  208. }
  209. if state.percentage != 100, state.insulin > 0 {
  210. Divider()
  211. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  212. Text(
  213. state.insulinRecommended.formatted() + unit
  214. ).font(.callout).foregroundColor(.blue).bold()
  215. }
  216. }
  217. }
  218. // Warning
  219. if state.error, state.insulinRecommended > 0 {
  220. VStack(spacing: 5) {
  221. Divider()
  222. Text("Warning!").font(.callout).bold().foregroundColor(.orange)
  223. Text(alertString()).font(.footnote)
  224. Divider()
  225. }.padding(.horizontal, 10)
  226. }
  227. // Footer
  228. if !(state.error && state.insulinRecommended > 0) {
  229. VStack {
  230. Text(
  231. "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."
  232. ).font(.caption2).foregroundColor(.secondary)
  233. }.padding(20)
  234. }
  235. // Hide button
  236. VStack {
  237. Button { presentInfo = false }
  238. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  239. .foregroundColor(.blue)
  240. }.padding(.bottom, 10)
  241. }
  242. .background(
  243. RoundedRectangle(cornerRadius: 8, style: .continuous)
  244. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  245. // .fill(Color(.systemGray).gradient) // A more prominent pop-up, but harder to read
  246. )
  247. }
  248. // Localize the Oref0 error/warning strings. The default should never be returned
  249. private func alertString() -> String {
  250. switch state.errorString {
  251. case 1,
  252. 2:
  253. return NSLocalizedString(
  254. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  255. comment: "Bolus pop-up / Alert string. Make translations concise!"
  256. ) + state.minGuardBG
  257. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  258. .rawValue + ", " +
  259. NSLocalizedString(
  260. "which is below your Threshold (",
  261. comment: "Bolus pop-up / Alert string. Make translations concise!"
  262. ) + state
  263. .threshold.formatted() + " " + state.units.rawValue + ")"
  264. case 3:
  265. return NSLocalizedString(
  266. "Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ",
  267. comment: "Bolus pop-up / Alert string. Make translations concise!"
  268. ) +
  269. state.expectedDelta
  270. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  271. NSLocalizedString(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  272. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  273. case 4:
  274. return NSLocalizedString(
  275. "Eventual Glucose > Target Glucose, but glucose is falling faster 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(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  281. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  282. case 5:
  283. return NSLocalizedString(
  284. "Eventual Glucose > Target Glucose, but glucose is changing faster 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(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  290. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  291. case 6:
  292. return NSLocalizedString(
  293. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  294. comment: "Bolus pop-up / Alert string. Make translations concise!"
  295. ) + state
  296. .minPredBG
  297. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  298. .units
  299. .rawValue
  300. default:
  301. return "Ignore Warning..."
  302. }
  303. }
  304. }
  305. }
  306. struct ActivityIndicator: UIViewRepresentable {
  307. @Binding var isAnimating: Bool
  308. let style: UIActivityIndicatorView.Style
  309. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  310. UIActivityIndicatorView(style: style)
  311. }
  312. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  313. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  314. }
  315. }