diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/model/ChallengeDatabaseRepository.java b/HealthTrack/app/src/main/java/com/example/healthtrack/model/ChallengeDatabaseRepository.java index a8fcca1..8f4e504 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/model/ChallengeDatabaseRepository.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/model/ChallengeDatabaseRepository.java @@ -11,13 +11,12 @@ public ChallengeDatabaseRepository() { db = challengeDatabase.getDatabaseReference(); } - public void addChallenge(String userId, CommunityChallenge communityChallenge, + public void addChallenge(CommunityChallenge communityChallenge, DatabaseReference.CompletionListener completionListener) { - DatabaseReference userRef = db.child(userId); - String challengeId = userRef.push().getKey(); + String challengeId = db.push().getKey(); if (challengeId != null) { - userRef.child(challengeId).setValue(communityChallenge, completionListener); + db.child(challengeId).setValue(communityChallenge, completionListener); } } } diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityChallengeAdapter.java b/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityChallengeAdapter.java new file mode 100644 index 0000000..abb19d0 --- /dev/null +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityChallengeAdapter.java @@ -0,0 +1,86 @@ +package com.example.healthtrack.view; +import android.content.Context; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.PopupWindow; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.RecyclerView; + +import com.example.healthtrack.R; +import com.example.healthtrack.model.ChallengeDatabase; +import com.example.healthtrack.model.CommunityChallenge; +import com.example.healthtrack.model.Workout; +import com.example.healthtrack.model.WorkoutPlan; +import com.example.healthtrack.viewModel.WorkoutViewModel; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.ValueEventListener; + +import org.w3c.dom.Text; + +import java.util.ArrayList; +import java.util.HashMap; + +public class CommunityChallengeAdapter extends RecyclerView.Adapter { + private Context context; + private ArrayList list; + + public CommunityChallengeAdapter(@NonNull Context context, ArrayList list) { + this.context = context; + this.list = list; + } + + @NonNull + @Override + public CommunityChallengeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(context).inflate(R.layout.challenge_item, parent, false); + return new MyViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull CommunityChallengeAdapter.MyViewHolder holder, int position) { + String challengeID = list.get(position); + DatabaseReference database = ChallengeDatabase.getInstance().getDatabaseReference().child(challengeID); + database.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot snapshot) { + HashMap data = (HashMap) snapshot.getValue(); + holder.userID.setText(data.get("userId")); + holder.challengeName.setText(data.get("name")); + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + + } + }); + } + + @Override + public int getItemCount() { + return list.size(); + } + + public static class MyViewHolder extends RecyclerView.ViewHolder { + private TextView userID; + private TextView challengeName; + + public MyViewHolder(@NonNull View itemView) { + super(itemView); + + userID = itemView.findViewById(R.id.challengeUser); + challengeName = itemView.findViewById(R.id.challengeName); + } + } +} \ No newline at end of file diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityScreen.java b/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityScreen.java index 4886e33..f1fff46 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityScreen.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/view/CommunityScreen.java @@ -12,18 +12,34 @@ import android.widget.Toast; import androidx.activity.EdgeToEdge; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultCallback; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.example.healthtrack.R; +import com.example.healthtrack.model.ChallengeDatabase; import com.example.healthtrack.model.CommunityChallenge; import com.example.healthtrack.model.Observer; import com.example.healthtrack.model.WorkoutPlan; import com.example.healthtrack.viewModel.CommunityViewModel; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; +import com.google.firebase.database.ChildEventListener; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; public class CommunityScreen extends AppCompatActivity implements Observer { private Dialog dialog; @@ -32,13 +48,23 @@ public class CommunityScreen extends AppCompatActivity implements Observer { private Button btnDialogWorkout; private FirebaseAuth mAuth; private CommunityViewModel communityViewModel; + private DatabaseReference db; + private RecyclerView recyclerView; + private ArrayList challengeList; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_community_screen); + recyclerView = findViewById(R.id.challengeList); + challengeList = new ArrayList<>(); + recyclerView.setHasFixedSize(true); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + CommunityChallengeAdapter adapter = new CommunityChallengeAdapter(this, challengeList); + recyclerView.setAdapter(adapter); communityViewModel = new ViewModelProvider(this).get(CommunityViewModel.class); + db = ChallengeDatabase.getInstance().getDatabaseReference(); mAuth = FirebaseAuth.getInstance(); dialog = new Dialog(CommunityScreen.this); dialog.setContentView(R.layout.add_challenge_popout); @@ -54,6 +80,37 @@ protected void onCreate(Bundle savedInstanceState) { Toast.LENGTH_SHORT).show(); } + db.addChildEventListener(new ChildEventListener() { + @Override + public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + String workoutId = snapshot.getKey(); + challengeList.add(workoutId); + recyclerView.setAdapter(new CommunityChallengeAdapter(CommunityScreen.this, challengeList)); + } + + @Override + public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + String workoutId = snapshot.getKey(); + challengeList.add(workoutId); + recyclerView.setAdapter(new CommunityChallengeAdapter(CommunityScreen.this, challengeList)); + } + + @Override + public void onChildRemoved(@NonNull DataSnapshot snapshot) { + recyclerView.setAdapter(new CommunityChallengeAdapter(CommunityScreen.this, challengeList)); + } + + @Override + public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + recyclerView.setAdapter(new CommunityChallengeAdapter(CommunityScreen.this, challengeList)); + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + recyclerView.setAdapter(new CommunityChallengeAdapter(CommunityScreen.this, challengeList)); + } + }); + Button backButton = findViewById(R.id.btn_community_back); backButton.setOnClickListener(new View.OnClickListener() { @Override @@ -177,7 +234,7 @@ public void onClick(View v) { new ArrayList(AddWorkoutCommunity.getReturnList()), dayInt, monthInt, yearInt); - communityViewModel.addChallenge(userId, challenge); + communityViewModel.addChallenge(challenge); AddWorkoutCommunity.getReturnList().clear(); diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/viewModel/CommunityViewModel.java b/HealthTrack/app/src/main/java/com/example/healthtrack/viewModel/CommunityViewModel.java index 03b6c09..895bbec 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/viewModel/CommunityViewModel.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/viewModel/CommunityViewModel.java @@ -23,8 +23,8 @@ public CommunityViewModel() { communityChallenge.addObserver(new WorkoutPlans()); } - public void addChallenge(String userId, CommunityChallenge communityChallenge) { - challengeDatabaseRepository.addChallenge(userId, communityChallenge, + public void addChallenge(CommunityChallenge communityChallenge) { + challengeDatabaseRepository.addChallenge(communityChallenge, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, diff --git a/HealthTrack/app/src/main/res/layout/challenge_item.xml b/HealthTrack/app/src/main/res/layout/challenge_item.xml new file mode 100644 index 0000000..4bd8e31 --- /dev/null +++ b/HealthTrack/app/src/main/res/layout/challenge_item.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file