Create singleton to access to database

This commit is contained in:
jacques 2022-03-27 18:09:19 +02:00
parent d6bd1590e8
commit 7f89c9b5aa
2 changed files with 41 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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;
}
}