AddCarbsRootView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. HStack {
  36. Text("Carbs").fontWeight(.semibold)
  37. Spacer()
  38. DecimalTextField(
  39. "0",
  40. value: $state.carbs,
  41. formatter: formatter,
  42. autofocus: true,
  43. cleanInput: true
  44. )
  45. Text("grams").foregroundColor(.secondary)
  46. }.padding(.vertical)
  47. if state.useFPU {
  48. proteinAndFat()
  49. }
  50. HStack {
  51. Button {
  52. isPromtPresented = true
  53. }
  54. label: { Text("Save as Preset") }
  55. }
  56. .frame(maxWidth: .infinity, alignment: .trailing)
  57. .controlSize(.mini)
  58. .buttonStyle(BorderlessButtonStyle())
  59. .disabled(
  60. (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
  61. (
  62. (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
  63. .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
  64. .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
  65. .protein
  66. )
  67. )
  68. .popover(isPresented: $isPromtPresented) {
  69. presetPopover
  70. }
  71. }
  72. if state.useFPU {
  73. Section {
  74. mealPresets
  75. }
  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").font(.title3) }
  83. .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  84. .frame(maxWidth: .infinity, alignment: .center)
  85. }
  86. if !state.useFPU {
  87. Section {
  88. mealPresets
  89. }
  90. }
  91. }
  92. .onAppear(perform: configureView)
  93. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  94. }
  95. var presetPopover: some View {
  96. Form {
  97. Section(header: Text("Enter Meal Preset Name")) {
  98. TextField("Name Of Dish", text: $dish)
  99. Button {
  100. saved = true
  101. if dish != "", saved {
  102. let preset = Presets(context: moc)
  103. preset.dish = dish
  104. preset.fat = state.fat as NSDecimalNumber
  105. preset.protein = state.protein as NSDecimalNumber
  106. preset.carbs = state.carbs as NSDecimalNumber
  107. try? moc.save()
  108. state.addNewPresetToWaitersNotepad(dish)
  109. saved = false
  110. isPromtPresented = false
  111. }
  112. }
  113. label: { Text("Save") }
  114. Button {
  115. dish = ""
  116. saved = false
  117. isPromtPresented = false }
  118. label: { Text("Cancel") }
  119. }
  120. }
  121. }
  122. var mealPresets: some View {
  123. Section {
  124. VStack {
  125. Picker("Meal Presets", selection: $state.selection) {
  126. Text("Empty").tag(nil as Presets?)
  127. ForEach(carbPresets, id: \.self) { (preset: Presets) in
  128. Text(preset.dish ?? "").tag(preset as Presets?)
  129. }
  130. }
  131. .pickerStyle(.automatic)
  132. ._onBindingChange($state.selection) { _ in
  133. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  134. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  135. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  136. state.addToSummation()
  137. }
  138. }
  139. HStack {
  140. Button("Delete Preset") {
  141. showAlert.toggle()
  142. }
  143. .disabled(state.selection == nil)
  144. .accentColor(.orange)
  145. .buttonStyle(BorderlessButtonStyle())
  146. .alert(
  147. "Delete preset '\(state.selection?.dish ?? "")'?",
  148. isPresented: $showAlert,
  149. actions: {
  150. Button("No", role: .cancel) {}
  151. Button("Yes", role: .destructive) {
  152. state.deletePreset()
  153. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  154. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  155. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  156. state.addPresetToNewMeal()
  157. }
  158. }
  159. )
  160. Button {
  161. if state.carbs != 0,
  162. (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  163. {
  164. state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
  165. } else { state.carbs = 0 }
  166. if state.fat != 0,
  167. (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  168. {
  169. state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
  170. } else { state.fat = 0 }
  171. if state.protein != 0,
  172. (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  173. {
  174. state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
  175. } else { state.protein = 0 }
  176. state.removePresetFromNewMeal()
  177. if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
  178. }
  179. label: { Text("[ -1 ]") }
  180. .disabled(
  181. state
  182. .selection == nil ||
  183. (
  184. !state.summation.contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
  185. )
  186. )
  187. .buttonStyle(BorderlessButtonStyle())
  188. .frame(maxWidth: .infinity, alignment: .trailing)
  189. .accentColor(.minus)
  190. Button {
  191. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  192. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  193. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  194. state.addPresetToNewMeal()
  195. }
  196. label: { Text("[ +1 ]") }
  197. .disabled(state.selection == nil)
  198. .buttonStyle(BorderlessButtonStyle())
  199. .accentColor(.blue)
  200. }
  201. }
  202. }
  203. @ViewBuilder private func proteinAndFat() -> some View {
  204. HStack {
  205. Text("Protein").foregroundColor(.red) // .fontWeight(.thin)
  206. Spacer()
  207. DecimalTextField(
  208. "0",
  209. value: $state.protein,
  210. formatter: formatter,
  211. autofocus: false,
  212. cleanInput: true
  213. ).foregroundColor(.loopRed)
  214. Text("grams").foregroundColor(.secondary)
  215. }
  216. HStack {
  217. Text("Fat").foregroundColor(.orange) // .fontWeight(.thin)
  218. Spacer()
  219. DecimalTextField(
  220. "0",
  221. value: $state.fat,
  222. formatter: formatter,
  223. autofocus: false,
  224. cleanInput: true
  225. )
  226. Text("grams").foregroundColor(.secondary)
  227. }
  228. }
  229. }
  230. }