BolusRootView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(
  26. formatter
  27. .string(from: viewModel.inslinRequired as NSNumber)! +
  28. NSLocalizedString(" U", comment: "Insulin unit")
  29. ).foregroundColor(.secondary)
  30. }.contentShape(Rectangle())
  31. .onTapGesture {
  32. viewModel.amount = viewModel.inslinRecommended
  33. }
  34. HStack {
  35. Text("Insulin recommended")
  36. Spacer()
  37. Text(
  38. formatter
  39. .string(from: viewModel.inslinRequired as NSNumber)! +
  40. NSLocalizedString(" U", comment: "Insulin unit")
  41. ).foregroundColor(.secondary)
  42. }.contentShape(Rectangle())
  43. .onTapGesture {
  44. viewModel.amount = viewModel.inslinRecommended
  45. }
  46. }
  47. }
  48. if !viewModel.waitForSuggestion {
  49. Section(header: Text("Bolus")) {
  50. HStack {
  51. Text("Amount")
  52. Spacer()
  53. DecimalTextField(
  54. "0",
  55. value: $viewModel.amount,
  56. formatter: formatter,
  57. autofocus: true,
  58. cleanInput: true
  59. )
  60. Text("U").foregroundColor(.secondary)
  61. }
  62. }
  63. Section {
  64. Button { viewModel.add() }
  65. label: { Text("Enact bolus") }
  66. .disabled(viewModel.amount <= 0)
  67. }
  68. Section {
  69. if viewModel.waitForSuggestionInitial {
  70. Button { viewModel.showModal(for: nil) }
  71. label: { Text("Continue without bolus") }
  72. } else {
  73. Button { isAddInsulinAlertPresented = true }
  74. label: { Text("Add insulin without actually bolusing") }
  75. .disabled(viewModel.amount <= 0)
  76. }
  77. }
  78. }
  79. }
  80. .alert(isPresented: $isAddInsulinAlertPresented) {
  81. let amount = formatter
  82. .string(from: viewModel.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  83. return Alert(
  84. title: Text("Are you sure?"),
  85. message: Text("Add \(amount) without bolusing"),
  86. primaryButton: .destructive(
  87. Text("Add"),
  88. action: { viewModel.addWithoutBolus() }
  89. ),
  90. secondaryButton: .cancel()
  91. )
  92. }
  93. .navigationTitle("Enact Bolus")
  94. .navigationBarTitleDisplayMode(.automatic)
  95. .navigationBarItems(leading: Button("Close", action: viewModel.hideModal))
  96. }
  97. }
  98. }