Skip to content

Commit

Permalink
Se muestran los puntos que tiene el usuario en la pantalla de quiniel…
Browse files Browse the repository at this point in the history
…a (temporal)
  • Loading branch information
DavidRamosArchilla committed Apr 8, 2023
1 parent 7c7dd3b commit 52acaca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
10 changes: 7 additions & 3 deletions app/src/main/java/com/example/f1/IF1ApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public interface IF1ApiService {

@GET("/api/f1/current/last/results.json")
Call<JsonObject> getLastRace(); //(@Path("id") long id)
@GET("api/f1/current/drivers.json")
Call<JsonObject> getDrivers();

@GET("/api/f1/{year}.json")
Call<JsonObject> getRacesOfYear(@Path("year") String year);

@GET("/api/f1/{year}/{round}/results.json")
Call<JsonObject> getResultOfRace(@Path("year") String year, @Path("round") String round);
@GET("/api/f1/{year}/{round}/qualifying.json")
Call<JsonObject> getQualiOfRace(@Path("year") String year, @Path("round") String round);
@GET("/api/f1/{year}/driverStrandings.json")
Call<JsonObject> getStanding(@Path("year") String year);

@GET("/api/f1/current/next/races.json")
Call<JsonObject> getNextRace();

@GET("api/f1/current/drivers.json")
Call<JsonObject> getDrivers();
}
49 changes: 38 additions & 11 deletions app/src/main/java/com/example/f1/QuinielaFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand All @@ -16,11 +16,11 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
Expand All @@ -33,9 +33,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import retrofit2.Call;
import retrofit2.Callback;
Expand Down Expand Up @@ -75,15 +75,42 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mostrarPuntos(view);
btn.setOnClickListener(v -> {
crearDialog((dialog, which) -> {
String fechaCarrera = "2023-04-02"; // TODO: hay que obtener la fecha, esto es para el ejemplo
List<String> quiniela = obtenerQuiniela();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/quinielas");
mDatabase.child(idUsuario).child(fechaCarrera).setValue(quiniela);

new Thread(() -> {
CompletableFuture<String> future = obtenerFechaSiguienteCarrera();
String fechaCarrera;
try {
// de esta forma se bloquea el thread hasta que la llamada a la api acaba
fechaCarrera = future.get();
} catch (ExecutionException | InterruptedException e) {
Snackbar.make(view, "No se ha podido obtener datos de la siguiente carrera", Snackbar.LENGTH_LONG).show();
throw new RuntimeException(e);
}
List<String> quiniela = obtenerQuiniela();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/quinielas");
mDatabase.child(idUsuario).child(fechaCarrera).setValue(quiniela);
Snackbar.make(view, "Se ha enviado correctamente para el: " + fechaCarrera, Snackbar.LENGTH_LONG).show();
}).start();
});
});
}

private CompletableFuture<String> obtenerFechaSiguienteCarrera() {
CompletableFuture<String> future = new CompletableFuture<>();
((PantallaInicioActivity)getActivity()).getService().getNextRace().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
String fecha = getFechaCarrera(response.body());
future.complete(fecha);
}

@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}

private void mostrarPuntos(View view) {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("puntos/" + idUsuario);
mDatabase.addValueEventListener(new ValueEventListener() {
Expand All @@ -107,7 +134,7 @@ private void comprobarUltimaCarrera() {
((PantallaInicioActivity)getActivity()).getService().getLastRace().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
String fechaUltimaCarrera = getFechaUltimaCarrera(response.body());
String fechaUltimaCarrera = getFechaCarrera(response.body());
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/quinielas");
mDatabase.child(idUsuario).child(fechaUltimaCarrera)
.addListenerForSingleValueEvent(new ComprobarQuinielaListener(getContext(), response.body(), fechaUltimaCarrera));
Expand All @@ -118,7 +145,7 @@ public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
private String getFechaUltimaCarrera(JsonObject response) {
private String getFechaCarrera(JsonObject response) {
return response.getAsJsonObject("MRData")
.getAsJsonObject("RaceTable")
.getAsJsonArray("Races")
Expand Down

0 comments on commit 52acaca

Please sign in to comment.