mirror of
https://github.com/jfoucry/Pilldroid.git
synced 2024-11-09 23:41:08 +01:00
Create singleton to access to database
This commit is contained in:
parent
d6bd1590e8
commit
7f89c9b5aa
2 changed files with 41 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue