BolusRootView.swift 3.1 KB

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