AddCarbsRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 noteSaved = false
  11. @State var mealSaved = 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(state.carbs > state.maxCarbs ? "⚠️" : "g").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 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. isPromtPresented = 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: $isPromtPresented) {
  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 {
  113. mealSaved = true
  114. state.add()
  115. }
  116. label: { Text(state.saveButtonText()).font(.title3) }
  117. .disabled(
  118. mealSaved
  119. || state.carbs > state.maxCarbs
  120. || state.fat > state.maxFat
  121. || state.protein > state.maxProtein
  122. || (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  123. )
  124. .foregroundStyle(
  125. mealSaved || (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ? .gray :
  126. state.carbs > state.maxCarbs || state.fat > state.maxFat || state.protein > state
  127. .maxProtein ? .red : .blue
  128. )
  129. .frame(maxWidth: .infinity, alignment: .center)
  130. } footer: { Text(state.waitersNotepad().description) }
  131. if !state.useFPUconversion {
  132. Section {
  133. mealPresets
  134. }
  135. }
  136. }
  137. .onAppear(perform: configureView)
  138. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  139. }
  140. var presetPopover: some View {
  141. Form {
  142. Section {
  143. TextField("Name Of Dish", text: $dish)
  144. Button {
  145. noteSaved = true
  146. if dish != "", noteSaved {
  147. let preset = Presets(context: moc)
  148. preset.dish = dish
  149. preset.fat = state.fat as NSDecimalNumber
  150. preset.protein = state.protein as NSDecimalNumber
  151. preset.carbs = state.carbs as NSDecimalNumber
  152. try? moc.save()
  153. state.addNewPresetToWaitersNotepad(dish)
  154. noteSaved = false
  155. isPromtPresented = false
  156. }
  157. }
  158. label: { Text("Save") }
  159. Button {
  160. dish = ""
  161. noteSaved = false
  162. isPromtPresented = false }
  163. label: { Text("Cancel") }
  164. } header: { Text("Enter Meal Preset Name") }
  165. }
  166. }
  167. var mealPresets: some View {
  168. Section {
  169. VStack {
  170. Picker("Meal Presets", selection: $state.selection) {
  171. Text("Empty").tag(nil as Presets?)
  172. ForEach(carbPresets, id: \.self) { (preset: Presets) in
  173. Text(preset.dish ?? "").tag(preset as Presets?)
  174. }
  175. }
  176. .pickerStyle(.automatic)
  177. ._onBindingChange($state.selection) { _ in
  178. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  179. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  180. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  181. state.addToSummation()
  182. }
  183. }
  184. HStack {
  185. Button("Delete Preset") {
  186. showAlert.toggle()
  187. }
  188. .disabled(state.selection == nil)
  189. .accentColor(.orange)
  190. .buttonStyle(BorderlessButtonStyle())
  191. .alert(
  192. "Delete preset '\(state.selection?.dish ?? "")'?",
  193. isPresented: $showAlert,
  194. actions: {
  195. Button("No", role: .cancel) {}
  196. Button("Yes", role: .destructive) {
  197. state.deletePreset()
  198. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  199. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  200. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  201. state.addPresetToNewMeal()
  202. }
  203. }
  204. )
  205. Button {
  206. if state.carbs != 0,
  207. (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  208. {
  209. state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
  210. } else { state.carbs = 0 }
  211. if state.fat != 0,
  212. (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  213. {
  214. state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
  215. } else { state.fat = 0 }
  216. if state.protein != 0,
  217. (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  218. {
  219. state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
  220. } else { state.protein = 0 }
  221. state.removePresetFromNewMeal()
  222. if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
  223. }
  224. label: { Text("[ -1 ]") }
  225. .disabled(
  226. state
  227. .selection == nil ||
  228. (
  229. !state.summation.contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
  230. )
  231. )
  232. .buttonStyle(BorderlessButtonStyle())
  233. .frame(maxWidth: .infinity, alignment: .trailing)
  234. .accentColor(.minus)
  235. Button {
  236. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  237. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  238. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  239. state.addPresetToNewMeal()
  240. }
  241. label: { Text("[ +1 ]") }
  242. .disabled(state.selection == nil)
  243. .buttonStyle(BorderlessButtonStyle())
  244. .accentColor(.blue)
  245. }
  246. }
  247. }
  248. @ViewBuilder private func proteinAndFat() -> some View {
  249. HStack {
  250. Text("Fat").foregroundColor(.orange) // .fontWeight(.thin)
  251. Spacer()
  252. DecimalTextField(
  253. "0",
  254. value: $state.fat,
  255. formatter: formatter,
  256. autofocus: false,
  257. cleanInput: true
  258. )
  259. Text(state.fat > state.maxFat ? "⚠️" : "g").foregroundColor(.secondary)
  260. }
  261. HStack {
  262. Text("Protein").foregroundColor(.red) // .fontWeight(.thin)
  263. Spacer()
  264. DecimalTextField(
  265. "0",
  266. value: $state.protein,
  267. formatter: formatter,
  268. autofocus: false,
  269. cleanInput: true
  270. )
  271. Text(state.protein > state.maxProtein ? "⚠️" : "g").foregroundColor(.secondary)
  272. }
  273. }
  274. }
  275. }