AddMealPresetView.swift 3.5 KB

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