AddCarbsRootView.swift 12 KB

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