AddCarbsRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension AddCarbs {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. let editMode: Bool
  8. @StateObject var state = StateModel()
  9. @State var dish: String = ""
  10. @State var isPromptPresented = false
  11. @State var saved = false
  12. @State private var showAlert = false
  13. @FocusState private var isFocused: Bool
  14. @FetchRequest(
  15. entity: Presets.entity(),
  16. sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
  17. ) var carbPresets: FetchedResults<Presets>
  18. @Environment(\.managedObjectContext) var moc
  19. private var formatter: NumberFormatter {
  20. let formatter = NumberFormatter()
  21. formatter.numberStyle = .decimal
  22. formatter.maximumFractionDigits = 1
  23. return formatter
  24. }
  25. var body: some View {
  26. Form {
  27. if let carbsReq = state.carbsRequired {
  28. Section {
  29. HStack {
  30. Text("Carbs required")
  31. Spacer()
  32. Text(formatter.string(from: carbsReq as NSNumber)! + " g")
  33. }
  34. }
  35. }
  36. Section {
  37. HStack {
  38. Text("Carbs").fontWeight(.semibold)
  39. Spacer()
  40. DecimalTextField(
  41. "0",
  42. value: $state.carbs,
  43. formatter: formatter,
  44. autofocus: true,
  45. cleanInput: true
  46. )
  47. Text("grams").foregroundColor(.secondary)
  48. }.padding(.vertical)
  49. if state.useFPUconversion {
  50. proteinAndFat()
  51. }
  52. HStack {
  53. Text("Note").foregroundColor(.secondary)
  54. TextField("", text: $state.note).multilineTextAlignment(.trailing)
  55. if state.note != "", isFocused {
  56. Button { isFocused = false } label: { Image(systemName: "keyboard.chevron.compact.down") }
  57. .controlSize(.mini)
  58. }
  59. }.focused($isFocused)
  60. .popover(isPresented: $isPromptPresented) {
  61. presetPopover
  62. }
  63. }
  64. Section {
  65. mealPresets
  66. }
  67. Section {
  68. Button { state.add() }
  69. label: { Text(state.skipBolus ? "Save" : "Continue") }
  70. .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
  71. .frame(maxWidth: .infinity, alignment: .center)
  72. } footer: { Text(state.waitersNotepad().description) }
  73. Section {
  74. DatePicker("Change Date", selection: $state.date)
  75. }
  76. }
  77. .onAppear {
  78. configureView {
  79. state.loadEntries(editMode)
  80. }
  81. }
  82. .navigationTitle("Add Meal")
  83. .navigationBarTitleDisplayMode(.inline)
  84. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  85. }
  86. var presetPopover: some View {
  87. Form {
  88. Section {
  89. TextField("Name Of Dish", text: $dish)
  90. Button {
  91. saved = true
  92. if dish != "", saved {
  93. let preset = Presets(context: moc)
  94. preset.dish = dish
  95. preset.fat = state.fat as NSDecimalNumber
  96. preset.protein = state.protein as NSDecimalNumber
  97. preset.carbs = state.carbs as NSDecimalNumber
  98. try? moc.save()
  99. state.addNewPresetToWaitersNotepad(dish)
  100. saved = false
  101. isPromptPresented = false
  102. }
  103. }
  104. label: { Text("Save") }
  105. Button {
  106. dish = ""
  107. saved = false
  108. isPromptPresented = false }
  109. label: { Text("Cancel") }
  110. } header: { Text("Enter Meal Preset Name") }
  111. }
  112. }
  113. var mealPresets: some View {
  114. Section {
  115. HStack {
  116. Button {
  117. isPromptPresented = true
  118. }
  119. label: { Text("Save as Preset") }
  120. .buttonStyle(BorderlessButtonStyle())
  121. .disabled(
  122. (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
  123. (
  124. (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
  125. .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
  126. .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
  127. .protein
  128. )
  129. )
  130. Picker("Select a Preset", selection: $state.selection) {
  131. Text("Presets").tag(nil as Presets?)
  132. ForEach(carbPresets, id: \.self) { (preset: Presets) in
  133. Text(preset.dish ?? "").tag(preset as Presets?)
  134. }
  135. }
  136. .labelsHidden()
  137. .frame(maxWidth: .infinity, alignment: .trailing)
  138. ._onBindingChange($state.selection) { _ in
  139. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  140. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  141. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  142. state.addToSummation()
  143. }
  144. }
  145. if state.selection != nil {
  146. HStack {
  147. Button("Delete Preset") {
  148. showAlert.toggle()
  149. }
  150. .disabled(state.selection == nil)
  151. .tint(.orange)
  152. .buttonStyle(BorderlessButtonStyle())
  153. .alert(
  154. "Delete preset '\(state.selection?.dish ?? "")'?",
  155. isPresented: $showAlert,
  156. actions: {
  157. Button("No", role: .cancel) {}
  158. Button("Yes", role: .destructive) {
  159. state.deletePreset()
  160. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  161. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  162. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  163. state.addPresetToNewMeal()
  164. }
  165. }
  166. )
  167. Button {
  168. if state.carbs != 0,
  169. (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  170. {
  171. state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
  172. } else { state.carbs = 0 }
  173. if state.fat != 0,
  174. (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  175. {
  176. state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
  177. } else { state.fat = 0 }
  178. if state.protein != 0,
  179. (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  180. {
  181. state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
  182. } else { state.protein = 0 }
  183. state.removePresetFromNewMeal()
  184. if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
  185. }
  186. label: { Text("[ -1 ]") }
  187. .disabled(
  188. state
  189. .selection == nil ||
  190. (
  191. !state.summation
  192. .contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
  193. )
  194. )
  195. .buttonStyle(BorderlessButtonStyle())
  196. .frame(maxWidth: .infinity, alignment: .trailing)
  197. .tint(.minus)
  198. Button {
  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. label: { Text("[ +1 ]") }
  205. .disabled(state.selection == nil)
  206. .buttonStyle(BorderlessButtonStyle())
  207. .tint(.blue)
  208. }
  209. }
  210. }
  211. }
  212. @ViewBuilder private func proteinAndFat() -> some View {
  213. HStack {
  214. Text("Fat").foregroundColor(.orange)
  215. Spacer()
  216. DecimalTextField(
  217. "0",
  218. value: $state.fat,
  219. formatter: formatter,
  220. autofocus: false,
  221. cleanInput: true
  222. )
  223. Text("grams").foregroundColor(.secondary)
  224. }
  225. HStack {
  226. Text("Protein").foregroundColor(.red)
  227. Spacer()
  228. DecimalTextField(
  229. "0",
  230. value: $state.protein,
  231. formatter: formatter,
  232. autofocus: false,
  233. cleanInput: true
  234. ).foregroundColor(.loopRed)
  235. Text("grams").foregroundColor(.secondary)
  236. }
  237. }
  238. }
  239. }