This commit is contained in:
2026-04-14 17:22:27 +01:00
parent a79ee68417
commit 6a1e604b96
5 changed files with 326 additions and 81 deletions

View File

@@ -10,6 +10,7 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.vdcscore.R;
import com.example.vdcscore.databinding.FragmentGalleryBinding; import com.example.vdcscore.databinding.FragmentGalleryBinding;
import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseError;
@@ -23,8 +24,12 @@ import java.util.List;
public class GalleryFragment extends Fragment { public class GalleryFragment extends Fragment {
private FragmentGalleryBinding binding; private FragmentGalleryBinding binding;
private MatchdaysAdapter adapter; private MatchesAdapter adapter;
private DatabaseReference mDatabase; private DatabaseReference mDatabase;
private List<Matchday> matchdaysList = new ArrayList<>();
private int currentJornadaIndex = 0;
private String currentCategory = "seniores";
private ValueEventListener currentListener;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
@@ -33,57 +38,119 @@ public class GalleryFragment extends Fragment {
View root = binding.getRoot(); View root = binding.getRoot();
// Initialize RecyclerView // Initialize RecyclerView
adapter = new MatchdaysAdapter(); adapter = new MatchesAdapter();
binding.recyclerMatchdays.setLayoutManager(new LinearLayoutManager(getContext())); binding.recyclerMatchdays.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerMatchdays.setAdapter(adapter); binding.recyclerMatchdays.setAdapter(adapter);
// Initialize Firebase // Setup Toggle Group
mDatabase = FirebaseDatabase.getInstance().getReference("matchdays"); binding.toggleGroupCategory.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
if (isChecked) {
if (checkedId == R.id.btn_seniores) {
currentCategory = "seniores";
} else if (checkedId == R.id.btn_juniores) {
currentCategory = "juniores";
}
fetchMatchdays();
}
});
// Fetch Data // Setup Navigation Buttons
binding.btnPrevJornada.setOnClickListener(v -> {
if (currentJornadaIndex > 0) {
currentJornadaIndex--;
updateUI();
}
});
binding.btnNextJornada.setOnClickListener(v -> {
if (currentJornadaIndex < matchdaysList.size() - 1) {
currentJornadaIndex++;
updateUI();
}
});
// Initial Fetch
fetchMatchdays(); fetchMatchdays();
return root; return root;
} }
private void fetchMatchdays() { private void fetchMatchdays() {
mDatabase.addValueEventListener(new ValueEventListener() { if (mDatabase != null && currentListener != null) {
mDatabase.removeEventListener(currentListener);
}
binding.textJornadaName.setText("Carregando...");
mDatabase = FirebaseDatabase.getInstance().getReference("jornadas").child(currentCategory);
currentListener = new ValueEventListener() {
@Override @Override
public void onDataChange(@NonNull DataSnapshot snapshot) { public void onDataChange(@NonNull DataSnapshot snapshot) {
if (binding == null) return; if (binding == null) return;
List<Matchday> matchdays = new ArrayList<>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) { matchdaysList.clear();
Matchday matchday = new Matchday(); for (DataSnapshot jornadaSnap : snapshot.getChildren()) {
matchday.setId(postSnapshot.getKey()); String name = jornadaSnap.getKey();
matchday.setName(postSnapshot.child("name").getValue(String.class));
List<Match> matches = new ArrayList<>(); List<Match> matches = new ArrayList<>();
for (DataSnapshot matchSnapshot : postSnapshot.child("matches").getChildren()) {
Match match = matchSnapshot.getValue(Match.class); for (DataSnapshot matchSnap : jornadaSnap.getChildren()) {
Match match = matchSnap.getValue(Match.class);
if (match != null) { if (match != null) {
match.setId(matchSnapshot.getKey()); match.setId(matchSnap.getKey());
matches.add(match); matches.add(match);
} }
} }
matchday.setMatches(matches);
matchdays.add(matchday); if (!matches.isEmpty()) {
matchdaysList.add(new Matchday(name, matches));
}
}
if (matchdaysList.isEmpty()) {
binding.textJornadaName.setText("Sem jornadas");
adapter.setMatches(new ArrayList<>());
} else {
// Try to stay on the same index or go to the first one
if (currentJornadaIndex >= matchdaysList.size()) {
currentJornadaIndex = matchdaysList.size() - 1;
}
updateUI();
} }
adapter.setMatchdays(matchdays);
} }
@Override @Override
public void onCancelled(@NonNull DatabaseError error) { public void onCancelled(@NonNull DatabaseError error) {
if (getContext() != null) { if (getContext() != null) {
Toast.makeText(getContext(), "Erro ao carregar jornadas: " + error.getMessage(), Toast.LENGTH_SHORT) Toast.makeText(getContext(), "Erro ao carregar: " + error.getMessage(), Toast.LENGTH_SHORT).show();
.show();
} }
} }
}); };
mDatabase.addValueEventListener(currentListener);
}
private void updateUI() {
if (matchdaysList.isEmpty()) return;
Matchday currentMatchday = matchdaysList.get(currentJornadaIndex);
binding.textJornadaName.setText(currentMatchday.getName());
adapter.setMatches(currentMatchday.getMatches());
// Enable/Disable navigation buttons
binding.btnPrevJornada.setEnabled(currentJornadaIndex > 0);
binding.btnNextJornada.setEnabled(currentJornadaIndex < matchdaysList.size() - 1);
// Visual feedback for disabled buttons
binding.btnPrevJornada.setAlpha(currentJornadaIndex > 0 ? 1.0f : 0.3f);
binding.btnNextJornada.setAlpha(currentJornadaIndex < matchdaysList.size() - 1 ? 1.0f : 0.3f);
} }
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
if (mDatabase != null && currentListener != null) {
mDatabase.removeEventListener(currentListener);
}
binding = null; binding = null;
} }
} }

View File

@@ -1,27 +1,22 @@
package com.example.vdcscore.ui.gallery; package com.example.vdcscore.ui.gallery;
import com.google.firebase.database.PropertyName;
public class Match { public class Match {
private String id; private String id;
private String homeTeam; private String homeName;
private String awayTeam; private String awayName;
private int homeScore; private Integer homeScore;
private int awayScore; private Integer awayScore;
private String date; private String date;
private String time; private String time;
private String status; // "Scheduled", "Finished", "Live" private String homeLogo;
private String awayLogo;
private String stadium;
public Match() { public Match() {
} }
public Match(String homeTeam, String awayTeam, String date, String time) {
this.homeTeam = homeTeam;
this.awayTeam = awayTeam;
this.date = date;
this.time = time;
this.status = "Scheduled";
}
// Getters and Setters
public String getId() { public String getId() {
return id; return id;
} }
@@ -30,59 +25,93 @@ public class Match {
this.id = id; this.id = id;
} }
public String getHomeTeam() { @PropertyName("home_nome")
return homeTeam; public String getHomeName() {
return homeName;
} }
public void setHomeTeam(String homeTeam) { @PropertyName("home_nome")
this.homeTeam = homeTeam; public void setHomeName(String homeName) {
this.homeName = homeName;
} }
public String getAwayTeam() { @PropertyName("away_nome")
return awayTeam; public String getAwayName() {
return awayName;
} }
public void setAwayTeam(String awayTeam) { @PropertyName("away_nome")
this.awayTeam = awayTeam; public void setAwayName(String awayName) {
this.awayName = awayName;
} }
public int getHomeScore() { @PropertyName("home_golos")
public Integer getHomeScore() {
return homeScore; return homeScore;
} }
public void setHomeScore(int homeScore) { @PropertyName("home_golos")
public void setHomeScore(Integer homeScore) {
this.homeScore = homeScore; this.homeScore = homeScore;
} }
public int getAwayScore() { @PropertyName("away_golos")
public Integer getAwayScore() {
return awayScore; return awayScore;
} }
public void setAwayScore(int awayScore) { @PropertyName("away_golos")
public void setAwayScore(Integer awayScore) {
this.awayScore = awayScore; this.awayScore = awayScore;
} }
@PropertyName("data")
public String getDate() { public String getDate() {
return date; return date;
} }
@PropertyName("data")
public void setDate(String date) { public void setDate(String date) {
this.date = date; this.date = date;
} }
@PropertyName("hora")
public String getTime() { public String getTime() {
return time; return time;
} }
@PropertyName("hora")
public void setTime(String time) { public void setTime(String time) {
this.time = time; this.time = time;
} }
public String getStatus() { @PropertyName("home_logo")
return status; public String getHomeLogo() {
return homeLogo;
} }
public void setStatus(String status) { @PropertyName("home_logo")
this.status = status; public void setHomeLogo(String homeLogo) {
this.homeLogo = homeLogo;
}
@PropertyName("away_logo")
public String getAwayLogo() {
return awayLogo;
}
@PropertyName("away_logo")
public void setAwayLogo(String awayLogo) {
this.awayLogo = awayLogo;
}
@PropertyName("campo")
public String getStadium() {
return stadium;
}
@PropertyName("campo")
public void setStadium(String stadium) {
this.stadium = stadium;
} }
} }

View File

@@ -3,21 +3,32 @@ package com.example.vdcscore.ui.gallery;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.vdcscore.R; import com.example.vdcscore.R;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHolder> { public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHolder> {
private List<Match> matches; private List<Match> matches = new ArrayList<>();
public MatchesAdapter() {
}
public MatchesAdapter(List<Match> matches) { public MatchesAdapter(List<Match> matches) {
this.matches = matches; this.matches = (matches != null) ? matches : new ArrayList<>();
}
public void setMatches(List<Match> matches) {
this.matches = (matches != null) ? matches : new ArrayList<>();
notifyDataSetChanged();
} }
@NonNull @NonNull
@@ -31,21 +42,45 @@ public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHold
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Match match = matches.get(position); Match match = matches.get(position);
holder.textHomeTeam.setText(match.getHomeTeam());
holder.textAwayTeam.setText(match.getAwayTeam());
if ("Finished".equals(match.getStatus())) { // Team Names with fallback
String homeName = match.getHomeName();
String awayName = match.getAwayName();
holder.textHomeTeam.setText(isValid(homeName) ? homeName : "Equipa A");
holder.textAwayTeam.setText(isValid(awayName) ? awayName : "Equipa B");
// Logos
Glide.with(holder.itemView.getContext())
.load(match.getHomeLogo())
.placeholder(R.drawable.ic_club_placeholder)
.into(holder.imgHomeLogo);
Glide.with(holder.itemView.getContext())
.load(match.getAwayLogo())
.placeholder(R.drawable.ic_club_placeholder)
.into(holder.imgAwayLogo);
// Scores and Time
if (match.getHomeScore() != null && match.getAwayScore() != null) {
holder.textScore.setText(match.getHomeScore() + " - " + match.getAwayScore()); holder.textScore.setText(match.getHomeScore() + " - " + match.getAwayScore());
holder.textTime.setText("Final"); holder.textTime.setText(match.getDate() != null ? match.getDate() : "Final");
} else { } else {
holder.textScore.setText("Vs"); holder.textScore.setText("Vs");
holder.textTime.setText(match.getTime()); String timeStr = "";
if (match.getDate() != null) timeStr += match.getDate();
if (match.getTime() != null) timeStr += " " + match.getTime();
holder.textTime.setText(timeStr.trim().isEmpty() ? "Agendado" : timeStr.trim());
} }
} }
private boolean isValid(String text) {
return text != null && !text.trim().isEmpty() && !text.equalsIgnoreCase("null");
}
@Override @Override
public int getItemCount() { public int getItemCount() {
return matches == null ? 0 : matches.size(); return matches.size();
} }
public static class ViewHolder extends RecyclerView.ViewHolder { public static class ViewHolder extends RecyclerView.ViewHolder {
@@ -53,6 +88,8 @@ public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHold
public final TextView textAwayTeam; public final TextView textAwayTeam;
public final TextView textScore; public final TextView textScore;
public final TextView textTime; public final TextView textTime;
public final ImageView imgHomeLogo;
public final ImageView imgAwayLogo;
public ViewHolder(View view) { public ViewHolder(View view) {
super(view); super(view);
@@ -60,6 +97,8 @@ public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHold
textAwayTeam = view.findViewById(R.id.text_away_team); textAwayTeam = view.findViewById(R.id.text_away_team);
textScore = view.findViewById(R.id.text_score); textScore = view.findViewById(R.id.text_score);
textTime = view.findViewById(R.id.text_time); textTime = view.findViewById(R.id.text_time);
imgHomeLogo = view.findViewById(R.id.img_home_logo);
imgAwayLogo = view.findViewById(R.id.img_away_logo);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -9,19 +10,91 @@
tools:context=".ui.gallery.GalleryFragment"> tools:context=".ui.gallery.GalleryFragment">
<TextView <TextView
android:id="@+id/text_title"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Jornadas" android:text="Jornadas"
android:textSize="24sp" android:textSize="28sp"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@color/primary_color" android:textColor="@color/primary_color"
android:layout_marginBottom="16dp" /> android:layout_marginBottom="16dp" />
<!-- Category Toggle -->
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_group_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:singleSelection="true"
app:selectionRequired="true"
app:checkedButton="@+id/btn_seniores">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_seniores"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Seniores"
android:textAllCaps="false" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_juniores"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Juniores"
android:textAllCaps="false" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<!-- Matchday Navigation -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:background="@drawable/bg_jornada_nav"
android:padding="8dp"
android:layout_marginBottom="16dp">
<ImageButton
android:id="@+id/btn_prev_jornada"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@android:drawable/ic_media_previous"
android:contentDescription="Anterior"
app:tint="@color/primary_color" />
<TextView
android:id="@+id/text_jornada_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Carregando..."
android:textSize="18sp"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/text_primary" />
<ImageButton
android:id="@+id/btn_next_jornada"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@android:drawable/ic_media_next"
android:contentDescription="Próxima"
app:tint="@color/primary_color" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_matchdays" android:id="@+id/recycler_matchdays"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clipToPadding="false" android:clipToPadding="false"
android:paddingBottom="16dp"/> android:paddingBottom="16dp"
tools:listitem="@layout/item_match"/>
</LinearLayout> </LinearLayout>

View File

@@ -1,30 +1,50 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:gravity="center_vertical" android:gravity="center_vertical"
android:padding="8dp"> android:padding="12dp"
android:layout_marginBottom="8dp"
android:background="@drawable/bg_match_item">
<!-- Home Team --> <!-- Home Team Section -->
<TextView <LinearLayout
android:id="@+id/text_home_team"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:gravity="end" android:gravity="center_vertical|end"
android:text="Home" android:orientation="horizontal">
android:textStyle="bold"
android:textColor="@color/text_primary" />
<!-- Score / Time --> <TextView
android:id="@+id/text_home_team"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:text="Home"
android:textStyle="bold"
android:textColor="@color/text_primary"
android:layout_marginEnd="8dp" />
<ImageView
android:id="@+id/img_home_logo"
android:layout_width="24dp"
android:layout_height="24dp"
android:padding="2dp"
android:scaleType="centerInside"
android:src="@drawable/ic_club_placeholder" />
</LinearLayout>
<!-- Score / Time Section -->
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp" android:layout_marginHorizontal="12dp"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:minWidth="60dp"> android:minWidth="70dp">
<TextView <TextView
android:id="@+id/text_score" android:id="@+id/text_score"
@@ -32,27 +52,44 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Vs" android:text="Vs"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@color/secondary_color" android:textColor="@color/primary_color"
android:textSize="16sp" /> android:textSize="18sp" />
<TextView <TextView
android:id="@+id/text_time" android:id="@+id/text_time"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="15:00" android:text="15:00"
android:textSize="12sp" android:textSize="11sp"
android:textColor="@color/text_secondary" /> android:textColor="@color/text_secondary" />
</LinearLayout> </LinearLayout>
<!-- Away Team --> <!-- Away Team Section -->
<TextView <LinearLayout
android:id="@+id/text_away_team"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:gravity="start" android:gravity="center_vertical|start"
android:text="Away" android:orientation="horizontal">
android:textStyle="bold"
android:textColor="@color/text_primary" /> <ImageView
android:id="@+id/img_away_logo"
android:layout_width="24dp"
android:layout_height="24dp"
android:padding="2dp"
android:scaleType="centerInside"
android:src="@drawable/ic_club_placeholder" />
<TextView
android:id="@+id/text_away_team"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="Away"
android:textStyle="bold"
android:textColor="@color/text_primary"
android:layout_marginStart="8dp" />
</LinearLayout>
</LinearLayout> </LinearLayout>