Skip to content

Commit

Permalink
Merge pull request #166 from rkozyak/Brian
Browse files Browse the repository at this point in the history
Made display for Challenges
  • Loading branch information
jvchblade authored Jul 26, 2024
2 parents 4630332 + 6a0b886 commit 71ffb12
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<CommunityChallengeAdapter.MyViewHolder> {
private Context context;
private ArrayList<String> list;

public CommunityChallengeAdapter(@NonNull Context context, ArrayList<String> 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<String, String> data = (HashMap<String, String>) 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> 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);
Expand All @@ -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
Expand Down Expand Up @@ -177,7 +234,7 @@ public void onClick(View v) {
new ArrayList<WorkoutPlan>(AddWorkoutCommunity.getReturnList()), dayInt,
monthInt, yearInt);

communityViewModel.addChallenge(userId, challenge);
communityViewModel.addChallenge(challenge);

AddWorkoutCommunity.getReturnList().clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions HealthTrack/app/src/main/res/layout/challenge_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardCornerRadius="0dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/challengeName"
android:fontFamily="@font/federo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="WORKOUT PLAN"
android:textColor="@color/black"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/challengeUser"
android:fontFamily="@font/darker_grotesque_semibold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="24dp"
android:hint="Author"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@+id/challengeName"
app:layout_constraintStart_toStartOf="@id/challengeName"
app:layout_constraintTop_toBottomOf="@id/challengeName" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

0 comments on commit 71ffb12

Please sign in to comment.