AddCarbsRootView.swift 10 KB

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