AddCarbsRootView.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. HStack {
  26. Text("Amount")
  27. Spacer()
  28. DecimalTextField("0", value: $state.carbs, formatter: formatter, autofocus: true, cleanInput: true)
  29. Text("grams").foregroundColor(.secondary)
  30. }
  31. DatePicker("Date", selection: $state.date)
  32. }
  33. Section {
  34. Button { state.add() }
  35. label: { Text("Add") }
  36. .disabled(state.carbs <= 0)
  37. }
  38. }
  39. .onAppear(perform: configureView)
  40. .navigationTitle("Add Carbs")
  41. .navigationBarTitleDisplayMode(.automatic)
  42. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  43. }
  44. }
  45. }