AddCarbsRootView.swift 12 KB

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