AddMealPresetView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. private var color: LinearGradient {
  15. colorScheme == .dark ? LinearGradient(
  16. gradient: Gradient(colors: [
  17. Color.bgDarkBlue,
  18. Color.bgDarkerDarkBlue
  19. ]),
  20. startPoint: .top,
  21. endPoint: .bottom
  22. )
  23. :
  24. LinearGradient(
  25. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  26. startPoint: .top,
  27. endPoint: .bottom
  28. )
  29. }
  30. private var mealFormatter: NumberFormatter {
  31. let formatter = NumberFormatter()
  32. formatter.numberStyle = .decimal
  33. formatter.maximumFractionDigits = 1
  34. return formatter
  35. }
  36. var body: some View {
  37. NavigationStack {
  38. Form {
  39. Section {
  40. TextField("Name Of Dish", text: $dish)
  41. } header: {
  42. Text("New Preset")
  43. }
  44. .listRowBackground(Color.chart)
  45. Section {
  46. carbsTextField()
  47. if displayFatAndProtein {
  48. proteinAndFat()
  49. }
  50. }
  51. .listRowBackground(Color.chart)
  52. savePresetButton
  53. }
  54. .scrollContentBackground(.hidden).background(color)
  55. .navigationTitle("Add Meal Preset")
  56. .navigationBarTitleDisplayMode(.inline)
  57. .toolbar(content: {
  58. ToolbarItem(placement: .topBarLeading) {
  59. Button {
  60. onCancel()
  61. } label: {
  62. Text("Cancel")
  63. }
  64. }
  65. })
  66. }
  67. }
  68. @ViewBuilder private func carbsTextField() -> some View {
  69. HStack {
  70. Text("Carbs").fontWeight(.semibold)
  71. Spacer()
  72. TextFieldWithToolBar(
  73. text: $presetCarbs,
  74. placeholder: "0",
  75. keyboardType: .numberPad,
  76. numberFormatter: mealFormatter
  77. )
  78. Text("g").foregroundColor(.secondary)
  79. }
  80. }
  81. @ViewBuilder private func proteinAndFat() -> some View {
  82. HStack {
  83. Text("Fat").foregroundColor(.orange)
  84. Spacer()
  85. TextFieldWithToolBar(text: $presetFat, placeholder: "0", keyboardType: .numberPad, numberFormatter: mealFormatter)
  86. Text("g").foregroundColor(.secondary)
  87. }
  88. HStack {
  89. Text("Protein").foregroundColor(.red)
  90. Spacer()
  91. TextFieldWithToolBar(
  92. text: $presetProtein,
  93. placeholder: "0",
  94. keyboardType: .numberPad,
  95. numberFormatter: mealFormatter
  96. )
  97. Text("g").foregroundColor(.secondary)
  98. }
  99. }
  100. private var savePresetButton: some View {
  101. Button {
  102. onSave()
  103. }
  104. label: {
  105. Text("Save")
  106. .font(.headline)
  107. .foregroundStyle(Color.white)
  108. .frame(maxWidth: .infinity, alignment: .center)
  109. }
  110. .listRowBackground(Color(.systemBlue))
  111. .shadow(radius: 3)
  112. .clipShape(RoundedRectangle(cornerRadius: 8))
  113. }
  114. }