Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| bb998f039d |
6 changed files with 121 additions and 21 deletions
|
|
@ -18,27 +18,28 @@ public class HelperApi {
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> void call(Call<ResponseApi<T>> call, ICallbackApi<T> callback)
|
public static <T> void call(Call<ResponseApi<T>> call, ICallbackApi<T> callback) {
|
||||||
{
|
|
||||||
call.enqueue(new Callback<ResponseApi<T>>() {
|
call.enqueue(new Callback<ResponseApi<T>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<ResponseApi<T>> call, Response<ResponseApi<T>> response) {
|
public void onResponse(Call<ResponseApi<T>> call, Response<ResponseApi<T>> response) {
|
||||||
Log.d("API", "code : " + response.body().getCode() +
|
// 1. On vérifie si la requête HTTP a réussi ET si on a un corps de réponse
|
||||||
" message : " + response.body().getMessage() +
|
|
||||||
" result : " + response.body().getResult()
|
|
||||||
);
|
|
||||||
if (response.isSuccessful() && response.body() != null) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
|
// On peut maintenant logger sans risque
|
||||||
|
Log.d("API", "code : " + response.body().getCode() +
|
||||||
|
" message : " + response.body().getMessage());
|
||||||
|
|
||||||
callback.onSuccess(response.body().getResult());
|
callback.onSuccess(response.body().getResult());
|
||||||
} else {
|
} else {
|
||||||
|
// Cas d'erreur HTTP (ex: 404) ou body null
|
||||||
callback.onError();
|
callback.onError();
|
||||||
Log.e("API", "Erreur API: " + response.code());
|
Log.e("API", "Erreur HTTP ou Body null. Code: " + response.code());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<ResponseApi<T>> call, Throwable throwable) {
|
public void onFailure(Call<ResponseApi<T>> call, Throwable throwable) {
|
||||||
callback.onError();
|
callback.onError();
|
||||||
Log.e("API", "Erreur API", throwable);
|
Log.e("API", "Erreur réseau / désérialisation", throwable);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,4 +22,7 @@ public interface IRequestApi {
|
||||||
|
|
||||||
@DELETE("profil/{champs}")
|
@DELETE("profil/{champs}")
|
||||||
Call<ResponseApi<Integer>> supprProfil(@Path(value = "champs", encoded = true) String profilJson);
|
Call<ResponseApi<Integer>> supprProfil(@Path(value = "champs", encoded = true) String profilJson);
|
||||||
|
|
||||||
|
@DELETE("profil/{keep}")
|
||||||
|
Call<ResponseApi<Integer>> purgeProfils(@Path(value = "keep", encoded = true) String profilJson);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.example.coach.contract;
|
||||||
|
|
||||||
|
public interface IMainView extends IAllView{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.example.coach.presenter;
|
||||||
|
|
||||||
|
import com.example.coach.api.HelperApi;
|
||||||
|
import com.example.coach.api.ICallbackApi;
|
||||||
|
import com.example.coach.api.IRequestApi;
|
||||||
|
import com.example.coach.contract.IMainView;
|
||||||
|
|
||||||
|
public class MainPresenter {
|
||||||
|
private IMainView vue;
|
||||||
|
public void purgeBDD(Integer nb, ICallbackApi<Void> callback)
|
||||||
|
{
|
||||||
|
String json = "{\"keep\":\"" + nb + "\"}";
|
||||||
|
HelperApi.call(HelperApi.getApi().purgeProfils(json), new ICallbackApi<Integer>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(Integer result) {
|
||||||
|
if (result >= 1) {
|
||||||
|
vue.afficherMessage("profils supprimés : " + result);
|
||||||
|
callback.onSuccess(null);
|
||||||
|
}else{
|
||||||
|
vue.afficherMessage("BDD déjà purgée");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError() {
|
||||||
|
vue.afficherMessage("échec suppression profils");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public MainPresenter(IMainView vue)
|
||||||
|
{
|
||||||
|
this.vue = vue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,10 @@ package com.example.coach.view;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
@ -11,11 +14,16 @@ import androidx.core.view.ViewCompat;
|
||||||
import androidx.core.view.WindowInsetsCompat;
|
import androidx.core.view.WindowInsetsCompat;
|
||||||
|
|
||||||
import com.example.coach.R;
|
import com.example.coach.R;
|
||||||
|
import com.example.coach.api.ICallbackApi;
|
||||||
|
import com.example.coach.contract.IMainView;
|
||||||
|
import com.example.coach.presenter.MainPresenter;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity implements IMainView {
|
||||||
|
|
||||||
private ImageButton btnMonIMG;
|
private ImageButton btnMonIMG;
|
||||||
private ImageButton btnMonHistorique;
|
private ImageButton btnMonHistorique;
|
||||||
|
private Button btnPurge;
|
||||||
|
private MainPresenter presenter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
@ -34,12 +42,14 @@ public class MainActivity extends AppCompatActivity {
|
||||||
{
|
{
|
||||||
chargeObjetsGraphiques();
|
chargeObjetsGraphiques();
|
||||||
creerMenu();
|
creerMenu();
|
||||||
|
presenter = new MainPresenter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void chargeObjetsGraphiques()
|
private void chargeObjetsGraphiques()
|
||||||
{
|
{
|
||||||
btnMonIMG = (ImageButton) findViewById(R.id.btnMonIMG);
|
btnMonIMG = (ImageButton) findViewById(R.id.btnMonIMG);
|
||||||
btnMonHistorique = (ImageButton) findViewById(R.id.btnMonHistorique);
|
btnMonHistorique = (ImageButton) findViewById(R.id.btnMonHistorique);
|
||||||
|
btnPurge = (Button) findViewById(R.id.btnPurge);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ecouteMenu(Class classe)
|
private void ecouteMenu(Class classe)
|
||||||
|
|
@ -52,5 +62,30 @@ public class MainActivity extends AppCompatActivity {
|
||||||
{
|
{
|
||||||
btnMonIMG.setOnClickListener(v -> ecouteMenu(CalculActivity.class));
|
btnMonIMG.setOnClickListener(v -> ecouteMenu(CalculActivity.class));
|
||||||
btnMonHistorique.setOnClickListener(v -> ecouteMenu(HistoActivity.class));
|
btnMonHistorique.setOnClickListener(v -> ecouteMenu(HistoActivity.class));
|
||||||
|
btnPurge.setOnClickListener(v -> btnPurge_clic());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btnPurge_clic()
|
||||||
|
{
|
||||||
|
presenter.purgeBDD(5, new ICallbackApi<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(Void result) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void afficherMessage(String message) {
|
||||||
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -54,20 +54,41 @@
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:id="@+id/textView5"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:gravity="center_horizontal"
|
android:layout_weight="1"
|
||||||
android:text="mon historique"
|
android:orientation="vertical">
|
||||||
android:textSize="34sp" />
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView5"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:text="mon historique"
|
||||||
|
android:textSize="34sp" />
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/btnMonHistorique"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
app:srcCompat="@drawable/historique" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnPurge"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Purger BDD" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<ImageButton
|
|
||||||
android:id="@+id/btnMonHistorique"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center_horizontal"
|
|
||||||
app:srcCompat="@drawable/historique" />
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Reference in a new issue