AddCarbsRootView.swift 13 KB

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