BolusRootView.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import SwiftUI
  2. extension Bolus {
  3. struct RootView: BaseView {
  4. @EnvironmentObject var viewModel: ViewModel<Provider>
  5. @State private var isAddInsulinAlertPresented = false
  6. private var formatter: NumberFormatter {
  7. let formatter = NumberFormatter()
  8. formatter.numberStyle = .decimal
  9. formatter.maximumFractionDigits = 2
  10. return formatter
  11. }
  12. var body: some View {
  13. Form {
  14. Section(header: Text("Recommendation")) {
  15. if viewModel.waitForSuggestion {
  16. HStack {
  17. Text("Wait please").foregroundColor(.secondary)
  18. Spacer()
  19. ProgressView()
  20. }
  21. } else {
  22. HStack {
  23. Text("Insulin required").foregroundColor(.secondary)
  24. Spacer()
  25. Text(formatter.string(from: viewModel.inslinRequired as NSNumber)! + " U").foregroundColor(.secondary)
  26. }.contentShape(Rectangle())
  27. .onTapGesture {
  28. viewModel.amount = viewModel.inslinRecommended
  29. }
  30. HStack {
  31. Text("Insulin recommended")
  32. Spacer()
  33. Text(formatter.string(from: viewModel.inslinRecommended as NSNumber)! + " U")
  34. }.contentShape(Rectangle())
  35. .onTapGesture {
  36. viewModel.amount = viewModel.inslinRecommended
  37. }
  38. }
  39. }
  40. if !viewModel.waitForSuggestion {
  41. Section(header: Text("Bolus")) {
  42. HStack {
  43. Text("Amount")
  44. Spacer()
  45. DecimalTextField(
  46. "0",
  47. value: $viewModel.amount,
  48. formatter: formatter,
  49. autofocus: true,
  50. cleanInput: true
  51. )
  52. Text("U").foregroundColor(.secondary)
  53. }
  54. }
  55. Section {
  56. Button { viewModel.add() }
  57. label: { Text("Enact bolus") }
  58. .disabled(viewModel.amount <= 0)
  59. if viewModel.waitForSuggestionInitial {
  60. Button { viewModel.showModal(for: nil) }
  61. label: { Text("Continue without bolus") }
  62. }
  63. }
  64. Section {
  65. if !viewModel.waitForSuggestionInitial {
  66. Button { isAddInsulinAlertPresented = true }
  67. label: { Text("Add insulin without actually bolusing") }
  68. .disabled(viewModel.amount <= 0)
  69. }
  70. }
  71. }
  72. }
  73. .alert(isPresented: $isAddInsulinAlertPresented) {
  74. let amount = formatter.string(from: viewModel.amount as NSNumber)! + " U"
  75. return Alert(
  76. title: Text("Are your sure?"),
  77. message: Text("Add \(amount) without bolusing"),
  78. primaryButton: .destructive(
  79. Text("Add"),
  80. action: { viewModel.addWithoutBolus() }
  81. ),
  82. secondaryButton: .cancel()
  83. )
  84. }
  85. .navigationTitle("Enact Bolus")
  86. .navigationBarTitleDisplayMode(.automatic)
  87. .navigationBarItems(leading: Button("Close", action: viewModel.hideModal))
  88. }
  89. }
  90. }