alteracoes

This commit is contained in:
2026-01-21 10:39:29 +00:00
parent a70395f258
commit f034364a41
4 changed files with 312 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
package com.example.vdcscore.data;
import androidx.annotation.NonNull;
import com.example.vdcscore.models.Club;
import com.example.vdcscore.models.Jornada;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class FirebaseRepository {
private final DatabaseReference mDatabase;
private static FirebaseRepository instance;
private FirebaseRepository() {
mDatabase = FirebaseDatabase.getInstance().getReference();
}
public static synchronized FirebaseRepository getInstance() {
if (instance == null) {
instance = new FirebaseRepository();
}
return instance;
}
// --- Clubs ---
public void addClub(Club club, OnCompleteListener<Void> onCompleteListener) {
String key = club.getId();
if (key == null || key.isEmpty()) {
key = mDatabase.child("clubs").push().getKey();
club.setId(key);
}
mDatabase.child("clubs").child(key).setValue(club).addOnCompleteListener(onCompleteListener);
}
public void getClubs(final DataCallback<List<Club>> callback) {
mDatabase.child("clubs").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<Club> clubs = new ArrayList<>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Club club = postSnapshot.getValue(Club.class);
if (club != null) {
club.setId(postSnapshot.getKey());
clubs.add(club);
}
}
callback.onSuccess(clubs);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onFailure(error.toException());
}
});
}
// --- Jornadas ---
public void addJornada(Jornada jornada, OnCompleteListener<Void> onCompleteListener) {
String key = jornada.getId();
if (key == null || key.isEmpty()) {
key = mDatabase.child("jornadas").push().getKey();
jornada.setId(key);
}
mDatabase.child("jornadas").child(key).setValue(jornada).addOnCompleteListener(onCompleteListener);
}
public void getJornadas(final DataCallback<List<Jornada>> callback) {
mDatabase.child("jornadas").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<Jornada> jornadas = new ArrayList<>();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Jornada jornada = postSnapshot.getValue(Jornada.class);
if (jornada != null) {
jornada.setId(postSnapshot.getKey());
jornadas.add(jornada);
}
}
callback.onSuccess(jornadas);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onFailure(error.toException());
}
});
}
public interface DataCallback<T> {
void onSuccess(T data);
void onFailure(Exception e);
}
}

View File

@@ -0,0 +1,51 @@
package com.example.vdcscore.models;
public class Club {
private String id;
private String name;
private String logoUrl;
private String stadium;
// Stats could be here or in a separate object, keeping basic info here
public Club() {
}
public Club(String id, String name, String logoUrl, String stadium) {
this.id = id;
this.name = name;
this.logoUrl = logoUrl;
this.stadium = stadium;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
public String getStadium() {
return stadium;
}
public void setStadium(String stadium) {
this.stadium = stadium;
}
}

View File

@@ -0,0 +1,102 @@
package com.example.vdcscore.models;
public class Game {
private String id;
private String homeTeamId;
private String awayTeamId;
private String homeTeamName;
private String awayTeamName;
private int homeScore;
private int awayScore;
private boolean isFinished;
private long timestamp;
public Game() {
// Required for Firebase
}
public Game(String id, String homeTeamId, String awayTeamId, String homeTeamName, String awayTeamName,
long timestamp) {
this.id = id;
this.homeTeamId = homeTeamId;
this.awayTeamId = awayTeamId;
this.homeTeamName = homeTeamName;
this.awayTeamName = awayTeamName;
this.timestamp = timestamp;
this.isFinished = false;
this.homeScore = 0;
this.awayScore = 0;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getHomeTeamId() {
return homeTeamId;
}
public void setHomeTeamId(String homeTeamId) {
this.homeTeamId = homeTeamId;
}
public String getAwayTeamId() {
return awayTeamId;
}
public void setAwayTeamId(String awayTeamId) {
this.awayTeamId = awayTeamId;
}
public String getHomeTeamName() {
return homeTeamName;
}
public void setHomeTeamName(String homeTeamName) {
this.homeTeamName = homeTeamName;
}
public String getAwayTeamName() {
return awayTeamName;
}
public void setAwayTeamName(String awayTeamName) {
this.awayTeamName = awayTeamName;
}
public int getHomeScore() {
return homeScore;
}
public void setHomeScore(int homeScore) {
this.homeScore = homeScore;
}
public int getAwayScore() {
return awayScore;
}
public void setAwayScore(int awayScore) {
this.awayScore = awayScore;
}
public boolean isFinished() {
return isFinished;
}
public void setFinished(boolean finished) {
isFinished = finished;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}

View File

@@ -0,0 +1,55 @@
package com.example.vdcscore.models;
import java.util.ArrayList;
import java.util.List;
public class Jornada {
private String id;
private int number;
private List<Game> games;
private boolean isCurrent;
public Jornada() {
// Required for Firebase
this.games = new ArrayList<>();
}
public Jornada(String id, int number) {
this.id = id;
this.number = number;
this.games = new ArrayList<>();
this.isCurrent = false;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public List<Game> getGames() {
return games;
}
public void setGames(List<Game> games) {
this.games = games;
}
public boolean isCurrent() {
return isCurrent;
}
public void setCurrent(boolean current) {
isCurrent = current;
}
}