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.recyclerview.widget.LinearLayoutManager;
import com.example.vdcscore.R;
import com.example.vdcscore.databinding.FragmentGalleryBinding;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
@@ -23,8 +24,12 @@ import java.util.List;
public class GalleryFragment extends Fragment {
private FragmentGalleryBinding binding;
private MatchdaysAdapter adapter;
private MatchesAdapter adapter;
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,
ViewGroup container, Bundle savedInstanceState) {
@@ -33,57 +38,119 @@ public class GalleryFragment extends Fragment {
View root = binding.getRoot();
// Initialize RecyclerView
adapter = new MatchdaysAdapter();
adapter = new MatchesAdapter();
binding.recyclerMatchdays.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerMatchdays.setAdapter(adapter);
// Initialize Firebase
mDatabase = FirebaseDatabase.getInstance().getReference("matchdays");
// Setup Toggle Group
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();
return root;
}
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
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (binding == null) return;
List<Matchday> matchdays = new ArrayList<>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Matchday matchday = new Matchday();
matchday.setId(postSnapshot.getKey());
matchday.setName(postSnapshot.child("name").getValue(String.class));
matchdaysList.clear();
for (DataSnapshot jornadaSnap : snapshot.getChildren()) {
String name = jornadaSnap.getKey();
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) {
match.setId(matchSnapshot.getKey());
match.setId(matchSnap.getKey());
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
public void onCancelled(@NonNull DatabaseError error) {
if (getContext() != null) {
Toast.makeText(getContext(), "Erro ao carregar jornadas: " + error.getMessage(), Toast.LENGTH_SHORT)
.show();
Toast.makeText(getContext(), "Erro ao carregar: " + error.getMessage(), Toast.LENGTH_SHORT).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
public void onDestroyView() {
super.onDestroyView();
if (mDatabase != null && currentListener != null) {
mDatabase.removeEventListener(currentListener);
}
binding = null;
}
}

View File

@@ -1,27 +1,22 @@
package com.example.vdcscore.ui.gallery;
import com.google.firebase.database.PropertyName;
public class Match {
private String id;
private String homeTeam;
private String awayTeam;
private int homeScore;
private int awayScore;
private String homeName;
private String awayName;
private Integer homeScore;
private Integer awayScore;
private String date;
private String time;
private String status; // "Scheduled", "Finished", "Live"
private String homeLogo;
private String awayLogo;
private String stadium;
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() {
return id;
}
@@ -30,59 +25,93 @@ public class Match {
this.id = id;
}
public String getHomeTeam() {
return homeTeam;
@PropertyName("home_nome")
public String getHomeName() {
return homeName;
}
public void setHomeTeam(String homeTeam) {
this.homeTeam = homeTeam;
@PropertyName("home_nome")
public void setHomeName(String homeName) {
this.homeName = homeName;
}
public String getAwayTeam() {
return awayTeam;
@PropertyName("away_nome")
public String getAwayName() {
return awayName;
}
public void setAwayTeam(String awayTeam) {
this.awayTeam = awayTeam;
@PropertyName("away_nome")
public void setAwayName(String awayName) {
this.awayName = awayName;
}
public int getHomeScore() {
@PropertyName("home_golos")
public Integer getHomeScore() {
return homeScore;
}
public void setHomeScore(int homeScore) {
@PropertyName("home_golos")
public void setHomeScore(Integer homeScore) {
this.homeScore = homeScore;
}
public int getAwayScore() {
@PropertyName("away_golos")
public Integer getAwayScore() {
return awayScore;
}
public void setAwayScore(int awayScore) {
@PropertyName("away_golos")
public void setAwayScore(Integer awayScore) {
this.awayScore = awayScore;
}
@PropertyName("data")
public String getDate() {
return date;
}
@PropertyName("data")
public void setDate(String date) {
this.date = date;
}
@PropertyName("hora")
public String getTime() {
return time;
}
@PropertyName("hora")
public void setTime(String time) {
this.time = time;
}
public String getStatus() {
return status;
@PropertyName("home_logo")
public String getHomeLogo() {
return homeLogo;
}
public void setStatus(String status) {
this.status = status;
@PropertyName("home_logo")
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.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.vdcscore.R;
import java.util.ArrayList;
import java.util.List;
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) {
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
@@ -31,21 +42,45 @@ public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHold
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int 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.textTime.setText("Final");
holder.textTime.setText(match.getDate() != null ? match.getDate() : "Final");
} else {
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
public int getItemCount() {
return matches == null ? 0 : matches.size();
return matches.size();
}
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 textScore;
public final TextView textTime;
public final ImageView imgHomeLogo;
public final ImageView imgAwayLogo;
public ViewHolder(View view) {
super(view);
@@ -60,6 +97,8 @@ public class MatchesAdapter extends RecyclerView.Adapter<MatchesAdapter.ViewHold
textAwayTeam = view.findViewById(R.id.text_away_team);
textScore = view.findViewById(R.id.text_score);
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"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -9,19 +10,91 @@
tools:context=".ui.gallery.GalleryFragment">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jornadas"
android:textSize="24sp"
android:textSize="28sp"
android:textStyle="bold"
android:textColor="@color/primary_color"
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
android:id="@+id/recycler_matchdays"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"/>
android:paddingBottom="16dp"
tools:listitem="@layout/item_match"/>
</LinearLayout>

View File

@@ -1,30 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<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_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp">
android:padding="12dp"
android:layout_marginBottom="8dp"
android:background="@drawable/bg_match_item">
<!-- Home Team -->
<TextView
android:id="@+id/text_home_team"
<!-- Home Team Section -->
<LinearLayout
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:gravity="center_vertical|end"
android:orientation="horizontal">
<!-- 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
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:gravity="center"
android:orientation="vertical"
android:minWidth="60dp">
android:minWidth="70dp">
<TextView
android:id="@+id/text_score"
@@ -32,27 +52,44 @@
android:layout_height="wrap_content"
android:text="Vs"
android:textStyle="bold"
android:textColor="@color/secondary_color"
android:textSize="16sp" />
android:textColor="@color/primary_color"
android:textSize="18sp" />
<TextView
android:id="@+id/text_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15:00"
android:textSize="12sp"
android:textSize="11sp"
android:textColor="@color/text_secondary" />
</LinearLayout>
<!-- Away Team -->
<TextView
android:id="@+id/text_away_team"
<!-- Away Team Section -->
<LinearLayout
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:gravity="center_vertical|start"
android:orientation="horizontal">
<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>