diff --git a/app/src/main/java/com/example/coach/presenter/CalculPresenter.java b/app/src/main/java/com/example/coach/presenter/CalculPresenter.java index f4a67e9..5142eb4 100644 --- a/app/src/main/java/com/example/coach/presenter/CalculPresenter.java +++ b/app/src/main/java/com/example/coach/presenter/CalculPresenter.java @@ -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()); + } + } } diff --git a/app/src/main/java/com/example/coach/view/MainActivity.java b/app/src/main/java/com/example/coach/view/MainActivity.java index e9e3e25..0c2bf7f 100644 --- a/app/src/main/java/com/example/coach/view/MainActivity.java +++ b/app/src/main/java/com/example/coach/view/MainActivity.java @@ -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(); } /**