diff --git a/app/src/main/java/com/example/coach/data/ProfilDAO.java b/app/src/main/java/com/example/coach/data/ProfilDAO.java new file mode 100644 index 0000000..229ee92 --- /dev/null +++ b/app/src/main/java/com/example/coach/data/ProfilDAO.java @@ -0,0 +1,90 @@ +package com.example.coach.data; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.example.coach.model.Profil; + +import java.util.Date; + +public class ProfilDAO extends SQLiteOpenHelper { + + private static final String DATABASE_NAME = "coach.db"; + private static final int DATABASE_VERSION = 1; + private static final String TABLE_PROFIL = "profil"; + private static final String COL_POIDS = "poids"; + private static final String COL_TAILLE = "taille"; + private static final String COL_AGE = "age"; + private static final String COL_SEXE = "sexe"; + private static final String COL_DATEMESURE = "dateMesure"; + + /** + * @param db The database. + */ + @Override + public void onCreate(SQLiteDatabase db) { + String createTable = "CREATE TABLE " + TABLE_PROFIL + " (" + + COL_DATEMESURE + " INTEGER PRIMARY KEY, "+ + COL_POIDS + " INTEGER, "+ + COL_TAILLE + " INTEGER, "+ + COL_AGE + " INTEGER, "+ + COL_SEXE + " INTEGER)"; + db.execSQL(createTable); + } + + /** + * @param db The database. + * @param oldVersion The old database version. + * @param newVersion The new database version. + */ + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFIL); + onCreate(db); + } + + public ProfilDAO(Context context) + { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + public void insertProfil(Profil profil) + { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues values = new ContentValues(); + + values.put(COL_POIDS, profil.getPoids()); + values.put(COL_AGE, profil.getAge()); + values.put(COL_TAILLE, profil.getTaille()); + values.put(COL_SEXE, profil.getSexe()); + values.put(COL_DATEMESURE, profil.getDateMesure().getTime()); + + db.insert(TABLE_PROFIL, null, values); + db.close(); + } + + public Profil getLastProfil() + { + SQLiteDatabase db = this.getReadableDatabase(); + Profil profil = null; + String query = "SELECT * FROM " + TABLE_PROFIL + " ORDER BY " + COL_DATEMESURE + " DESC LIMIT 1"; + Cursor cursor = db.rawQuery(query, null); + + if(cursor.moveToFirst()){ + int poids = cursor.getInt(cursor.getColumnIndexOrThrow(COL_POIDS)); + int taille = cursor.getInt(cursor.getColumnIndexOrThrow(COL_TAILLE)); + int age = cursor.getInt(cursor.getColumnIndexOrThrow(COL_AGE)); + int sexe = cursor.getInt(cursor.getColumnIndexOrThrow(COL_SEXE)); + long date = cursor.getLong(cursor.getColumnIndexOrThrow(COL_DATEMESURE)); + + profil = new Profil(poids, taille, age, sexe, new Date(date)); + } + cursor.close(); + db.close(); + + return profil; + } +} 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..163dad8 100644 --- a/app/src/main/java/com/example/coach/presenter/CalculPresenter.java +++ b/app/src/main/java/com/example/coach/presenter/CalculPresenter.java @@ -1,6 +1,9 @@ package com.example.coach.presenter; +import android.content.Context; + import com.example.coach.contract.ICalculView; +import com.example.coach.data.ProfilDAO; import com.example.coach.model.Profil; import java.util.Date; @@ -10,13 +13,15 @@ import java.util.Date; */ public class CalculPresenter { private ICalculView vue; + private ProfilDAO profilDAO; /** * 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.profilDAO = new ProfilDAO(context); } /** @@ -30,6 +35,16 @@ public class CalculPresenter { public void creerProfil(Integer sexe, Integer poids, Integer taille, Integer age) { Profil profil = new Profil(poids, taille, age, sexe, new Date()); + profilDAO.insertProfil(profil); vue.AfficherResultat(profil.getImage(), profil.getImg(), profil.getMessage(), profil.normal()); } + + public void chargerDernierProfil() + { + Profil profil = profilDAO.getLastProfil(); + if(profil != null) + { + 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..cab702c 100644 --- a/app/src/main/java/com/example/coach/view/MainActivity.java +++ b/app/src/main/java/com/example/coach/view/MainActivity.java @@ -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.chargerDernierProfil(); } /**