AddCarbsRootView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import SwiftUI
  2. import Swinject
  3. extension AddCarbs {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. private var formatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 0
  11. return formatter
  12. }
  13. var body: some View {
  14. Form {
  15. if let carbsReq = state.carbsRequired {
  16. Section {
  17. HStack {
  18. Text("Carbs required")
  19. Spacer()
  20. Text(formatter.string(from: carbsReq as NSNumber)! + " g")
  21. }
  22. }
  23. }
  24. Section {
  25. Section {
  26. HStack {
  27. Text("Carbs").fontWeight(.semibold)
  28. Spacer()
  29. DecimalTextField("0", value: $state.carbs, formatter: formatter, autofocus: true, cleanInput: true)
  30. Text("grams").foregroundColor(.secondary)
  31. }.padding(.vertical)
  32. // Adding Protein and Fat. Test
  33. HStack {
  34. Text("Protein").foregroundColor(.loopRed).fontWeight(.thin)
  35. Spacer()
  36. DecimalTextField(
  37. "0",
  38. value: $state.protein,
  39. formatter: formatter,
  40. autofocus: false,
  41. cleanInput: true
  42. ).foregroundColor(.loopRed)
  43. Text("grams").foregroundColor(.secondary)
  44. }
  45. HStack {
  46. Text("Fat").foregroundColor(.loopYellow).fontWeight(.thin)
  47. Spacer()
  48. DecimalTextField(
  49. "0",
  50. value: $state.fat,
  51. formatter: formatter,
  52. autofocus: false,
  53. cleanInput: true
  54. )
  55. Text("grams").foregroundColor(.secondary)
  56. }
  57. DatePicker("Date", selection: $state.date)
  58. }
  59. }
  60. Section {
  61. Button { state.add() }
  62. label: { Text("Add") }
  63. .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  64. }
  65. }
  66. .onAppear(perform: configureView)
  67. .navigationTitle("Add Carbs")
  68. .navigationBarTitleDisplayMode(.automatic)
  69. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  70. }
  71. }
  72. }