AddMealPresetView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import CoreData
  2. import Foundation
  3. import Observation
  4. import SwiftUI
  5. struct AddMealPresetView: View {
  6. @Binding var dish: String
  7. @Binding var presetCarbs: Decimal
  8. @Binding var presetFat: Decimal
  9. @Binding var presetProtein: Decimal
  10. @Binding var displayFatAndProtein: Bool
  11. var onSave: () -> Void
  12. var onCancel: () -> Void
  13. @Environment(\.colorScheme) private var colorScheme
  14. @Environment(AppState.self) var appState
  15. private var mealFormatter: NumberFormatter {
  16. let formatter = NumberFormatter()
  17. formatter.numberStyle = .decimal
  18. formatter.maximumFractionDigits = 1
  19. return formatter
  20. }
  21. var body: some View {
  22. NavigationStack {
  23. Form {
  24. Section {
  25. TextField("Name Of Dish", text: $dish)
  26. } header: {
  27. Text("New Preset")
  28. }
  29. .listRowBackground(Color.chart)
  30. Section {
  31. carbsTextField()
  32. if displayFatAndProtein {
  33. proteinAndFat()
  34. }
  35. }
  36. .listRowBackground(Color.chart)
  37. savePresetButton
  38. }
  39. .scrollContentBackground(.hidden)
  40. .background(appState.trioBackgroundColor(for: colorScheme))
  41. .navigationTitle("Add Meal Preset")
  42. .navigationBarTitleDisplayMode(.inline)
  43. .toolbar(content: {
  44. ToolbarItem(placement: .topBarLeading) {
  45. Button {
  46. onCancel()
  47. } label: {
  48. Text("Cancel")
  49. }
  50. }
  51. })
  52. }
  53. }
  54. @ViewBuilder private func carbsTextField() -> some View {
  55. HStack {
  56. Text("Carbs").fontWeight(.semibold)
  57. Spacer()
  58. TextFieldWithToolBar(
  59. text: $presetCarbs,
  60. placeholder: "0",
  61. keyboardType: .numberPad,
  62. numberFormatter: mealFormatter
  63. )
  64. Text("g").foregroundColor(.secondary)
  65. }
  66. }
  67. @ViewBuilder private func proteinAndFat() -> some View {
  68. HStack {
  69. Text("Fat").foregroundColor(.orange)
  70. Spacer()
  71. TextFieldWithToolBar(text: $presetFat, placeholder: "0", keyboardType: .numberPad, numberFormatter: mealFormatter)
  72. Text("g").foregroundColor(.secondary)
  73. }
  74. HStack {
  75. Text("Protein").foregroundColor(.red)
  76. Spacer()
  77. TextFieldWithToolBar(
  78. text: $presetProtein,
  79. placeholder: "0",
  80. keyboardType: .numberPad,
  81. numberFormatter: mealFormatter
  82. )
  83. Text("g").foregroundColor(.secondary)
  84. }
  85. }
  86. private var savePresetButton: some View {
  87. Button {
  88. onSave()
  89. }
  90. label: {
  91. Text("Save")
  92. .font(.headline)
  93. .foregroundStyle(Color.white)
  94. .frame(maxWidth: .infinity, alignment: .center)
  95. }
  96. .listRowBackground(Color(.systemBlue))
  97. .shadow(radius: 3)
  98. .clipShape(RoundedRectangle(cornerRadius: 8))
  99. }
  100. }