I am new to Swift and am building a nutrient tracking application for a project. I need the users foods to be saved to the day that they add them to their food diary section which displays the foods added to each meal.
So the user adds an apple for example, and this would be saved to today, and once the day passes, the displayed data is stored to the date.
I currently have the food items saving with a Date using CoreData and this is displayed in a list:
func saveBreakfast() {
let newBreakfastItem = BreakfastItem(context: self.moc)
newBreakfastItem.id = UUID()
newBreakfastItem.name = self.item.name
newBreakfastItem.calories = Int32(self.totalCalories)
newBreakfastItem.carbs = Int32(self.totalCarbs)
newBreakfastItem.protein = Int32(self.totalProtein)
newBreakfastItem.fat = Int32(self.totalFats)
newBreakfastItem.date = self.dateAdded
do {
if self.mocB.hasChanges { // saves only if changes are made
try? self.mocB.save()
}
}
}
ForEach(self.BreakfastItems, id: \.id) { newBreakfastItems in
HStack{
Text(newBreakfastItems.name ?? "Unknown")
.font(.headline)
.font(.headline)
HStack {
Text("Cal: \(Int32(newBreakfastItems.calories))")
.fontWeight(.light)
.foregroundColor(Color("Green Font"))
Text("F: \(Int32(newBreakfastItems.fat))")
.fontWeight(.ultraLight)
Text("P: \(Int32(newBreakfastItems.protein))")
.fontWeight(.ultraLight)
Text("C: \(Int32(newBreakfastItems.carbs))")
.fontWeight(.ultraLight)
However, Im not sure how to view the food saved to past days, as currently the app is just adding to the same list all the time, regardless of the day its saved on.
Would some form of ForEach Date statement work to cycle through views based on the day selected from the calendar?
Thanks in advance!