Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrections for PIs. #57

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Stronger.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
38756AB22B946D0E00FDF599 /* ExerciseInfo.json in Resources */ = {isa = PBXBuildFile; fileRef = 38756AB12B946D0E00FDF599 /* ExerciseInfo.json */; };
38C4563E2B96CF3B009D69AA /* WorkoutHomeButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38C4563D2B96CF3B009D69AA /* WorkoutHomeButton.swift */; };
38FE1D022B716A42008F8BE0 /* WorkoutVideoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38FE1D012B7169DF008F8BE0 /* WorkoutVideoView.swift */; };
4002F0AC2B9AFC4D00AD3D47 /* MissedWorkoutTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4002F0AB2B9AFC4D00AD3D47 /* MissedWorkoutTest.swift */; };
4011141A2B7EE59A000083A2 /* ProgressCircle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 401114192B7EE59A000083A2 /* ProgressCircle.swift */; };
4011141D2B83FCD1000083A2 /* SummaryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4011141C2B83FCD1000083A2 /* SummaryView.swift */; };
401114232B8489D1000083A2 /* GoalResist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 401114222B8489D1000083A2 /* GoalResist.swift */; };
Expand Down Expand Up @@ -218,6 +219,7 @@
38756AB12B946D0E00FDF599 /* ExerciseInfo.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = ExerciseInfo.json; sourceTree = "<group>"; };
38C4563D2B96CF3B009D69AA /* WorkoutHomeButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WorkoutHomeButton.swift; sourceTree = "<group>"; };
38FE1D012B7169DF008F8BE0 /* WorkoutVideoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkoutVideoView.swift; sourceTree = "<group>"; };
4002F0AB2B9AFC4D00AD3D47 /* MissedWorkoutTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MissedWorkoutTest.swift; sourceTree = "<group>"; };
401114192B7EE59A000083A2 /* ProgressCircle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressCircle.swift; sourceTree = "<group>"; };
4011141C2B83FCD1000083A2 /* SummaryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SummaryView.swift; sourceTree = "<group>"; };
401114222B8489D1000083A2 /* GoalResist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalResist.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -566,6 +568,7 @@
40D4E47E2B97CB9900261632 /* HomeTest.swift */,
40D4E4812B97CBB800261632 /* TapSwitchTest.swift */,
40D4E4832B97CD0A00261632 /* WorkoutTest.swift */,
4002F0AB2B9AFC4D00AD3D47 /* MissedWorkoutTest.swift */,
);
path = StrongerUITests;
sourceTree = "<group>";
Expand Down Expand Up @@ -920,6 +923,7 @@
40D4E47F2B97CB9900261632 /* HomeTest.swift in Sources */,
2F4E237E2989A2FE0013F3D9 /* LaunchTests.swift in Sources */,
40D4E4822B97CBB800261632 /* TapSwitchTest.swift in Sources */,
4002F0AC2B9AFC4D00AD3D47 /* MissedWorkoutTest.swift in Sources */,
40D4E4842B97CD0A00261632 /* WorkoutTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Stronger/Account/Height.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
@Binding private var height: Int
public var body: some View {
HStack {
Text(HeightKey.name)
Text("\(HeightKey.name): inches")

Check warning on line 63 in Stronger/Account/Height.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Account/Height.swift#L63

Added line #L63 was not covered by tests
Spacer()
TextField("Height", value: $height, formatter: NumberFormatter())
.frame(width: 120) // set frame width to enable more spaces.
Expand Down
5 changes: 4 additions & 1 deletion Stronger/Account/StartDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
public static let category: AccountKeyCategory = .personalDetails

public static var initialValue: InitialValue<Value> {
.empty(Date())
.default(Date())

Check warning on line 26 in Stronger/Account/StartDate.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Account/StartDate.swift#L26

Added line #L26 was not covered by tests
}
// public static var initialValue: Date {
// Date()
// }
}

extension AccountKeys {
Expand Down
5 changes: 4 additions & 1 deletion Stronger/Account/StartDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
private var dateRange: ClosedRange<Date> {
let calendar = Calendar.current
let startDateComponents = DateComponents(year: 1800, month: 1, day: 1)
let endDate = Date.now
let curDate = Date.now
guard let endDate = Calendar.current.date(byAdding: .day, value: 1, to: curDate) else {
fatalError("Could not translate \(startDateComponents) to a valid date.")
}

Check warning on line 36 in Stronger/Account/StartDatePicker.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Account/StartDatePicker.swift#L33-L36

Added lines #L33 - L36 were not covered by tests

guard let startDate = calendar.date(from: startDateComponents) else {
fatalError("Could not translate \(startDateComponents) to a valid date.")
Expand Down
4 changes: 2 additions & 2 deletions Stronger/Account/Weight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

public var body: some View {
HStack {
Text(WeightKey.name)
Text("\(WeightKey.name): lbs")

Check warning on line 67 in Stronger/Account/Weight.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Account/Weight.swift#L67

Added line #L67 was not covered by tests
Spacer()
Button(action: {
showAlert.toggle() // Toggle alert visibility
Expand All @@ -81,7 +81,7 @@
)
}

TextField("Weight", value: $weight, formatter: NumberFormatter())
TextField("Weight: lbs", value: $weight, formatter: NumberFormatter())

Check warning on line 84 in Stronger/Account/Weight.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Account/Weight.swift#L84

Added line #L84 was not covered by tests
.frame(width: 120) // set frame width to enable more spaces.
}
}
Expand Down
4 changes: 2 additions & 2 deletions Stronger/Exercise/WorkoutHome.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
NavigationLink(
destination: WorkoutSelections(
presentingAccount: $presentingAccount,
selectedWeek: selectedWeek,
selectedDay: selectedDay
selectedWeek: selectedWeek + 1,
selectedDay: selectedDay + 1

Check warning on line 60 in Stronger/Exercise/WorkoutHome.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutHome.swift#L59-L60

Added lines #L59 - L60 were not covered by tests
)
) {
Text("Enter Workout Information\n for Week \(selectedWeek + 1) Day \(selectedDay + 1).")
Expand Down
139 changes: 40 additions & 99 deletions Stronger/Exercise/WorkoutSelection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,6 @@
// Ensure the width does not exceed the screen width
return min(desiredWidth, geometry.size.width * 0.5) // Set width as 50% of screen width
}
// private func adjustSelectedWeek(_ week: Int) -> Int {
// switch week {
// case 1...3:
// return 1
// case 4...6:
// return 4
// case 7...9:
// return 7
// case 10...12:
// return 10
// default:
// return week // Return the original value for any other cases
// }
// }

private func parseExercises(week: Int, day: Int) -> Workout? {
// Get the URL of the JSON file in the app bundle
Expand Down Expand Up @@ -255,6 +241,19 @@
}
}

private func populateUniqueValuesSet(from documents: [QueryDocumentSnapshot] = []) -> Set<String> {
var uniqueValuesSet = Set<String>()

// Iterate over documents to populate the set
documents.forEach { document in
if let exercise = document["exercise"] as? String {
uniqueValuesSet.insert(exercise)
}
}

return uniqueValuesSet
}

Check warning on line 255 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L244-L255

Added lines #L244 - L255 were not covered by tests

private func updateExerciseDate() async throws {
// Get current user ID
guard let userID = try await getCurrentUserID() else {
Expand All @@ -266,14 +265,18 @@
let dbe = Firestore.firestore()

let userDocRef = dbe.collection("users").document(userID).collection("exerciseLog")
let query = userDocRef.whereField("week", isEqualTo: self.selectedWeek)
let query = userDocRef.whereField("week", isEqualTo: self.selectedWeek ?? 1)

Check warning on line 268 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L268

Added line #L268 was not covered by tests

query.getDocuments { querySnapshot, error in
if let error = error {
print("Error fetching documents: \(error)")
} else {
guard let querySnapshot = querySnapshot else {
print("querySnapshot is nil")
return
}

Check warning on line 277 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L274-L277

Added lines #L274 - L277 were not covered by tests
// Get the count of documents
let documentCount = querySnapshot?.documents.count ?? 0
let documentCount = querySnapshot.documents.count

Check warning on line 279 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L279

Added line #L279 was not covered by tests

print("Number of documents returned: \(documentCount)")

Expand All @@ -283,14 +286,16 @@
self.selectedDay = 1
} else {
// Check if there are documents with selectedDay equal to 1
let selectedDay1Count = querySnapshot?.documents.filter { $0["exerciseDay"] as? Int == 2 }.count ?? 0
if selectedDay1Count > 3 {
let selectedDay2Count = populateUniqueValuesSet(from: querySnapshot.documents.filter { $0["exerciseDay"] as? Int == 2 }).count
if selectedDay2Count > 3 {

Check warning on line 290 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L289-L290

Added lines #L289 - L290 were not covered by tests
print("Documents found with selectedDay equal to 2. Setting selectedDay to 3.")
self.selectedDay = 3
} else {
// Check if there are documents with selectedDay equal to 2
let selectedDay2Count = querySnapshot?.documents.filter { $0["exerciseDay"] as? Int == 1 }.count ?? 0
if selectedDay2Count > 3 {
let selectedDay1Count = populateUniqueValuesSet(
from: querySnapshot.documents.filter { $0["exerciseDay"] as? Int == 1 }
).count
if selectedDay1Count > 3 {

Check warning on line 298 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L295-L298

Added lines #L295 - L298 were not covered by tests
print("Documents found with selectedDay equal to 1. Setting selectedDay to 2.")
self.selectedDay = 2
} else {
Expand All @@ -313,54 +318,11 @@
return details.accountId
}

// private func calculateWeeksElapsed(completion: @escaping (Int) -> Void) {
// // Get current user ID
// guard let userID = try await getCurrentUserID() else {
// completion(1) // Return default value if user ID cannot be retrieved
// return
// }
//
// // Reference to Firestore database
// let dbe = Firestore.firestore()
//
// // Reference to document for current user
// let userDocRef = dbe.collection("users").document(userID)
//
// // Get snapshot of document
// userDocRef.getDocument { userDocSnapshot, error in
// guard let userData = userDocSnapshot?.data(),
// let startDayTimestamp = userData["StartDateKey"] as? Timestamp else {
// print("Error retrieving start day timestamp:", error?.localizedDescription ?? "Unknown error")
// completion(1) // Return default value if start day timestamp cannot be retrieved
// return
// }
//
// // Get start date from Timestamp
// var startDayDate = startDayTimestamp.dateValue()
//
// // Get current date
// let currentDate = Date()
//
// // Get Calendar instance
// let calendar = Calendar.current
//
// // Move start day to closest Monday
// let weekday = calendar.component(.weekday, from: startDayDate)
// let daysToMonday = (7 - weekday + 2) % 7 // +2 because Sunday is 1-based in `weekday` but we want Monday to be 0-based
// startDayDate = calendar.date(byAdding: .day, value: -daysToMonday, to: startDayDate) ?? startDayDate
//
// // Calculate difference in weeks between start day and current date
// let weeksElapsed = calendar.dateComponents([.weekOfYear], from: startDayDate, to: currentDate).weekOfYear ?? 0
// let roundedWeeksElapsed = max(weeksElapsed, 0) // Ensure weeksElapsed is non-negative
// completion(roundedWeeksElapsed)
// }
// }

// Function to retrieve start day field and calculate weeks elapsed
private func calculateWeeksElapsed() async throws -> Int {
private func calculateWeeksElapsed() async throws -> Int? {
// Get current user ID
guard let userID = try await getCurrentUserID() else {
return 1
return nil
}

// Reference to Firestore database
Expand All @@ -375,13 +337,12 @@
// Check if document exists and contains start day field
guard let userData = userDocSnapshot.data(),
let startDayTimestamp = userData["StartDateKey"] as? Timestamp else {
print("did not find start day key", userID)
return 1
}

print("Found start date key", userID)
return nil
}

Check warning on line 342 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L340-L342

Added lines #L340 - L342 were not covered by tests
// Get start date from Timestamp
var startDayDate = startDayTimestamp.dateValue()
// Adjust time zones apparently
var startDayDate = startDayTimestamp.dateValue().addingTimeInterval(TimeInterval(TimeZone.current.secondsFromGMT()))

Check warning on line 345 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L344-L345

Added lines #L344 - L345 were not covered by tests

// Get current date
let currentDate = Date()
Expand All @@ -390,37 +351,17 @@
let calendar = Calendar.current

// Move start day to closest Monday
let weekday = calendar.component(.weekday, from: startDayDate)
let daysToMonday = (7 - weekday + 2) % 7 // +2 because Sunday is 1-based in `weekday` but we want Monday to be 0-based
print("daysTOMonday \(daysToMonday)")
startDayDate = calendar.date(byAdding: .day, value: -daysToMonday, to: startDayDate) ?? startDayDate
print("startDayDate \(startDayDate)")

// If startDayDate is Monday, keep it as is, otherwise move it to the previous Monday
if calendar.component(.weekday, from: startDayDate) != 2 {
// Calculate days to Monday
let weekday = calendar.component(.weekday, from: startDayDate)
let daysToMonday = (weekday - 2) % 7
startDayDate = calendar.date(byAdding: .day, value: -daysToMonday, to: startDayDate) ?? startDayDate
}

Check warning on line 360 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L354-L360

Added lines #L354 - L360 were not covered by tests
// Calculate difference in weeks between start day and current date
let weeksElapsed = calendar.dateComponents([.weekOfYear], from: startDayDate, to: currentDate).weekOfYear ?? 0
print("weeksElapsed \(weeksElapsed)")
let roundedWeeksElapsed = weeksElapsed > 0 ? weeksElapsed : 0 // Ensure weeksElapsed is non-negative
print("rounded weeks elapsed \(roundedWeeksElapsed)")
return weeksElapsed
}

private func uploadUserData() {
let dbe = Firestore.firestore()

// Data to be uploaded
let userData: [String: Any] = [
"name": "Rohan Gondor",
"role": "admin"
]

// Upload data to Firestore
dbe.collection("users").document("admin").setData(userData) { error in
if let error = error {
print("Error uploading user data: \(error.localizedDescription)")
} else {
print("User data uploaded successfully!")
}
}
return weeksElapsed + 1 // as we are using 1 based for selected week.

Check warning on line 364 in Stronger/Exercise/WorkoutSelection.swift

View check run for this annotation

Codecov / codecov/patch

Stronger/Exercise/WorkoutSelection.swift#L364

Added line #L364 was not covered by tests
}
}

Expand Down
Loading
Loading