From 2f5432620662d6e13241ea24dee17919030dfb73 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <87148630+Jean-BaptisteC@users.noreply.github.com> Date: Tue, 26 Mar 2024 21:17:58 +0100 Subject: [PATCH] Use lambda everywhere (#23) --- .../pilldroid/CustomScannerActivity.java | 23 ++--- .../java/net/foucry/pilldroid/DBHelper.java | 13 +-- .../foucry/pilldroid/DrugDetailActivity.java | 15 ++- .../foucry/pilldroid/DrugListActivity.java | 92 ++++++++----------- .../main/java/net/foucry/pilldroid/Utils.java | 13 +-- .../net/foucry/pilldroid/WelcomeActivity.java | 11 +-- 6 files changed, 66 insertions(+), 101 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java b/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java index 3806408..1a0a0c6 100644 --- a/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java @@ -57,19 +57,16 @@ public class CustomScannerActivity extends Activity implements DecoratedBarcodeV //changeMaskColor(null); changeLaserVisibility(true); - barcodeScannerView.decodeSingle(new BarcodeCallback() { - @Override - public void barcodeResult(BarcodeResult result) { - Intent scanResult = new Intent(); - //Bundle scanResultBundle = new Bundle(); - scanResult.putExtra("Barcode Content", result.getText()); - scanResult.putExtra("Barcode Format name", result.getBarcodeFormat().name()); - scanResult.putExtra("returnCode", captureIntentBundle.getInt("returnCode")); - scanResult.putExtra("resultCode", 1); - CustomScannerActivity.this.setResult(RESULT_OK, scanResult); - Log.d(TAG, "scanResult == " + scanResult); - finish(); - } + barcodeScannerView.decodeSingle(result -> { + Intent scanResult = new Intent(); + //Bundle scanResultBundle = new Bundle(); + scanResult.putExtra("Barcode Content", result.getText()); + scanResult.putExtra("Barcode Format name", result.getBarcodeFormat().name()); + scanResult.putExtra("returnCode", captureIntentBundle.getInt("returnCode")); + scanResult.putExtra("resultCode", 1); + CustomScannerActivity.this.setResult(RESULT_OK, scanResult); + Log.d(TAG, "scanResult == " + scanResult); + finish(); }); } diff --git a/app/src/main/java/net/foucry/pilldroid/DBHelper.java b/app/src/main/java/net/foucry/pilldroid/DBHelper.java index 16fa01a..073c29d 100644 --- a/app/src/main/java/net/foucry/pilldroid/DBHelper.java +++ b/app/src/main/java/net/foucry/pilldroid/DBHelper.java @@ -270,14 +270,11 @@ class DBHelper extends SQLiteOpenHelper { Log.d(TAG, "Before sort == " + drugs); - drugs.sort(new Comparator() { - @Override - public int compare(Drug lhs, Drug rhs) { - if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0) - return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); - else - return (int) (lhs.getStock() - rhs.getStock()); - } + drugs.sort((lhs, rhs) -> { + if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0) + return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); + else + return (int) (lhs.getStock() - rhs.getStock()); }); Log.d(TAG, "After sort " + drugs); diff --git a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java index b228af7..7e3c5ab 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java @@ -47,16 +47,13 @@ public class DrugDetailActivity extends AppCompatActivity { } ImageButton fab = findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Log.d(TAG, "Click on save icon"); + fab.setOnClickListener(v -> { + Log.d(TAG, "Click on save icon"); - getDrugChanges(); - setResult(1); - finish(); - overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right); - } + getDrugChanges(); + setResult(1); + finish(); + overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right); }); // Show the Up button in the action bar. diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index fa3a9c0..7fdd535 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -375,21 +375,16 @@ public class DrugListActivity extends AppCompatActivity { } } }); - ok.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - dialog.cancel(); - Log.i("EditText Value",editText.getEditableText().toString()); - MedicinesDAO medicinesDAO = medicines.getMedicinesDAO(); - Medicine aMedicine = medicinesDAO.getMedicineByCIP13(cip13); - askToAddInDB(aMedicine); - } + ok.setOnClickListener(v -> { + dialog.cancel(); + Log.i("EditText Value",editText.getEditableText().toString()); + MedicinesDAO medicinesDAO = medicines.getMedicinesDAO(); + Medicine aMedicine = medicinesDAO.getMedicineByCIP13(cip13); + askToAddInDB(aMedicine); }); - cancel.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.cancel(); - Log.i(TAG, "dismiss dialog"); - } + cancel.setOnClickListener(v -> { + dialog.cancel(); + Log.i(TAG, "dismiss dialog"); }); dialog.show(); @@ -423,27 +418,21 @@ public class DrugListActivity extends AppCompatActivity { cpl.setEnabled(false); } icon.setImageResource(R.drawable.tickmark); - btn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - // TODO Auto-generated method stub - dlg.dismiss(); - finish(); - addDrugToList(Utils.medicine2prescription(aMedicine)); - } + btn.setOnClickListener(v -> { + // TODO Auto-generated method stub + dlg.dismiss(); + finish(); + addDrugToList(Utils.medicine2prescription(aMedicine)); }); } else { msgString = getString(R.string.msgNotFound); msg.setText(msgString); cpl.setText(""); icon.setImageResource(R.drawable.tickcross); - btn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - // TODO Auto-generated method stub - dlg.dismiss(); - finish(); - } + btn.setOnClickListener(v -> { + // TODO Auto-generated method stub + dlg.dismiss(); + finish(); }); } dlg.show(); @@ -521,13 +510,10 @@ public class DrugListActivity extends AppCompatActivity { } 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(); + Snackbar.LENGTH_LONG).setAction(R.string.Undo, v -> { + prescriptionList.add(position, prescription); + mAdapter.notifyItemInserted(position); + }).show(); } @Override @@ -654,17 +640,14 @@ public class DrugListActivity extends AppCompatActivity { holder.mView.setBackgroundResource(R.drawable.gradient_bg); holder.mIconView.setImageResource(R.drawable.ic_suspended_pill); - holder.mView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Prescription aPrescription = mValues.get(position); - Context context = v.getContext(); - Intent intent = new Intent(context, DrugDetailActivity.class); - intent.putExtra("prescription", aPrescription); - startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); - overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left); + holder.mView.setOnClickListener(v -> { + Prescription aPrescription = mValues.get(position); + Context context = v.getContext(); + Intent intent = new Intent(context, DrugDetailActivity.class); + intent.putExtra("prescription", aPrescription); + startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); + overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left); - } }); } else { int remainingStock = (int) Math.floor(mValues.get(position).getStock() / mValues.get(position).getTake()); @@ -680,16 +663,13 @@ public class DrugListActivity extends AppCompatActivity { holder.mIconView.setImageResource(R.drawable.ok_stock_vect); } - holder.mView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Prescription prescription = mValues.get(position); - Context context = v.getContext(); - Intent intent = new Intent(context, DrugDetailActivity.class); - intent.putExtra("prescription", prescription); - startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); - overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left); - } + holder.mView.setOnClickListener(v -> { + Prescription prescription = mValues.get(position); + Context context = v.getContext(); + Intent intent = new Intent(context, DrugDetailActivity.class); + intent.putExtra("prescription", prescription); + startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); + overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left); }); } } diff --git a/app/src/main/java/net/foucry/pilldroid/Utils.java b/app/src/main/java/net/foucry/pilldroid/Utils.java index bca6971..2abea0e 100644 --- a/app/src/main/java/net/foucry/pilldroid/Utils.java +++ b/app/src/main/java/net/foucry/pilldroid/Utils.java @@ -49,14 +49,11 @@ public class Utils { } public static void sortPrescriptionList(List prescriptionList) { - prescriptionList.sort(new Comparator<>() { - @Override - public int compare(Prescription lhs, Prescription rhs) { - if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0) - return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); - else - return (int) (lhs.getStock() - rhs.getStock()); - } + prescriptionList.sort((lhs, rhs) -> { + if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0) + return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); + else + return (int) (lhs.getStock() - rhs.getStock()); }); } diff --git a/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java b/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java index 0d6505d..f6744d8 100644 --- a/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java @@ -156,13 +156,10 @@ public class WelcomeActivity extends AppCompatActivity { } //icon.setImageResource(R.drawable.pilldroid_icon); btn.setText(R.string.Yes); - btn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - // TODO Auto-generated method stub - dlg.dismiss(); - finish(); - } + btn.setOnClickListener(v -> { + // TODO Auto-generated method stub + dlg.dismiss(); + finish(); }); }