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