ticket fomattage

This commit is contained in:
OnlyPapy98
2026-04-30 13:08:05 +02:00
parent c0e5072523
commit 4ef19bd4e8
45 changed files with 2492 additions and 449 deletions

View File

@@ -1,9 +1,16 @@
package com.example.quiz.data.adapter;
import static java.security.AccessController.getContext;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
@@ -17,21 +24,23 @@ import java.util.List;
public class LastBetsAdapter extends RecyclerView.Adapter<LastBetsAdapter.LastBetsViewHolder> {
private List<ParisResponse> listeParis = new ArrayList<>(); // évite les NPE
private OnBetClick onBetClick; // peut être null si tu veux
public interface OnBetClick {
private onPariClickListener listener;
public interface onPariClickListener {
void onItemClick(ParisResponse pari);
void onItemCancel(ParisResponse pari);
}
public LastBetsAdapter(List<ParisResponse> listeParis, OnBetClick onBetClick) {
public void setPariClickListener(onPariClickListener listener) {
this.listener = listener;
}
public LastBetsAdapter(List<ParisResponse> listeParis) {
if (listeParis != null) this.listeParis = listeParis;
this.onBetClick = onBetClick;
}
// Constructeur vide utile si tu veux créer puis setData ensuite
public LastBetsAdapter(OnBetClick onBetClick) {
this.onBetClick = onBetClick;
}
public void setData(List<ParisResponse> newList) {
if (newList == null) {
@@ -77,10 +86,44 @@ public class LastBetsAdapter extends RecyclerView.Adapter<LastBetsAdapter.LastBe
// Mise (défensif)
holder.mise.setText("Mise: " + pari.getMiseTotale() + " CFA");
holder.tvStatut.setText(pari.getStatutPari() != null ? String.valueOf(pari.getStatutPari()) : "Pas de statut");
switch (pari.getStatutPari()){
case ANNULE:{
holder.tvStatut.setTextColor(Color.RED);
holder.btnAnnuler.setVisibility(View.GONE);
holder.btnImprimer.setVisibility(View.GONE);
break;
}
case PAYE:{
holder.tvStatut.setTextColor(Color.GREEN);
holder.btnAnnuler.setVisibility(View.GONE);
holder.btnImprimer.setVisibility(View.GONE);
break;
}
case ENREGISTRE:{
holder.tvStatut.setTextColor(Color.YELLOW);
holder.btnAnnuler.setVisibility(View.VISIBLE);
holder.btnImprimer.setVisibility(View.VISIBLE);
break;
}
default:{
holder.tvStatut.setTextColor(Color.BLACK);
holder.btnAnnuler.setVisibility(View.GONE);
holder.btnImprimer.setVisibility(View.GONE);
break;
}
}
// click listener défensif
holder.itemView.setOnClickListener(v -> {
if (onBetClick != null) onBetClick.onItemClick(pari);
holder.btnAnnuler.setOnClickListener(v -> {
if (listener != null) {
listener.onItemCancel(pari);
}
});
holder.btnImprimer.setOnClickListener(v -> {
if (listener != null) {
listener.onItemClick(pari);
}
});
}
@@ -90,7 +133,8 @@ public class LastBetsAdapter extends RecyclerView.Adapter<LastBetsAdapter.LastBe
}
public static class LastBetsViewHolder extends RecyclerView.ViewHolder {
TextView betType, horses, course, mise, refenceTicket, typeOfCourse;
TextView betType, horses, course, mise, refenceTicket, typeOfCourse, tvStatut;
Button btnAnnuler, btnImprimer;;
public LastBetsViewHolder(@NonNull View itemView) {
super(itemView);
@@ -100,6 +144,9 @@ public class LastBetsAdapter extends RecyclerView.Adapter<LastBetsAdapter.LastBe
mise = itemView.findViewById(R.id.last_bet_mise);
refenceTicket = itemView.findViewById(R.id.reference_ticket);
typeOfCourse = itemView.findViewById(R.id.type_of_course);
btnAnnuler = itemView.findViewById(R.id.btn_annuler);
btnImprimer = itemView.findViewById(R.id.btn_imprimer);
tvStatut = itemView.findViewById(R.id.tv_statut);
}
}
}

View File

@@ -1,6 +1,7 @@
package com.example.quiz.data.model;
import java.io.Serializable;
import java.time.temporal.TemporalAccessor;
import java.util.List;
public class ParisResponse implements Serializable {
@@ -58,7 +59,10 @@ public class ParisResponse implements Serializable {
PAYE,
ANNULE,
PERDANT,
GAGNANT
GAGNANT,
A_REMBOURSER,
REMBOURSE,
ECHEC_CACUL
}
// ================= INNER CLASS =================
@@ -67,7 +71,7 @@ public class ParisResponse implements Serializable {
public long getId() { return id; }
public String getNumeroTicket() { return numeroTicket; }
public String getDateHeurePrise() { return dateHeurePrise; }
public String getDateHeurePrise() { return dateHeurePrise; }
public String getQrPayload() { return qrPayload; }
public long getCourseId() { return courseId; }
public String getCourseNom() { return courseNom; }

View File

@@ -0,0 +1,36 @@
package com.example.quiz.data.model;
public class ResponseError {
private String message;
private int status;
private String timestamp;
public ResponseError(){
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}

View File

@@ -20,7 +20,7 @@ import retrofit2.converter.gson.GsonConverterFactory;
@Module
@InstallIn(SingletonComponent.class)
public class ApiClient {
private static final String BASE_URL = "https://boxer-adapting-bluegill.ngrok-free.app/api/";
private static final String BASE_URL = "https://alr.pmu.ml/api/";
@Provides

View File

@@ -45,9 +45,17 @@ public interface ApiService {
@GET("paris/agent/{agentId}/today")
Call<List<ParisResponse>> derniersParis(@Path("agentId") String agentId);
@PATCH("paris/numero/{numeroTicket}/statut")
Call<ParisResponse> annulerPari(@Path("numeroTicket") String numeroTicket,
@Body() CancelParisPaylaod cancelParisPaylaod);
@PATCH("paris/numero/{numeroTicket}/cancel")
Call<ParisResponse> annulerPari(@Path("numeroTicket") String numeroTicket);
@PATCH("paris/numero/{numeroTicket}/rembourser")
Call<ParisResponse> rembourserPari(@Path("numeroTicket") String numeroTicket);
@PATCH("paris/numero/{numeroTicket}/pay")
Call<ParisResponse> payerPari(@Path("numeroTicket") String numeroTicket);
@GET("paris/numero/{numeroTicket}")
Call<ParisResponse> getPariByNumero(@Path("numeroTicket") String numeroTicket);
@GET("paris/agent/{agentId}/solde/course/{courseId}")
Call<SoldeResponse> getSoldeByCourse(@Path("agentId") String createdBy, @Path("courseId") String courseId);
@@ -58,9 +66,15 @@ public interface ApiService {
@POST("terminaux")
Call<TpeResponse> createTpe(@Body Tpe tpe);
@GET("terminaux/{id}")
Call<TpeResponse> getTpeById(@Path("id") String id);
@GET("points-de-vente")
Call<PagedModel<PointDeVente>> getPointsDeVente(@Query("nom") String nom);
@GET("points-de-vente/{id}")
Call<PointDeVente> getPointDeVenteById(@Path("id") String id);
@PATCH("agents/me/pin")
Call<User> changePin(@Body ChangePin pin);

View File

@@ -61,7 +61,7 @@ public class StompManager {
}
// URL de connexion WebSocket
String url = "wss://boxer-adapting-bluegill.ngrok-free.app/ws";
String url = "wss://alr.pmu.ml/ws";
try {
// Créer le client STOMP

View File

@@ -5,11 +5,14 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.Course;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.model.Restriction;
import com.example.quiz.data.model.dtos.auth.User;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
@@ -34,7 +37,14 @@ public class AgentRepository {
if (response.isSuccessful()) {
liveAgents.postValue(Result.success(response.body()));
} else {
liveAgents.postValue(Result.error(response.message()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveAgents.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
@@ -54,7 +64,14 @@ public class AgentRepository {
if (response.isSuccessful()) {
liveAgent.postValue(Result.success(response.body()));
} else {
liveAgent.postValue(Result.error(response.message()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveAgent.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
@@ -74,7 +91,14 @@ public class AgentRepository {
if (response.isSuccessful()) {
liveAvailableBets.postValue(Result.success(response.body()));
} else {
liveAvailableBets.postValue(Result.error(response.message()));
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveAvailableBets.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
liveAvailableBets.postValue(Result.error(e.getMessage()));
}
}
}
@Override
@@ -94,7 +118,14 @@ public class AgentRepository {
if (response.isSuccessful()) {
liveSetAccess.postValue(Result.success(null));
} else {
liveSetAccess.postValue(Result.error(response.message()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveSetAccess.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
@@ -114,7 +145,14 @@ public class AgentRepository {
if (response.isSuccessful()) {
liveSetRestrictions.postValue(Result.success(null));
} else {
liveSetRestrictions.postValue(Result.error(response.message()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveSetRestrictions.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override

View File

@@ -7,8 +7,10 @@ import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.Course;
import com.example.quiz.data.model.PagedModel;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import javax.inject.Inject;
@@ -33,22 +35,15 @@ public class CourseRepository {
public void onResponse(Call<PagedModel<Course>> call, Response<PagedModel<Course>> response) {
if(response.isSuccessful()){
liveCourses.postValue(Result.success(response.body()));
// PagedModel<Course> openedPagesCourses = new PagedModel<>();
// List<Course> listOfCourses = new ArrayList<>();
// for(Course course: response.body().getContent()){
// if(course.getStatut().equals(OPENED_STATUT)){
// listOfCourses.add(course);
// }
// }
//// openedPagesCourses.setTotalPages(response.body().getTotalPages());
//// openedPagesCourses.setTotalElements(response.body().getTotalElements());
//// openedPagesCourses.setPageable(response.body().getPageable());
//// openedPagesCourses.setNumberOfElements(response.body().getNumberOfElements());
//// openedPagesCourses.setSize(response.body().getSize());
//// openedPagesCourses.setContent(listOfCourses);
// liveCourses.postValue(Result.success(response.body()));
}else{
liveCourses.postValue(Result.error(response.message()));
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveCourses.postValue(Result.error(errorResponse.getMessage()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -5,6 +5,7 @@ import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.model.dtos.auth.ChangePin;
import com.example.quiz.data.model.dtos.auth.LoginPayload;
import com.example.quiz.data.model.dtos.auth.LoginResponse;
@@ -12,6 +13,9 @@ import com.example.quiz.data.model.dtos.auth.User;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.data.remote.TokenManager;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import java.io.IOException;
import javax.inject.Inject;
@@ -37,10 +41,16 @@ public class LoginRepository {
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if(response.isSuccessful()){
liveLogin.postValue(Result.success(response.body()));
Log.d("TOKEN", response.body().getToken());
tokenManager.saveToken(response.body().getToken());
}else{
liveLogin.postValue(Result.error(response.toString()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveLogin.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@@ -64,7 +74,14 @@ public class LoginRepository {
if(response.isSuccessful()) {
liveUser.postValue(Result.success(response.body()));
}else {
liveUser.postValue(Result.error(response.message()));
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
liveUser.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override

View File

@@ -1,13 +1,17 @@
package com.example.quiz.data.repository;
import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.MiseInitiale;
import com.example.quiz.data.model.PariMise;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.data.remote.TokenManager;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import java.util.List;
@@ -34,7 +38,14 @@ public class PariMiseRepository {
if (response.isSuccessful()) {
livePariMise.postValue(Result.success(response.body()));
}else{
livePariMise.postValue(Result.error(response.message()));
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
livePariMise.postValue(Result.error(errorResponse.getMessage()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Override

View File

@@ -8,10 +8,12 @@ import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.Pari;
import com.example.quiz.data.model.ParisResponse;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.model.dtos.paris.CancelParisPaylaod;
import com.example.quiz.data.model.dtos.paris.SoldeResponse;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import java.util.Collections;
import java.util.List;
@@ -44,13 +46,10 @@ public class PariRepository {
} else {
// On récupère l'erreur exacte envoyée par le backend
try {
Map<String, String> errorBody = Collections.emptyMap();
if(response.errorBody() != null){
errorBody = (Map<String, String>) response.errorBody();
}
Log.d("PariRepository", response.errorBody().toString());
pariResponse.postValue(Result.error(errorBody.get("message")));
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
pariResponse.postValue(Result.error(errorResponse.getMessage()));
} catch (Exception e) {
pariResponse.postValue(Result.error("Erreur serveur"));
}
@@ -76,10 +75,10 @@ public class PariRepository {
derniersParis.postValue(Result.success(response.body()));
}else{
try{
String errorBody = response.errorBody() != null ?
response.errorBody().string() : "Erreur inconnue";
derniersParis.postValue(Result.error(errorBody));
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
derniersParis.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
derniersParis.postValue(Result.error(e.getMessage()));
}
@@ -95,21 +94,74 @@ public class PariRepository {
return derniersParis;
}
public LiveData<Result<ParisResponse>> annulerPari(String numeroTicket){
public LiveData<Result<ParisResponse>> payTicket(String numeroTicket){
MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
pariResponse.setValue(Result.loading());
CancelParisPaylaod cancelParisPaylaod = new CancelParisPaylaod("ANNULE");
apiService.annulerPari(numeroTicket, cancelParisPaylaod).enqueue(new Callback<ParisResponse>(){
apiService.payerPari(numeroTicket).enqueue(new Callback<ParisResponse>(){
@Override
public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
if(response.isSuccessful()){
pariResponse.postValue(Result.success(response.body()));
}else{
try{
String errorBody = response.errorBody() != null ?
response.errorBody().string() : "Erreur inconnue";
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
pariResponse.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
pariResponse.postValue(Result.error(e.getMessage()));
}
}
};
@Override
public void onFailure(Call<ParisResponse> call, Throwable throwable) {
pariResponse.postValue(Result.error(throwable.getMessage()));
}
});
return pariResponse;
}
pariResponse.postValue(Result.error(errorBody));
public LiveData<Result<ParisResponse>> rembourserTicket(String numeroTicket){
MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
pariResponse.setValue(Result.loading());
apiService.rembourserPari(numeroTicket).enqueue(new Callback<ParisResponse>(){
@Override
public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
if(response.isSuccessful()){
pariResponse.postValue(Result.success(response.body()));
}else{
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
pariResponse.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
pariResponse.postValue(Result.error(e.getMessage()));
}
}
}
@Override
public void onFailure(Call<ParisResponse> call, Throwable throwable) {
pariResponse.postValue(Result.error(throwable.getMessage()));
}
});
return pariResponse;
}
public LiveData<Result<ParisResponse>> annulerPari(String numeroTicket){
MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
pariResponse.setValue(Result.loading());
apiService.annulerPari(numeroTicket).enqueue(new Callback<ParisResponse>(){
@Override
public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
if(response.isSuccessful()){
pariResponse.postValue(Result.success(response.body()));
}else{
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
pariResponse.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
pariResponse.postValue(Result.error(e.getMessage()));
}
@@ -134,7 +186,10 @@ public class PariRepository {
solde.postValue(Result.success(response.body()));
}else{
try{
solde.postValue(Result.error(response.errorBody().string()));
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
solde.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
solde.postValue(Result.error(e.getMessage()));
}
@@ -159,7 +214,10 @@ public class PariRepository {
solde.postValue(Result.success(response.body()));
}else{
try {
solde.postValue(Result.error(response.errorBody().string()));
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
solde.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
solde.postValue(Result.error(e.getMessage()));
}
@@ -174,4 +232,32 @@ public class PariRepository {
return solde;
}
public LiveData<Result<ParisResponse>> getPariByNumero(String numeroTicket) {
MutableLiveData<Result<ParisResponse>> pari = new MutableLiveData<>();
pari.setValue(Result.loading());
apiService.getPariByNumero(numeroTicket).enqueue(new Callback<ParisResponse>() {
@Override
public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
if (response.isSuccessful()) {
pari.postValue(Result.success(response.body()));
}else{
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
pari.postValue(Result.error(errorResponse.getMessage()));
}catch (Exception e){
pari.postValue(Result.error(e.getMessage()));
}
}
}
@Override
public void onFailure(Call<ParisResponse> call, Throwable throwable) {
pari.postValue(Result.error(throwable.getMessage()));
}
});
return pari;
}
}

View File

@@ -5,10 +5,14 @@ import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.PagedModel;
import com.example.quiz.data.model.PointDeVente;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import java.io.IOException;
import javax.inject.Inject;
import retrofit2.*;
@@ -36,18 +40,14 @@ public class PointDeVenteRepository {
if (response.isSuccessful() && response.body() != null) {
result.postValue(Result.success(response.body()));
} else {
String errorMessage = "Erreur inconnue";
try {
if (response.errorBody() != null) {
errorMessage = response.errorBody().string();
}
} catch (Exception e) {
errorMessage = e.getMessage();
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
result.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
result.postValue(Result.error(errorMessage));
}
}
@@ -62,4 +62,33 @@ public class PointDeVenteRepository {
return result;
}
public LiveData<Result<PointDeVente>> getPointDeVenteById(String id) {
MutableLiveData<Result<PointDeVente>> result = new MutableLiveData<>();
result.setValue(Result.loading());
apiService.getPointDeVenteById(id).enqueue(new Callback<PointDeVente>() {
@Override
public void onResponse(Call<PointDeVente> call, Response<PointDeVente> response) {
if(response.isSuccessful()){
result.postValue(Result.success(response.body()));
}else{
try {
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
result.postValue(Result.error(errorResponse.getMessage()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void onFailure(Call<PointDeVente> call, Throwable throwable) {
result.postValue(Result.error(throwable.getMessage()));
}
});
return result;
}
}

View File

@@ -3,10 +3,12 @@ package com.example.quiz.data.repository;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.model.Tpe;
import com.example.quiz.data.model.TpeResponse;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;
import javax.inject.Inject;
@@ -33,7 +35,43 @@ public class TpeRepository {
if (response.isSuccessful()) {
tpeLiveData.postValue(Result.success(response.body()));
} else {
tpeLiveData.postValue(Result.error(response.message()));
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
tpeLiveData.postValue(Result.error(errorResponse.getMessage()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Override
public void onFailure(Call<TpeResponse> call, Throwable throwable) {
tpeLiveData.postValue(Result.error(throwable.getMessage()));
}
});
return tpeLiveData;
}
public LiveData<Result<TpeResponse>> getTpeById(String id) {
MutableLiveData<Result<TpeResponse>> tpeLiveData = new MutableLiveData<>();
tpeLiveData.setValue(Result.loading());
apiService.getTpeById(id).enqueue(new Callback<TpeResponse>() {
@Override
public void onResponse(Call<TpeResponse> call, Response<TpeResponse> response) {
if (response.isSuccessful()) {
tpeLiveData.postValue(Result.success(response.body()));
} else {
try{
String error = response.errorBody().string();
Gson gson = new Gson();
ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
tpeLiveData.postValue(Result.error(errorResponse.getMessage()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}