Skip to content

Commit

Permalink
Merge branch 'main' into Brian
Browse files Browse the repository at this point in the history
  • Loading branch information
jvchblade authored Jul 26, 2024
2 parents 9c4c019 + 4630332 commit 6a0b886
Show file tree
Hide file tree
Showing 19 changed files with 460 additions and 306 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.healthtrack.model;

import java.time.LocalDateTime;

public class CompletionTimestampDecorator extends WorkoutDecorator {
private LocalDateTime completionTimestamp;

public CompletionTimestampDecorator(WorkoutComponent workout) {
super(workout);
}

public void markCompleted() {
completionTimestamp = LocalDateTime.now();
}

@Override
public void perform() {
super.perform();
if (completionTimestamp != null) {
System.out.println("Workout completed at: " + completionTimestamp);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.healthtrack.model;

public class ParticipantTrackingDecorator extends WorkoutDecorator {
private int participants;

public ParticipantTrackingDecorator(WorkoutComponent workout) {
super(workout);
}

public void addParticipant() {
participants++;
}

@Override
public void perform() {
super.perform();
System.out.println("Number of participants: " + participants);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.example.healthtrack.model;

import com.example.healthtrack.model.SortingStrategy;

import java.util.ArrayList;

public class SortingContext {
public static ArrayList<WorkoutPlan> filter(SortingStrategy strategy, ArrayList<WorkoutPlan> list) {
public static ArrayList<WorkoutPlan> filter(SortingStrategy strategy,
ArrayList<WorkoutPlan> list) {
return strategy.sort(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Date;

public class Workout {
public class Workout implements WorkoutComponent {
private String userId;
private String name;
private Integer caloriesPerSet;
Expand Down Expand Up @@ -33,6 +33,11 @@ public Workout(String userId, String name, Integer caloriesPerSet,
this.date = date;
}

@Override
public void perform() {
System.out.println("Performing workout: " + name);
}

public String getUserId() {
return userId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.healthtrack.model;

public interface WorkoutComponent {
void perform();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.healthtrack.model;

public abstract class WorkoutDecorator implements WorkoutComponent {
protected WorkoutComponent decoratedWorkout;

public WorkoutDecorator(WorkoutComponent workout) {
this.decoratedWorkout = workout;
}

@Override
public void perform() {
decoratedWorkout.perform();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,91 @@ public void addWorkoutPlan(String userId, WorkoutPlan workoutPlan,
// Check for null values
if (userId == null || workoutPlan == null) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null);
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null);
}
return; // Exit the method early if any parameter is null
}

// Check for zero or negative calories
if (workoutPlan.getCaloriesPerSet() == null || workoutPlan.getCaloriesPerSet() <= 0) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null); // Use a relevant error code
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null); // Use a relevant error code
}
return;
}

// Check for empty workout plan name
if (workoutPlan.getName() == null || workoutPlan.getName().trim().isEmpty()) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null);
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null);
}
return;
}

// Check for empty notes
if (workoutPlan.getNotes() == null || workoutPlan.getNotes().trim().isEmpty()) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null); // Use a relevant error code
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null); // Use a relevant error code
}
return;
}

// Check for invalid reps
if (workoutPlan.getRepsPerSet() < 0) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null);
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null);
}
return;
}

// Check for negative sets
if (workoutPlan.getSets() < 0) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null);
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR),
null);
}
return;
}

DatabaseReference userRef = db.child(userId);

// Check for duplicates
userRef.orderByChild("name").equalTo(workoutPlan.getName()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// Duplicate found
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null); // Use a relevant error code
userRef.orderByChild("name").equalTo(workoutPlan.getName()).
addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// Duplicate found
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(
DatabaseError.UNKNOWN_ERROR), null);
}
} else {
String workoutPlanId = userRef.push().getKey();
if (workoutPlanId != null) {
userRef.child(workoutPlanId).setValue(workoutPlan,
completionListener);
} else {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(
DatabaseError.UNKNOWN_ERROR), null);
}
}
}
}
} else {
String workoutPlanId = userRef.push().getKey();
if (workoutPlanId != null) {
userRef.child(workoutPlanId).setValue(workoutPlan, completionListener);
} else {

@Override
public void onCancelled(DatabaseError databaseError) {
if (completionListener != null) {
completionListener.onComplete(DatabaseError.fromCode(DatabaseError.UNKNOWN_ERROR), null);
completionListener.onComplete(databaseError, null);
}
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
if (completionListener != null) {
completionListener.onComplete(databaseError, null);
}
}
});
});
}

//Note: Gets USER SPECIFIC workouts
Expand All @@ -104,8 +114,10 @@ public DatabaseReference getWorkoutPlansReference(String userId) {

// used for test case
public void getWorkoutPlan(String userId, String planName, ValueEventListener listener) {
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users").child(userId);
userRef.child("workoutPlans").orderByChild("name").equalTo(planName).addListenerForSingleValueEvent(listener);
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users").
child(userId);
userRef.child("workoutPlans").orderByChild("name").equalTo(planName).
addListenerForSingleValueEvent(listener);
}

}
Loading

0 comments on commit 6a0b886

Please sign in to comment.