From cdbaf798239a0f68ef73ec78eceb29e7f12350be Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Tue, 5 May 2020 22:06:26 +0200 Subject: [PATCH] change create List behavior --- .../pilldroid/MedicamentListActivity.java | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 2baa9a7..ebeee23 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -160,7 +160,7 @@ public class MedicamentListActivity extends AppCompatActivity { } } - if (medicaments == null) { + /* if (medicaments == null) { medicaments = dbHelper.getAllDrugs(); Collections.sort(medicaments, new Comparator() { @@ -170,6 +170,8 @@ public class MedicamentListActivity extends AppCompatActivity { } }); } +*/ + constructMedsList(); mRecyclerView = findViewById(R.id.medicament_list); assert mRecyclerView != null; @@ -227,15 +229,6 @@ public class MedicamentListActivity extends AppCompatActivity { */ public void newStockCalculation() { - Medicament currentMedicament; - dbHelper = new DBHelper(this); - - for (int position = 0 ; position < this. getCount() ; position++ ) { - currentMedicament = this.getItem(position); - currentMedicament.newStock(currentMedicament.getStock()); - dbHelper.updateDrug(currentMedicament); - } - // TODO: si un des médicaments est en rouge, on déclanche une notification visuelle pour dans 5 secondes Calendar calendar = Calendar.getInstance(); @@ -451,6 +444,32 @@ public class MedicamentListActivity extends AppCompatActivity { }); } + protected void constructMedsList() + { + Medicament currentMedicament; + dbHelper = new DBHelper(this); + + if (!medicaments.isEmpty()) { + medicaments.clear(); + } + + medicaments = dbHelper.getAllDrugs(); + + Collections.sort(medicaments, new Comparator() { + @Override + public int compare(Medicament lhs, Medicament rhs) { + return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); + } + }); + + for (int position = 0 ; position < this. getCount() ; position++ ) { + currentMedicament = this.getItem(position); + currentMedicament.newStock(currentMedicament.getStock()); + dbHelper.updateDrug(currentMedicament); + } + + + } @Override public int getItemCount() { return mValues.size();