AddCarbsRootView.swift 11 KB

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