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