Refactoring using Medicine and Prescription Room Databases

This commit is contained in:
jacques 2022-03-16 17:09:20 +01:00
parent a40dd5fe20
commit 7b76992998
2 changed files with 20 additions and 15 deletions

View file

@ -16,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import net.foucry.pilldroid.dao.PrescriptionsDAO; import net.foucry.pilldroid.dao.PrescriptionsDAO;
import net.foucry.pilldroid.databases.PrescriptionDatabase;
import net.foucry.pilldroid.models.Prescription; import net.foucry.pilldroid.models.Prescription;
import java.util.Date; import java.util.Date;
@ -111,7 +112,7 @@ public class DrugDetailActivity extends AppCompatActivity {
PrescriptionDatabase prescriptions = null; PrescriptionDatabase prescriptions = null;
assert false; assert false;
PrescriptionsDAO prescriptionsDAO = prescriptions.getMedicDAO(); PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO();
Prescription newPrescription = prescriptionsDAO.getMedicByCIP13(aPrescription.getCip13()); Prescription newPrescription = prescriptionsDAO.getMedicByCIP13(aPrescription.getCip13());

View file

@ -37,7 +37,11 @@ import androidx.room.Room;
import com.google.zxing.client.android.Intents; import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.ScanOptions; import com.journeyapps.barcodescanner.ScanOptions;
import net.foucry.pilldroid.dao.MedicinesDAO;
import net.foucry.pilldroid.dao.PrescriptionsDAO; import net.foucry.pilldroid.dao.PrescriptionsDAO;
import net.foucry.pilldroid.databases.MedicineDatabase;
import net.foucry.pilldroid.databases.PrescriptionDatabase;
import net.foucry.pilldroid.models.Medicine;
import net.foucry.pilldroid.models.Prescription; import net.foucry.pilldroid.models.Prescription;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -65,7 +69,7 @@ public class DrugListActivity extends AppCompatActivity {
private static final String TAG = DrugListActivity.class.getName(); private static final String TAG = DrugListActivity.class.getName();
public PrescriptionDatabase prescriptions; public PrescriptionDatabase prescriptions;
public PrescriptionDatabase medications; public MedicineDatabase medicines;
private List<Prescription> prescriptionList; // used for prescriptions private List<Prescription> prescriptionList; // used for prescriptions
@ -88,8 +92,8 @@ public class DrugListActivity extends AppCompatActivity {
); );
} }
medications = Room medicines = Room
.databaseBuilder(getApplicationContext(), PrescriptionDatabase.class, "medications") .databaseBuilder(getApplicationContext(), MedicineDatabase.class, "medicines")
.createFromAsset("drugs.db") .createFromAsset("drugs.db")
.build(); .build();
@ -239,11 +243,11 @@ public class DrugListActivity extends AppCompatActivity {
} }
// Get Drug from database // Get Drug from database
PrescriptionsDAO medicationDAO = medications.getMedicDAO(); // TODO MedicinesDAO medicinesDAO = medicines.getMedicinesDAO(); // TODO
final Prescription scannedMedication = medicationDAO.getMedicByCIP13(cip13); final Medicine scannedMedicine = medicinesDAO.getMedicineByCIP13(cip13);
// add Drug to prescription database // add Drug to prescription database
askToAddInDB(scannedMedication); askToAddInDB(scannedMedicine);
} }
} }
} }
@ -334,10 +338,10 @@ public class DrugListActivity extends AppCompatActivity {
.setPositiveButton("OK", (dialog, id) -> { .setPositiveButton("OK", (dialog, id) -> {
String cip13 = editText.getText().toString(); String cip13 = editText.getText().toString();
PrescriptionsDAO medicationsDAO = medications.getMedicDAO(); // TODO MedicinesDAO medicineDAO = medicines.getMedicinesDAO(); // TODO
Prescription aPrescription = medicationsDAO.getMedicByCIP13(cip13); Medicine aMedicine = medicineDAO.getMedicineByCIP13(cip13);
//Prescription aPrescription = medications.getDrugByCIP13(cip13); //Prescription aPrescription = medications.getDrugByCIP13(cip13);
askToAddInDB(aPrescription); askToAddInDB(aMedicine);
}) })
.setNegativeButton("Cancel", .setNegativeButton("Cancel",
(dialog, id) -> dialog.cancel()); (dialog, id) -> dialog.cancel());
@ -368,14 +372,14 @@ public class DrugListActivity extends AppCompatActivity {
* Ask if the drug found in the database should be include in the * Ask if the drug found in the database should be include in the
* user database * user database
* *
* @param aPrescription Prescription- medication to be added * @param aMedicine Prescription- medication to be added
*/ */
private void askToAddInDB(Prescription aPrescription) { private void askToAddInDB(Medicine aMedicine) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this); AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(getString(R.string.app_name)); dlg.setTitle(getString(R.string.app_name));
if (aPrescription != null) { if (aMedicine != null) {
String msg = aPrescription.getName() + " " + getString(R.string.msgFound); String msg = aMedicine.getName() + " " + getString(R.string.msgFound);
dlg.setMessage(msg); dlg.setMessage(msg);
dlg.setNegativeButton(getString(R.string.button_cancel), (dialog, which) -> { dlg.setNegativeButton(getString(R.string.button_cancel), (dialog, which) -> {
@ -383,7 +387,7 @@ public class DrugListActivity extends AppCompatActivity {
}); });
dlg.setPositiveButton(getString(R.string.button_ok), (dialog, which) -> { dlg.setPositiveButton(getString(R.string.button_ok), (dialog, which) -> {
// Add Drug to DB then try to show it // Add Drug to DB then try to show it
addDrugToList(aPrescription); addDrugToList(Utils.medicine2prescription(aMedicine));
}); });
} else { } else {
dlg.setMessage(getString(R.string.msgNotFound)); dlg.setMessage(getString(R.string.msgNotFound));