AddCarbsRootView.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // MARK: Adding Protein and Fat. Test
  33. if state.useFPU {
  34. HStack {
  35. Text("Protein").foregroundColor(.loopRed).fontWeight(.thin)
  36. Spacer()
  37. DecimalTextField(
  38. "0",
  39. value: $state.protein,
  40. formatter: formatter,
  41. autofocus: false,
  42. cleanInput: true
  43. ).foregroundColor(.loopRed)
  44. Text("grams").foregroundColor(.secondary)
  45. }
  46. HStack {
  47. Text("Fat").foregroundColor(.loopYellow).fontWeight(.thin)
  48. Spacer()
  49. DecimalTextField(
  50. "0",
  51. value: $state.fat,
  52. formatter: formatter,
  53. autofocus: false,
  54. cleanInput: true
  55. )
  56. Text("grams").foregroundColor(.secondary)
  57. }
  58. }
  59. DatePicker("Date", selection: $state.date)
  60. }
  61. }
  62. Section {
  63. Button { state.add() }
  64. label: { Text("Add") }
  65. .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  66. }
  67. }
  68. .onAppear(perform: configureView)
  69. .navigationTitle("Add Carbs")
  70. .navigationBarTitleDisplayMode(.automatic)
  71. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  72. }
  73. }
  74. }