diff --git a/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json b/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json index 64b56b9..898897c 100644 --- a/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json +++ b/app/schemas/net.foucry.pilldroid.databases.PrescriptionDatabase/1.json @@ -2,11 +2,11 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "46c4983931eaf7414e25dfd9e1a55e0f", + "identityHash": "7d1384a7162a98602e19c3ee54b7aee5", "entities": [ { "tableName": "prescriptions", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`cis` TEXT NOT NULL, `cip13` TEXT, `name` TEXT, `administration_mode` TEXT, `presentation` TEXT, `stock` REAL, `take` REAL, `warning` INTEGER, `alert` INTEGER, `last_update` INTEGER, PRIMARY KEY(`cis`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`cis` TEXT NOT NULL, `cip13` TEXT, `name` TEXT, `administration_mode` TEXT, `presentation` TEXT, `stock` REAL, `take` REAL, `warning` INTEGER, `alert` INTEGER, `last_update` INTEGER, `label_group` TEXT, `genetic_type` INTEGER, PRIMARY KEY(`cis`))", "fields": [ { "fieldPath": "cis", @@ -67,6 +67,18 @@ "columnName": "last_update", "affinity": "INTEGER", "notNull": false + }, + { + "fieldPath": "label_group", + "columnName": "label_group", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "genetic_type", + "columnName": "genetic_type", + "affinity": "INTEGER", + "notNull": false } ], "primaryKey": { @@ -82,7 +94,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '46c4983931eaf7414e25dfd9e1a55e0f')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7d1384a7162a98602e19c3ee54b7aee5')" ] } } \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java index eea98e9..5e7cd07 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java @@ -55,7 +55,7 @@ public class DrugDetailActivity extends AppCompatActivity { public void onClick(View view) { Log.d(TAG, "Click on save icon"); - getMDrugChanges(); + getDrugChanges(); setResult(1); finish(); overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right); @@ -107,11 +107,10 @@ public class DrugDetailActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } - private void getMDrugChanges() { + private void getDrugChanges() { Log.d(TAG, "Time to save new values"); - PrescriptionDatabase prescriptions = null; - assert false; + PrescriptionDatabase prescriptions = PrescriptionDatabase.getInstanceDatabase(this); PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); Prescription newPrescription = prescriptionsDAO.getMedicByCIP13(aPrescription.getCip13()); diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 6906407..628f7fb 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -93,17 +93,10 @@ public class DrugListActivity extends AppCompatActivity { } // Create medicines Room database from drugs.db files - medicines = Room - .databaseBuilder(getApplicationContext(), MedicineDatabase.class, "medicines") - .createFromAsset("drugs.db") - .allowMainThreadQueries() - .build(); + medicines = MedicineDatabase.getInstanceDatabase(this); // Create prescriptions Room database - prescriptions = Room - .databaseBuilder(getApplicationContext(), PrescriptionDatabase.class, "prescriptions") - .allowMainThreadQueries() - .build(); + prescriptions = PrescriptionDatabase.getInstanceDatabase(this); // Manually migrate old database to room PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); diff --git a/app/src/main/java/net/foucry/pilldroid/Utils.java b/app/src/main/java/net/foucry/pilldroid/Utils.java index 0d8c037..6e2f416 100644 --- a/app/src/main/java/net/foucry/pilldroid/Utils.java +++ b/app/src/main/java/net/foucry/pilldroid/Utils.java @@ -1,7 +1,5 @@ package net.foucry.pilldroid; -import androidx.room.Database; - import net.foucry.pilldroid.models.Medicine; import net.foucry.pilldroid.models.Prescription; diff --git a/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java b/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java index 944fd9f..0f50517 100644 --- a/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java +++ b/app/src/main/java/net/foucry/pilldroid/databases/MedicineDatabase.java @@ -1,6 +1,9 @@ package net.foucry.pilldroid.databases; +import android.content.Context; + import androidx.room.Database; +import androidx.room.Room; import androidx.room.RoomDatabase; import net.foucry.pilldroid.dao.MedicinesDAO; @@ -12,5 +15,21 @@ import net.foucry.pilldroid.models.Medicine; ) public abstract class MedicineDatabase extends RoomDatabase { + private static MedicineDatabase INSTANCE; public abstract MedicinesDAO getMedicinesDAO(); + public static MedicineDatabase getInstanceDatabase(Context context) { + if (INSTANCE == null) { + INSTANCE = + Room + .databaseBuilder(context.getApplicationContext(), MedicineDatabase.class, "medicines") + .createFromAsset("drugs.db") + .allowMainThreadQueries() + .build(); + } + return INSTANCE; + } + + public static void destroyInstance() { + INSTANCE = null; + } } \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java b/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java index 405d285..e8a3387 100644 --- a/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java +++ b/app/src/main/java/net/foucry/pilldroid/databases/PrescriptionDatabase.java @@ -1,17 +1,39 @@ package net.foucry.pilldroid.databases; +import android.content.Context; + import androidx.room.AutoMigration; import androidx.room.Database; +import androidx.room.Room; import androidx.room.RoomDatabase; import net.foucry.pilldroid.dao.PrescriptionsDAO; import net.foucry.pilldroid.models.Prescription; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + @Database( version = 1, entities = {Prescription.class} ) public abstract class PrescriptionDatabase extends RoomDatabase { + private static PrescriptionDatabase INSTANCE; public abstract PrescriptionsDAO getPrescriptionsDAO(); + public static PrescriptionDatabase getInstanceDatabase(Context context) { + if (INSTANCE == null) { + INSTANCE = + Room + .databaseBuilder(context.getApplicationContext(), + PrescriptionDatabase.class, "prescriptions") + .allowMainThreadQueries() + .build(); + } + return INSTANCE; + } + + public static void destroyInstance() { + INSTANCE = null; + } } diff --git a/app/src/main/java/net/foucry/pilldroid/models/Prescription.java b/app/src/main/java/net/foucry/pilldroid/models/Prescription.java index 6d23413..6e3edfc 100644 --- a/app/src/main/java/net/foucry/pilldroid/models/Prescription.java +++ b/app/src/main/java/net/foucry/pilldroid/models/Prescription.java @@ -23,7 +23,8 @@ public class Prescription implements Serializable { private Integer warning; private Integer alert; private Long last_update; - + private String label_group; + private Integer genetic_type; public void setCis(@NonNull String cis) { @@ -113,7 +114,21 @@ public class Prescription implements Serializable { public int getWarnThreshold() { return this.warning; } + public String getLabel_group() { + return label_group; + } + public void setLabel_group(String label_group) { + this.label_group = label_group; + } + + public Integer getGenetic_type() { + return genetic_type; + } + + public void setGenetic_type(Integer genetic_type) { + this.genetic_type = genetic_type; + } public Date getDateEndOfStock() { int numberDayOfTake; if (this.getTake() > 0) {