AddCarbsRootView.swift 12 KB

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