AddCarbsRootView.swift 12 KB

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