Skip to content

Commit

Permalink
Add adjustable date picker and undo button
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Bisenius committed May 19, 2024
1 parent 1a2bcdd commit 9c79eb7
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 76 deletions.
6 changes: 2 additions & 4 deletions Calorie Counter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
3801EAB12BF9188100C204C2 /* ProcessingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3801EAB02BF9188100C204C2 /* ProcessingView.swift */; };
3853E41F2BF80FEE00958AB4 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853E41E2BF80FEE00958AB4 /* Utilities.swift */; };
3853E4212BF8129900958AB4 /* MealProcessingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853E4202BF8129900958AB4 /* MealProcessingView.swift */; };
3853E4232BF8463B00958AB4 /* Weight.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853E4222BF8463B00958AB4 /* Weight.swift */; };
3853E4252BF84AD300958AB4 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853E4242BF84AD300958AB4 /* SettingsView.swift */; };
3853E4272BF85AC500958AB4 /* Miscellaneous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3853E4262BF85AC500958AB4 /* Miscellaneous.swift */; };
Expand All @@ -33,7 +32,6 @@
3801EAB02BF9188100C204C2 /* ProcessingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessingView.swift; sourceTree = "<group>"; };
3801EAB62BF946F600C204C2 /* Calorie-Counter-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "Calorie-Counter-Info.plist"; sourceTree = SOURCE_ROOT; };
3853E41E2BF80FEE00958AB4 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
3853E4202BF8129900958AB4 /* MealProcessingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MealProcessingView.swift; sourceTree = "<group>"; };
3853E4222BF8463B00958AB4 /* Weight.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weight.swift; sourceTree = "<group>"; };
3853E4242BF84AD300958AB4 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
3853E4262BF85AC500958AB4 /* Miscellaneous.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Miscellaneous.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -122,7 +120,6 @@
38C2ED712BF4815500B2C7C8 /* MacrosView.swift */,
38C2ED6B2BF4771600B2C7C8 /* LargeCardView.swift */,
3853E41E2BF80FEE00958AB4 /* Utilities.swift */,
3853E4202BF8129900958AB4 /* MealProcessingView.swift */,
3853E4262BF85AC500958AB4 /* Miscellaneous.swift */,
3853E42A2BF88E4B00958AB4 /* AI.swift */,
3853E42C2BF8906B00958AB4 /* Keys.swift */,
Expand Down Expand Up @@ -221,7 +218,6 @@
3853E41F2BF80FEE00958AB4 /* Utilities.swift in Sources */,
38C2ED702BF4804800B2C7C8 /* MainTabView.swift in Sources */,
38C2ED762BF4972E00B2C7C8 /* WelcomeView.swift in Sources */,
3853E4212BF8129900958AB4 /* MealProcessingView.swift in Sources */,
38C2ED682BF46C3E00B2C7C8 /* SummaryView.swift in Sources */,
3853E4272BF85AC500958AB4 /* Miscellaneous.swift in Sources */,
38C2ED6C2BF4771600B2C7C8 /* LargeCardView.swift in Sources */,
Expand Down Expand Up @@ -378,6 +374,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_UIUserInterfaceStyle = Light;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -418,6 +415,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_UIUserInterfaceStyle = Light;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Calorie Counter/Assets.xcassets/Calendar.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Calendar.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Calendar 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Calendar 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
23 changes: 23 additions & 0 deletions Calorie Counter/Assets.xcassets/Undo.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Undo.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Undo 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Undo 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 0 additions & 56 deletions Calorie Counter/Components/MealProcessingView.swift

This file was deleted.

32 changes: 32 additions & 0 deletions Calorie Counter/Components/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@
import Foundation
import SwiftUI

extension Date {
func isSameDay(as otherDate: Date) -> Bool {
let calendar = Calendar.current
return calendar.isDate(self, inSameDayAs: otherDate)
}
}

struct DatePickerView: View {
@Binding var selectedDate: Date
@Binding var isPresented: Bool

var body: some View {
VStack {
DatePicker("Select a Date", selection: $selectedDate, displayedComponents: .date)
.datePickerStyle(WheelDatePickerStyle())
.labelsHidden()

Button(action: {
isPresented = false
}) {
Text("Done")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding()
}
.padding()
}
}

func formatDate(_ date: Date) -> String {
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Calendar.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Calendar 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Calendar 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Undo.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Undo 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Undo 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 83 additions & 16 deletions Calorie Counter/Screens/SummaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ import SwiftData
import Foundation

struct SummaryView: View {
@State private var displayDate: String = ""
@State private var displayDate: Date = Date()
@State private var isDatePickerPresented: Bool = false
@Environment(\.modelContext) private var context

// Load meals
// Create a predicate for fetching meals created on the displayDate
private var datePredicate: Predicate<Meal> {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: displayDate)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
return #Predicate { meal in
meal.createdAt >= startOfDay && meal.createdAt < endOfDay
}
}

@Query private var meals: [Meal]

init() {
_meals = Query(filter: datePredicate)
}

@AppStorage("caloriesGoal") private var caloriesGoalStored: Int = -1
@AppStorage("proteinGoal") private var proteinGoalStored: Int = -1
Expand Down Expand Up @@ -45,17 +62,72 @@ struct SummaryView: View {
ScrollView {
VStack(alignment: .leading, spacing: 20) {
// Date and Summary Title
VStack(alignment: .leading, spacing: 5) {
Text("Summary")
.font(.largeTitle)
.fontWeight(.bold)
.onAppear {
// Initialize local state with stored values
caloriesGoal = caloriesGoalStored >= 0 ? caloriesGoalStored : nil
proteinGoal = proteinGoalStored >= 0 ? proteinGoalStored : nil
carbohydratesGoal = carbohydratesGoalStored >= 0 ? carbohydratesGoalStored : nil
fatsGoal = fatsGoalStored >= 0 ? fatsGoalStored : nil
HStack {
VStack(alignment: .leading, spacing: 5) {
Text(formatDate(displayDate))
.font(.subheadline)
.foregroundColor(.gray)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, alignment: .leading)
Text("Summary")
.font(.largeTitle)
.fontWeight(.bold)
.onAppear {
// Initialize local state with stored values
caloriesGoal = caloriesGoalStored >= 0 ? caloriesGoalStored : nil
proteinGoal = proteinGoalStored >= 0 ? proteinGoalStored : nil
carbohydratesGoal = carbohydratesGoalStored >= 0 ? carbohydratesGoalStored : nil
fatsGoal = fatsGoalStored >= 0 ? fatsGoalStored : nil
}
}
Button(action: {
isDatePickerPresented.toggle()
}) {
Image("Calendar")
.resizable()
.frame(width: 22, height: 22, alignment: .bottomTrailing)
.padding(.top, 20)
}
}

// Datepicker
if isDatePickerPresented {
VStack(spacing: 0) {
DatePicker("Select a Date", selection: $displayDate, displayedComponents: .date)
.datePickerStyle(WheelDatePickerStyle())
.labelsHidden()
HStack(spacing: 5) {
Button(action: {
isDatePickerPresented.toggle()
}) {
Text("Close Date Picker")
.fontWeight(.medium)
.frame(maxWidth: .infinity, alignment: .center)
}
.frame(maxWidth: .infinity)
.padding(10)
.background(Color.blue.opacity(0.2))
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.blue.opacity(0.65), lineWidth: 1)
)
Button(action: {
displayDate = Date()
}) {
Image("Undo")
.resizable()
.frame(width: 24, height: 24)
}
.padding(10)
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray.opacity(0.45), lineWidth: 1)
)
}
}
}

// Favorites Section
Expand Down Expand Up @@ -125,11 +197,6 @@ struct SummaryView: View {
}) {}
}
}
ForEach(meals.filter { $0.reviewedAt == nil }) { meal in
MealProcessingView(
createdAt: meal.createdAt
)
}
ForEach(meals.filter { $0.reviewedAt != nil }) { meal in
MealCardView(
label: meal.label ?? "N/A",
Expand Down

0 comments on commit 9c79eb7

Please sign in to comment.