AddCarbsRootView.swift 12 KB

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