AddCarbsRootView.swift 13 KB

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