BolusRootView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. private var formatter: NumberFormatter {
  10. let formatter = NumberFormatter()
  11. formatter.numberStyle = .decimal
  12. formatter.maximumFractionDigits = 2
  13. return formatter
  14. }
  15. var body: some View {
  16. Form {
  17. Section(header: Text("Recommendation")) {
  18. if state.waitForSuggestion {
  19. HStack {
  20. Text("Wait please").foregroundColor(.secondary)
  21. Spacer()
  22. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  23. }
  24. } else {
  25. HStack {
  26. Text("Insulin required").foregroundColor(.secondary)
  27. Spacer()
  28. Text(
  29. formatter
  30. .string(from: state.insulinRequired as NSNumber)! +
  31. NSLocalizedString(" U", comment: "Insulin unit")
  32. ).foregroundColor(.secondary)
  33. }.contentShape(Rectangle())
  34. .onTapGesture {
  35. state.amount = max(Decimal(round(Double(state.insulinRequired) * 20) / 20.0), 0) // round to x.x5
  36. // state.amount = max(0, self.apsManager.roundBolus(amount: state.insulinRequired)) //throws arror about optional parameter being nil
  37. }
  38. HStack {
  39. Text("Insulin recommended")
  40. Spacer()
  41. Text(
  42. formatter
  43. .string(from: state.insulinRecommended as NSNumber)! +
  44. NSLocalizedString(" U", comment: "Insulin unit")
  45. ).foregroundColor(.secondary)
  46. }.contentShape(Rectangle())
  47. .onTapGesture {
  48. state.amount = state.insulinRecommended
  49. }
  50. }
  51. }
  52. if !state.waitForSuggestion {
  53. Section(header: Text("Bolus")) {
  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. Section {
  68. Button { state.add() }
  69. label: { Text("Enact bolus") }
  70. .disabled(state.amount <= 0)
  71. }
  72. Section {
  73. if waitForSuggestion {
  74. Button { state.showModal(for: nil) }
  75. label: { Text("Continue without bolus") }
  76. } else {
  77. Button { isAddInsulinAlertPresented = true }
  78. label: { Text("Add insulin without actually bolusing") }
  79. .disabled(state.amount <= 0)
  80. }
  81. }
  82. }
  83. }
  84. .alert(isPresented: $isAddInsulinAlertPresented) {
  85. let amount = formatter
  86. .string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  87. return Alert(
  88. title: Text("Are you sure?"),
  89. message: Text("Add \(amount) without bolusing"),
  90. primaryButton: .destructive(
  91. Text("Add"),
  92. action: { state.addWithoutBolus() }
  93. ),
  94. secondaryButton: .cancel()
  95. )
  96. }
  97. .onAppear {
  98. configureView {
  99. state.waitForSuggestionInitial = waitForSuggestion
  100. state.waitForSuggestion = waitForSuggestion
  101. }
  102. }
  103. .navigationTitle("Enact Bolus")
  104. .navigationBarTitleDisplayMode(.automatic)
  105. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  106. }
  107. }
  108. }
  109. // fix iOS 15 bug
  110. struct ActivityIndicator: UIViewRepresentable {
  111. @Binding var isAnimating: Bool
  112. let style: UIActivityIndicatorView.Style
  113. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  114. UIActivityIndicatorView(style: style)
  115. }
  116. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  117. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  118. }
  119. }