AddCarbsRootView.swift 9.7 KB

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