BolusRootView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  60. Section {
  61. if viewModel.waitForSuggestionInitial {
  62. Button { viewModel.showModal(for: nil) }
  63. label: { Text("Continue without bolus") }
  64. } else {
  65. Button { isAddInsulinAlertPresented = true }
  66. label: { Text("Add insulin without actually bolusing") }
  67. .disabled(viewModel.amount <= 0)
  68. }
  69. }
  70. }
  71. }
  72. .alert(isPresented: $isAddInsulinAlertPresented) {
  73. let amount = formatter.string(from: viewModel.amount as NSNumber)! + " U"
  74. return Alert(
  75. title: Text("Are your sure?"),
  76. message: Text("Add \(amount) without bolusing"),
  77. primaryButton: .destructive(
  78. Text("Add"),
  79. action: { viewModel.addWithoutBolus() }
  80. ),
  81. secondaryButton: .cancel()
  82. )
  83. }
  84. .navigationTitle("Enact Bolus")
  85. .navigationBarTitleDisplayMode(.automatic)
  86. .navigationBarItems(leading: Button("Close", action: viewModel.hideModal))
  87. }
  88. }
  89. }