AddCarbsRootView.swift 8.4 KB

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