BolusRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. var body: some View {
  20. Form {
  21. Section {
  22. if state.waitForSuggestion {
  23. HStack {
  24. Text("Wait please").foregroundColor(.secondary)
  25. Spacer()
  26. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  27. }
  28. } else {
  29. HStack {
  30. Text("Insulin recommended")
  31. Spacer()
  32. Text(
  33. formatter
  34. .string(from: state.insulinRecommended as NSNumber)! +
  35. NSLocalizedString(" U", comment: "Insulin unit")
  36. ).foregroundColor((state.error && state.insulinRecommended > 0) ? .red : .secondary)
  37. }.contentShape(Rectangle())
  38. .onTapGesture {
  39. if state.error, state.insulinRecommended > 0 { displayError = true }
  40. else { state.amount = state.insulinRecommended }
  41. }
  42. HStack {
  43. Image(systemName: "info.bubble").symbolRenderingMode(.palette).foregroundStyle(
  44. .primary, .blue
  45. )
  46. }.onTapGesture {
  47. presentInfo.toggle()
  48. }
  49. }
  50. }
  51. header: { Text("Recommendation") }
  52. if !state.waitForSuggestion {
  53. Section {
  54. HStack {
  55. Text("Amount")
  56. Spacer()
  57. DecimalTextField(
  58. "0",
  59. value: $state.amount,
  60. formatter: formatter,
  61. autofocus: true,
  62. cleanInput: true
  63. )
  64. Text("U").foregroundColor(.secondary)
  65. }
  66. }
  67. header: { Text("Bolus") }
  68. Section {
  69. Button { state.add() }
  70. label: { Text("Enact bolus") }
  71. .disabled(state.amount <= 0)
  72. }
  73. Section {
  74. if waitForSuggestion {
  75. Button { state.showModal(for: nil) }
  76. label: { Text("Continue without bolus") }
  77. } else {
  78. Button { isAddInsulinAlertPresented = true }
  79. label: { Text("Add insulin without actually bolusing") }
  80. .disabled(state.amount <= 0)
  81. }
  82. }
  83. }
  84. }
  85. .alert(isPresented: $isAddInsulinAlertPresented) {
  86. let amount = formatter
  87. .string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  88. return Alert(
  89. title: Text("Are you sure?"),
  90. message: Text("Add \(amount) without bolusing"),
  91. primaryButton: .destructive(
  92. Text("Add"),
  93. action: { state.addWithoutBolus() }
  94. ),
  95. secondaryButton: .cancel()
  96. )
  97. }
  98. .alert(isPresented: $displayError) {
  99. Alert(
  100. title: Text("Warning!"),
  101. message: Text("\n" + state.errorString + NSLocalizedString(
  102. "\n\nTap 'Add' to continue with selected amount.",
  103. comment: "Alert text to confirm bolus amount to add"
  104. )),
  105. primaryButton: .destructive(
  106. Text("Add"),
  107. action: {
  108. state.amount = state.insulinRecommended
  109. displayError = false
  110. }
  111. ),
  112. secondaryButton: .cancel()
  113. )
  114. }
  115. .onAppear {
  116. configureView {
  117. state.waitForSuggestionInitial = waitForSuggestion
  118. state.waitForSuggestion = waitForSuggestion
  119. state.manual = manualBolus
  120. }
  121. }
  122. .navigationTitle("Enact Bolus")
  123. .navigationBarTitleDisplayMode(.automatic)
  124. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  125. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  126. bolusInfo
  127. }
  128. }
  129. var bolusInfo: some View {
  130. VStack {
  131. // Variables
  132. VStack(spacing: 3) {
  133. HStack {
  134. Text("Eventual Glucose").foregroundColor(.secondary)
  135. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  136. let fractionDigit = state.units == .mmolL ? 1 : 0
  137. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigit))))
  138. Text(state.units.rawValue).foregroundColor(.secondary)
  139. }
  140. HStack {
  141. Text("Target Glucose").foregroundColor(.secondary)
  142. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  143. let fractionDigit = state.units == .mmolL ? 1 : 0
  144. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigit))))
  145. Text(state.units.rawValue).foregroundColor(.secondary)
  146. }
  147. HStack {
  148. Text("ISF").foregroundColor(.secondary)
  149. let isf = state.isf
  150. Text(isf.formatted())
  151. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  152. .foregroundColor(.secondary)
  153. }
  154. HStack {
  155. Text("ISF:")
  156. Text("Insulin Sensitivity")
  157. }.foregroundColor(.secondary).italic()
  158. if state.percentage != 100 {
  159. HStack {
  160. Text("Percentage setting").foregroundColor(.secondary)
  161. let percentage = state.percentage
  162. Text(percentage.formatted())
  163. Text("%").foregroundColor(.secondary)
  164. }
  165. }
  166. }
  167. .font(.footnote)
  168. .padding(.top, 10)
  169. Divider()
  170. // Formula
  171. VStack(spacing: 5) {
  172. let unit = NSLocalizedString(
  173. " U",
  174. comment: "Unit in number of units delivered (keep the space character!)"
  175. )
  176. Text("(Eventual Glucose - Target) / ISF =").font(.callout).italic()
  177. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  178. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  179. HStack {
  180. Text(" = ").font(.callout)
  181. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  182. }
  183. if state.percentage != 100, state.insulin > 0 {
  184. Divider()
  185. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  186. Text(
  187. state.insulinRecommended.formatted() + unit
  188. ).font(.callout).foregroundColor(.blue).bold()
  189. }
  190. }
  191. }
  192. // Warning
  193. VStack {
  194. Divider()
  195. if state.error, state.insulinRecommended > 0 {
  196. Text("Warning!").font(.callout).foregroundColor(.orange).bold()
  197. Text(state.errorString).font(.caption)
  198. Divider()
  199. }
  200. }.padding(.horizontal, 10)
  201. // Footer. Warning string .
  202. if !(state.error && state.insulinRecommended > 0) {
  203. VStack {
  204. Text(
  205. "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."
  206. ).font(.caption2).foregroundColor(.secondary)
  207. }.padding(20)
  208. }
  209. // Hide button
  210. VStack {
  211. Button { presentInfo = false }
  212. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  213. .foregroundColor(.blue)
  214. }.padding(.bottom, 10)
  215. }
  216. .background(
  217. RoundedRectangle(cornerRadius: 8, style: .continuous)
  218. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  219. )
  220. }
  221. }
  222. }
  223. // fix iOS 15 bug
  224. struct ActivityIndicator: UIViewRepresentable {
  225. @Binding var isAnimating: Bool
  226. let style: UIActivityIndicatorView.Style
  227. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  228. UIActivityIndicatorView(style: style)
  229. }
  230. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  231. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  232. }
  233. }