BolusRootView.swift 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  74. .string(from: viewModel.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  75. return Alert(
  76. title: Text("Are you 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. }