diff --git a/app/src/main/java/com/example/vdcscore/ui/gallery/GalleryFragment.java b/app/src/main/java/com/example/vdcscore/ui/gallery/GalleryFragment.java index bc3e02d..51ca390 100644 --- a/app/src/main/java/com/example/vdcscore/ui/gallery/GalleryFragment.java +++ b/app/src/main/java/com/example/vdcscore/ui/gallery/GalleryFragment.java @@ -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 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 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 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; } } \ No newline at end of file diff --git a/app/src/main/java/com/example/vdcscore/ui/gallery/Match.java b/app/src/main/java/com/example/vdcscore/ui/gallery/Match.java index 97c8460..bbceea4 100644 --- a/app/src/main/java/com/example/vdcscore/ui/gallery/Match.java +++ b/app/src/main/java/com/example/vdcscore/ui/gallery/Match.java @@ -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; } } diff --git a/app/src/main/java/com/example/vdcscore/ui/gallery/MatchesAdapter.java b/app/src/main/java/com/example/vdcscore/ui/gallery/MatchesAdapter.java index 283e2d6..02b187d 100644 --- a/app/src/main/java/com/example/vdcscore/ui/gallery/MatchesAdapter.java +++ b/app/src/main/java/com/example/vdcscore/ui/gallery/MatchesAdapter.java @@ -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 { - private List matches; + private List matches = new ArrayList<>(); + + public MatchesAdapter() { + } public MatchesAdapter(List matches) { - this.matches = matches; + this.matches = (matches != null) ? matches : new ArrayList<>(); + } + + public void setMatches(List matches) { + this.matches = (matches != null) ? matches : new ArrayList<>(); + notifyDataSetChanged(); } @NonNull @@ -31,21 +42,45 @@ public class MatchesAdapter extends RecyclerView.Adapter + + + + + + + + + + + + + + + + + + + + android:paddingBottom="16dp" + tools:listitem="@layout/item_match"/> \ No newline at end of file diff --git a/app/src/main/res/layout/item_match.xml b/app/src/main/res/layout/item_match.xml index c67e7a3..ffe026a 100644 --- a/app/src/main/res/layout/item_match.xml +++ b/app/src/main/res/layout/item_match.xml @@ -1,30 +1,50 @@ + android:padding="12dp" + android:layout_marginBottom="8dp" + android:background="@drawable/bg_match_item"> - - + + android:gravity="center_vertical|end" + android:orientation="horizontal"> - + + + + + + + android:minWidth="70dp"> + android:textColor="@color/primary_color" + android:textSize="18sp" /> - - + + android:gravity="center_vertical|start" + android:orientation="horizontal"> + + + + +