AddMealPresetView.swift 3.5 KB

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