From 52acaca2a087c185dfd2d9771d565b5656002056 Mon Sep 17 00:00:00 2001 From: Davidram1802 <56789396+Davidram1802@users.noreply.github.com> Date: Sat, 8 Apr 2023 14:04:39 +0200 Subject: [PATCH] Se muestran los puntos que tiene el usuario en la pantalla de quiniela (temporal) --- .../java/com/example/f1/IF1ApiService.java | 10 ++-- .../java/com/example/f1/QuinielaFragment.java | 49 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/example/f1/IF1ApiService.java b/app/src/main/java/com/example/f1/IF1ApiService.java index aedac9d..e8e824b 100644 --- a/app/src/main/java/com/example/f1/IF1ApiService.java +++ b/app/src/main/java/com/example/f1/IF1ApiService.java @@ -11,15 +11,19 @@ public interface IF1ApiService { @GET("/api/f1/current/last/results.json") Call getLastRace(); //(@Path("id") long id) - @GET("api/f1/current/drivers.json") - Call getDrivers(); + @GET("/api/f1/{year}.json") Call getRacesOfYear(@Path("year") String year); - @GET("/api/f1/{year}/{round}/results.json") Call getResultOfRace(@Path("year") String year, @Path("round") String round); @GET("/api/f1/{year}/{round}/qualifying.json") Call getQualiOfRace(@Path("year") String year, @Path("round") String round); @GET("/api/f1/{year}/driverStrandings.json") Call getStanding(@Path("year") String year); + + @GET("/api/f1/current/next/races.json") + Call getNextRace(); + + @GET("api/f1/current/drivers.json") + Call getDrivers(); } diff --git a/app/src/main/java/com/example/f1/QuinielaFragment.java b/app/src/main/java/com/example/f1/QuinielaFragment.java index 62cb0d3..4bce465 100644 --- a/app/src/main/java/com/example/f1/QuinielaFragment.java +++ b/app/src/main/java/com/example/f1/QuinielaFragment.java @@ -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; @@ -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; @@ -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; @@ -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 quiniela = obtenerQuiniela(); - DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/quinielas"); - mDatabase.child(idUsuario).child(fechaCarrera).setValue(quiniela); - + new Thread(() -> { + CompletableFuture 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 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 obtenerFechaSiguienteCarrera() { + CompletableFuture future = new CompletableFuture<>(); + ((PantallaInicioActivity)getActivity()).getService().getNextRace().enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + String fecha = getFechaCarrera(response.body()); + future.complete(fecha); + } + + @Override + public void onFailure(Call call, Throwable t) { + future.completeExceptionally(t); + } + }); + return future; + } + private void mostrarPuntos(View view) { DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("puntos/" + idUsuario); mDatabase.addValueEventListener(new ValueEventListener() { @@ -107,7 +134,7 @@ private void comprobarUltimaCarrera() { ((PantallaInicioActivity)getActivity()).getService().getLastRace().enqueue(new Callback() { @Override public void onResponse(Call call, Response 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)); @@ -118,7 +145,7 @@ public void onFailure(Call call, Throwable t) { } }); } - private String getFechaUltimaCarrera(JsonObject response) { + private String getFechaCarrera(JsonObject response) { return response.getAsJsonObject("MRData") .getAsJsonObject("RaceTable") .getAsJsonArray("Races")