From d78c4f13ef07e9ff56ab6f15ef4ada1d40adc972 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 22 May 2022 18:49:10 +0200 Subject: [PATCH 01/10] Change SimpleItemRecyclerView into RecyclerView --- .../java/net/foucry/pilldroid/DrugListActivity.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 628f7fb..cdc5cdb 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -22,6 +22,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -73,7 +74,7 @@ public class DrugListActivity extends AppCompatActivity { private List prescriptionList; // used for prescriptions - private SimpleItemRecyclerViewAdapter mAdapter; + private RecyclerViewAdapter mAdapter; @Override public void onStart() { @@ -463,19 +464,19 @@ public class DrugListActivity extends AppCompatActivity { */ private void setupRecyclerView(@NonNull RecyclerView recyclerView) { recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext())); - mAdapter = new SimpleItemRecyclerViewAdapter(prescriptionList); + mAdapter = new RecyclerViewAdapter(prescriptionList); recyclerView.setAdapter(mAdapter); } /** * SimpleItemRecyclerViewAdapter */ - public class SimpleItemRecyclerViewAdapter extends - RecyclerView.Adapter { + public class RecyclerViewAdapter extends + RecyclerView.Adapter { private final List mValues; - SimpleItemRecyclerViewAdapter(List items) { + RecyclerViewAdapter(List items) { mValues = items; } From b78a4acc15b62aa2d188af066cb9a1969e16b828 Mon Sep 17 00:00:00 2001 From: jacques Date: Wed, 8 Jun 2022 11:27:34 +0200 Subject: [PATCH 02/10] Add trash icon --- app/src/main/res/drawable/ic_trash_can_outline.xml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/src/main/res/drawable/ic_trash_can_outline.xml diff --git a/app/src/main/res/drawable/ic_trash_can_outline.xml b/app/src/main/res/drawable/ic_trash_can_outline.xml new file mode 100644 index 0000000..07e6108 --- /dev/null +++ b/app/src/main/res/drawable/ic_trash_can_outline.xml @@ -0,0 +1,4 @@ + + + From 38ec2cb9ebbfcacadec7604d81439c165be513cb Mon Sep 17 00:00:00 2001 From: jacques Date: Wed, 8 Jun 2022 11:27:57 +0200 Subject: [PATCH 03/10] Add undo and french translation --- app/src/main/res/values-fr-rFR/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 28f8050..ffec178 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -68,4 +68,6 @@ Autorisation appareil photo manquante Oui, j\'ai compris Je comprends que le développeur de app_name n\'est pas responsable de la gestion de vos médicaments. CELA RELÈVE DE VOTRE SEULE RESPONSABILITË. + Annuler + Icone de poubelle \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd34a09..1d2fd64 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,4 +70,6 @@ Missing camera permission Yes, I understood I understood that de developer of app_name cannot be responsible ot your medication management. IT\'S YOU OWN RESPONSIBILITY. + Undo + Trash icon From 846f6ecbe9d544466fefe900959ce8a706d60800 Mon Sep 17 00:00:00 2001 From: jacques Date: Wed, 8 Jun 2022 11:28:17 +0200 Subject: [PATCH 04/10] Remove old comment --- app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java index 26265c6..35f39e8 100644 --- a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java +++ b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java @@ -50,7 +50,6 @@ public class AlarmReceiver extends BroadcastReceiver { PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); List prescriptionList = prescriptionsDAO.getAllMedics(); Prescription firstPrescription = null ; - //List drugs = dbHelper.getAllDrugs(); try { firstPrescription = prescriptionList.get(1); From b2f1df63508a62318cbde6f33f55a8f2366c97fe Mon Sep 17 00:00:00 2001 From: jacques Date: Wed, 8 Jun 2022 11:29:43 +0200 Subject: [PATCH 05/10] Add swipe with icon to delete medic from prescription. The database update is not yet coded --- .../foucry/pilldroid/DrugListActivity.java | 88 ++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index cdc5cdb..d7beaf7 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -9,6 +9,9 @@ import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.text.Editable; @@ -22,7 +25,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; -import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -31,9 +33,12 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; import androidx.room.Room; +import com.google.android.material.snackbar.Snackbar; import com.google.zxing.client.android.BuildConfig; import com.google.zxing.client.android.Intents; import com.journeyapps.barcodescanner.ScanOptions; @@ -466,7 +471,86 @@ public class DrugListActivity extends AppCompatActivity { recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext())); mAdapter = new RecyclerViewAdapter(prescriptionList); recyclerView.setAdapter(mAdapter); - } + + new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, (ItemTouchHelper.RIGHT|ItemTouchHelper.LEFT)) { + @Override + public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { + return false; + } + + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + int position = viewHolder.getBindingAdapterPosition(); + + Prescription prescription = prescriptionList.get(position); + + + // TODO: remove item form database + prescriptionList.remove(position); + mAdapter.notifyItemRemoved(position); + + Snackbar.make(recyclerView, prescription.getName(), + Snackbar.LENGTH_LONG).setAction(R.string.Undo, new View.OnClickListener() { + @Override + public void onClick(View v) { + prescriptionList.add(position, prescription); + mAdapter.notifyItemInserted(position); + } + }).show(); + } + + @Override + public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { + if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { + // Get RecyclerView item from the ViewHolder + View itemView = viewHolder.itemView; + + Paint p = new Paint(); + p.setColor(getColor(R.color.bg_screen4)); + Drawable icon; + icon = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_trash_can_outline); + + int xMarkMargin = (int) getApplicationContext().getResources().getDimension(R.dimen.fab_margin); + + assert icon != null; + int intrinsicWidth = icon.getIntrinsicWidth(); + int intrinsicHeight = icon.getIntrinsicHeight(); + int itemHeight = itemView.getBottom() - itemView.getTop(); + + + + if (dX > 0) { + // Draw Rect with varying right side, equal to displacement dX + c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX, + (float) itemView.getBottom(), p); + + int xMarkLeft = itemView.getLeft() + xMarkMargin; + int xMarkRight = 170; // Arbitrary value cannot find a way to make correct calculation + int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; + int xMarkBottom = itemView.getTop() + intrinsicHeight +xMarkTop; + icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); + + icon.draw(c); + } else { + // Draw Rect with varying left side, equal to the item's right side plus negative displacement dX + c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(), + (float) itemView.getRight(), (float) itemView.getBottom(), p); + + int xMarkLeft = itemView.getRight() - xMarkMargin - intrinsicWidth; + int xMarkRight = itemView.getRight() - xMarkMargin; + int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; + int xMarkBottom = xMarkTop + intrinsicHeight; + icon.setBounds(xMarkLeft, xMarkTop + 16, xMarkRight, xMarkBottom); + + icon.draw(c); + } + + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + } + }).attachToRecyclerView(recyclerView); + + } /** * SimpleItemRecyclerViewAdapter From 5043cda0fd18ad359e3bd1164a69d6e777f44788 Mon Sep 17 00:00:00 2001 From: jacques Date: Wed, 8 Jun 2022 19:01:24 +0200 Subject: [PATCH 06/10] Find a calculation to have same icon size in both swipe direction --- .../main/java/net/foucry/pilldroid/DrugListActivity.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index d7beaf7..8f3b1f4 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -525,9 +525,9 @@ public class DrugListActivity extends AppCompatActivity { (float) itemView.getBottom(), p); int xMarkLeft = itemView.getLeft() + xMarkMargin; - int xMarkRight = 170; // Arbitrary value cannot find a way to make correct calculation + int xMarkRight = itemView.getLeft() + xMarkMargin + intrinsicWidth; int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; - int xMarkBottom = itemView.getTop() + intrinsicHeight +xMarkTop; + int xMarkBottom = xMarkTop + intrinsicHeight;// +xMarkTop; icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); icon.draw(c); @@ -540,7 +540,7 @@ public class DrugListActivity extends AppCompatActivity { int xMarkRight = itemView.getRight() - xMarkMargin; int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; int xMarkBottom = xMarkTop + intrinsicHeight; - icon.setBounds(xMarkLeft, xMarkTop + 16, xMarkRight, xMarkBottom); + icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); icon.draw(c); } From 38a6c47ef5d434c1fa2ca3d4c0e4ed4fcd1c311a Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 10 Jun 2022 20:53:18 +0200 Subject: [PATCH 07/10] Add pen icon --- app/src/main/res/drawable/ic_edit_black_48dp.xml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/src/main/res/drawable/ic_edit_black_48dp.xml diff --git a/app/src/main/res/drawable/ic_edit_black_48dp.xml b/app/src/main/res/drawable/ic_edit_black_48dp.xml new file mode 100644 index 0000000..c027b1c --- /dev/null +++ b/app/src/main/res/drawable/ic_edit_black_48dp.xml @@ -0,0 +1,4 @@ + + + From 024b97751be17b27d5a035400dc726fb8b1b3f1c Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 10 Jun 2022 20:55:25 +0200 Subject: [PATCH 08/10] Add swipoe Right to Left to remove from list and prescription database Add swipe left to Right to edit prescription detail --- .../foucry/pilldroid/DrugListActivity.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 8f3b1f4..e506f99 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -485,9 +485,20 @@ public class DrugListActivity extends AppCompatActivity { Prescription prescription = prescriptionList.get(position); - // TODO: remove item form database - prescriptionList.remove(position); - mAdapter.notifyItemRemoved(position); + if (direction == ItemTouchHelper.LEFT) { + prescriptionList.remove(position); + mAdapter.notifyItemRemoved(position); + // Remove item form database + PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO();; + prescriptionsDAO.delete(prescription); + } else { + // Call DetailView + Intent intent = new Intent(getApplicationContext(), DrugDetailActivity.class); + intent.putExtra("prescription", prescription); + startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); + } + + Snackbar.make(recyclerView, prescription.getName(), Snackbar.LENGTH_LONG).setAction(R.string.Undo, new View.OnClickListener() { @@ -506,7 +517,6 @@ public class DrugListActivity extends AppCompatActivity { View itemView = viewHolder.itemView; Paint p = new Paint(); - p.setColor(getColor(R.color.bg_screen4)); Drawable icon; icon = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_trash_can_outline); @@ -517,9 +527,10 @@ public class DrugListActivity extends AppCompatActivity { int intrinsicHeight = icon.getIntrinsicHeight(); int itemHeight = itemView.getBottom() - itemView.getTop(); - - if (dX > 0) { + p.setColor(getColor(R.color.bg_screen3)); + icon = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_edit_black_48dp); + // Draw Rect with varying right side, equal to displacement dX c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX, (float) itemView.getBottom(), p); @@ -531,11 +542,15 @@ public class DrugListActivity extends AppCompatActivity { icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); icon.draw(c); + + // TODO : goto edit details } else { + p.setColor(getColor(R.color.bg_screen4)); // Draw Rect with varying left side, equal to the item's right side plus negative displacement dX c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom(), p); + int xMarkLeft = itemView.getRight() - xMarkMargin - intrinsicWidth; int xMarkRight = itemView.getRight() - xMarkMargin; int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; @@ -543,6 +558,7 @@ public class DrugListActivity extends AppCompatActivity { icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); icon.draw(c); + // TODO : remove prescription from database } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); From e42626fbfa43051db660005a96c61383594c19d9 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 10 Jun 2022 21:01:04 +0200 Subject: [PATCH 09/10] Optimizations --- .../java/net/foucry/pilldroid/DrugListActivity.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index e506f99..47eca77 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -489,7 +489,7 @@ public class DrugListActivity extends AppCompatActivity { prescriptionList.remove(position); mAdapter.notifyItemRemoved(position); // Remove item form database - PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO();; + PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); prescriptionsDAO.delete(prescription); } else { // Call DetailView @@ -539,11 +539,9 @@ public class DrugListActivity extends AppCompatActivity { int xMarkRight = itemView.getLeft() + xMarkMargin + intrinsicWidth; int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; int xMarkBottom = xMarkTop + intrinsicHeight;// +xMarkTop; + assert icon != null; icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); - icon.draw(c); - - // TODO : goto edit details } else { p.setColor(getColor(R.color.bg_screen4)); // Draw Rect with varying left side, equal to the item's right side plus negative displacement dX @@ -557,9 +555,8 @@ public class DrugListActivity extends AppCompatActivity { int xMarkBottom = xMarkTop + intrinsicHeight; icon.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); - icon.draw(c); - // TODO : remove prescription from database - } + } + icon.draw(c); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } From 6b6da518948b412b18b8f1d74766d51459216624 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 10 Jun 2022 21:01:04 +0200 Subject: [PATCH 10/10] Optimizations --- app/src/main/java/net/foucry/pilldroid/DrugListActivity.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 47eca77..a6cd73c 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -484,7 +484,6 @@ public class DrugListActivity extends AppCompatActivity { Prescription prescription = prescriptionList.get(position); - if (direction == ItemTouchHelper.LEFT) { prescriptionList.remove(position); mAdapter.notifyItemRemoved(position);