From 28a5481872c5b66c07251f732617d698adac6df5 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Wed, 13 Oct 2021 13:44:03 +0200 Subject: [PATCH] Add: Room database (2/5 in replacing SQLite with Room) --- .../com/looker/droidify/database/DatabaseX.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/kotlin/com/looker/droidify/database/DatabaseX.kt diff --git a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt new file mode 100644 index 00000000..ed11c6c2 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt @@ -0,0 +1,41 @@ +package com.looker.droidify.database + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase + +@Database( + entities = [ + Repository::class, + Product::class, + Category::class, + Installed::class, + Lock::class + ], version = 1 +) +abstract class DatabaseX : RoomDatabase() { + // TODO add the DAOs for the tables + + companion object { + @Volatile + private var INSTANCE: DatabaseX? = null + + fun getInstance(context: Context): DatabaseX { + synchronized(this) { + if (INSTANCE == null) { + INSTANCE = Room + .databaseBuilder( + context.applicationContext, + DatabaseX::class.java, + "main_database.db" + ) + .fallbackToDestructiveMigration() + .allowMainThreadQueries() + .build() + } + return INSTANCE!! + } + } + } +} \ No newline at end of file