AddCarbsRootView.swift 12 KB

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