AddCarbsRootView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension AddCarbs {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State var dish: String = ""
  9. @State var isPromtPresented = false
  10. @State var saved = false
  11. @State private var showAlert = false
  12. @FetchRequest(
  13. entity: Presets.entity(),
  14. sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
  15. ) var carbPresets: FetchedResults<Presets>
  16. @Environment(\.managedObjectContext) var moc
  17. private var formatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 1
  21. return formatter
  22. }
  23. var body: some View {
  24. Form {
  25. if let carbsReq = state.carbsRequired {
  26. Section {
  27. HStack {
  28. Text("Carbs required")
  29. Spacer()
  30. Text(formatter.string(from: carbsReq as NSNumber)! + " g")
  31. }
  32. }
  33. }
  34. Section {
  35. Section {
  36. HStack {
  37. Text("Carbs").fontWeight(.semibold)
  38. Spacer()
  39. DecimalTextField(
  40. "0",
  41. value: $state.carbs,
  42. formatter: formatter,
  43. autofocus: true,
  44. cleanInput: true
  45. )
  46. Text("grams").foregroundColor(.secondary)
  47. }.padding(.vertical)
  48. if state.useFPU {
  49. proteinAndFat()
  50. }
  51. HStack {
  52. Button {
  53. isPromtPresented = true
  54. }
  55. label: { Text("Save as Preset") }
  56. }
  57. .frame(maxWidth: .infinity, alignment: .trailing)
  58. .controlSize(.mini)
  59. .buttonStyle(BorderlessButtonStyle())
  60. .disabled(
  61. (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
  62. (
  63. (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
  64. .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
  65. .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
  66. .protein
  67. )
  68. )
  69. .popover(isPresented: $isPromtPresented) {
  70. presetPopover
  71. }
  72. }
  73. }
  74. Section {
  75. mealPresets
  76. }
  77. Section {
  78. DatePicker("Date", selection: $state.date)
  79. }
  80. Section(footer: Text(state.waitersNotepad().description)) {
  81. Button { state.add() }
  82. label: { Text("Save and continue") }
  83. .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  84. }
  85. }
  86. .onAppear(perform: configureView)
  87. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  88. }
  89. var presetPopover: some View {
  90. Form {
  91. Section(header: Text("Enter Meal Preset Name")) {
  92. TextField("Name Of Dish", text: $dish)
  93. Button {
  94. saved = true
  95. if dish != "", saved {
  96. let preset = Presets(context: moc)
  97. preset.dish = dish
  98. preset.fat = state.fat as NSDecimalNumber
  99. preset.protein = state.protein as NSDecimalNumber
  100. preset.carbs = state.carbs as NSDecimalNumber
  101. try? moc.save()
  102. state.selection = preset
  103. saved = false
  104. isPromtPresented = false
  105. }
  106. }
  107. label: { Text("Save") }
  108. Button {
  109. dish = ""
  110. saved = false
  111. isPromtPresented = false }
  112. label: { Text("Cancel") }
  113. }
  114. }
  115. }
  116. var mealPresets: some View {
  117. Section {
  118. VStack {
  119. Picker("Meal Presets", selection: $state.selection) {
  120. Text("Empty").tag(nil as Presets?)
  121. ForEach(carbPresets, id: \.self) { (preset: Presets) in
  122. Text(preset.dish ?? "").tag(preset as Presets?)
  123. }
  124. }
  125. .pickerStyle(.automatic)
  126. ._onBindingChange($state.selection) { _ in
  127. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  128. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  129. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  130. state.summation.append(state.selection?.dish ?? "")
  131. }
  132. }
  133. HStack {
  134. Button("Delete Preset") {
  135. showAlert.toggle()
  136. }
  137. .disabled(state.selection == nil)
  138. .accentColor(.orange)
  139. .buttonStyle(BorderlessButtonStyle())
  140. .alert(
  141. "Delete preset '\(state.selection?.dish ?? "")'?",
  142. isPresented: $showAlert,
  143. actions: {
  144. Button("No", role: .cancel) {}
  145. Button("Yes", role: .destructive) {
  146. state.deletePreset()
  147. }
  148. }
  149. )
  150. Button {
  151. if state.carbs != 0,
  152. (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) > 0
  153. {
  154. state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
  155. } else { state.carbs = 0 }
  156. if state.fat != 0,
  157. (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) > 0
  158. {
  159. state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
  160. } else { state.fat = 0 }
  161. if state.protein != 0,
  162. (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) > 0
  163. {
  164. state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
  165. } else { state.protein = 0 }
  166. state.removePresetFromNewMeal()
  167. }
  168. label: { Text("[ -1 ]") }
  169. .disabled(state.selection == nil || (
  170. (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
  171. .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
  172. .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
  173. .protein
  174. ))
  175. .buttonStyle(BorderlessButtonStyle())
  176. .frame(maxWidth: .infinity, alignment: .trailing)
  177. .accentColor(.minus)
  178. Button {
  179. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  180. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  181. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  182. state.addPresetToNewMeal()
  183. }
  184. label: { Text("[ +1 ]") }
  185. .disabled(state.selection == nil)
  186. .buttonStyle(BorderlessButtonStyle())
  187. .accentColor(.blue)
  188. }
  189. }
  190. }
  191. @ViewBuilder private func proteinAndFat() -> some View {
  192. HStack {
  193. Text("Protein").foregroundColor(.red) // .fontWeight(.thin)
  194. Spacer()
  195. DecimalTextField(
  196. "0",
  197. value: $state.protein,
  198. formatter: formatter,
  199. autofocus: false,
  200. cleanInput: true
  201. ).foregroundColor(.loopRed)
  202. Text("grams").foregroundColor(.secondary)
  203. }
  204. HStack {
  205. Text("Fat").foregroundColor(.orange) // .fontWeight(.thin)
  206. Spacer()
  207. DecimalTextField(
  208. "0",
  209. value: $state.fat,
  210. formatter: formatter,
  211. autofocus: false,
  212. cleanInput: true
  213. )
  214. Text("grams").foregroundColor(.secondary)
  215. }
  216. }
  217. }
  218. }