S3b : sérialisation

This commit is contained in:
Erwann PHILIPPE 2026-02-03 16:42:38 +01:00
parent 102a28da49
commit eb93410553
2 changed files with 30 additions and 3 deletions

View file

@ -1,7 +1,11 @@
package com.example.coach.presenter;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.coach.contract.ICalculView;
import com.example.coach.model.Profil;
import com.google.gson.Gson;
import java.util.Date;
@ -10,13 +14,19 @@ import java.util.Date;
*/
public class CalculPresenter {
private ICalculView vue;
private static final String NOM_FIC = "coach_records";
private static final String PROFIL_CLE = "profil_json";
private Gson gson;
private SharedPreferences prefs;
/**
* Constructeur : valorise la propriété permettant d'accéder à la vue
* @param vue
*/
public CalculPresenter(ICalculView vue) {
public CalculPresenter(ICalculView vue, Context context) {
this.vue = vue;
this.prefs = context.getSharedPreferences(NOM_FIC, Context.MODE_PRIVATE);
this.gson = new Gson();
}
/**
@ -30,6 +40,22 @@ public class CalculPresenter {
public void creerProfil(Integer sexe, Integer poids, Integer taille, Integer age)
{
Profil profil = new Profil(poids, taille, age, sexe, new Date());
sauvegarderProfil(profil);
vue.AfficherResultat(profil.getImage(), profil.getImg(), profil.getMessage(), profil.normal());
}
private void sauvegarderProfil(Profil profil)
{
String json = gson.toJson(profil);
prefs.edit().putString(PROFIL_CLE, json).apply();
}
public void chargerProfil()
{
String json = prefs.getString(PROFIL_CLE, null);
if(json != null){
Profil profil = gson.fromJson(json, Profil.class);
vue.remplirChamps(profil.getPoids(), profil.getTaille(), profil.getAge(), profil.getSexe());
}
}
}

View file

@ -88,7 +88,7 @@ public class MainActivity extends AppCompatActivity implements ICalculView {
if(sexe == 1){
rdHomme.setChecked(true);
}else{
rdFemme.setChecked(false);
rdFemme.setChecked(true);
}
}
@ -98,8 +98,9 @@ public class MainActivity extends AppCompatActivity implements ICalculView {
private void init()
{
chargeObjetsGraphiques();
presenter = new CalculPresenter(this);
presenter = new CalculPresenter(this, this);
btnCalc.setOnClickListener(v -> btnCalcClic());
presenter.chargerProfil();
}
/**