[Android] Refactor Mediadatabase with SingletonHolder
Geoffrey Métais
git at videolan.org
Thu Aug 9 10:47:52 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug 9 10:47:20 2018 +0200| [d77bf181c0839445e0d625beb86b9dd8970cb61f] | committer: Geoffrey Métais
Refactor Mediadatabase with SingletonHolder
> https://code.videolan.org/videolan/vlc-android/commit/d77bf181c0839445e0d625beb86b9dd8970cb61f
---
.../src/org/videolan/vlc/database/MediaDatabase.kt | 44 +++++++++-------------
.../vlc/repository/BrowserFavRepository.kt | 2 +-
.../vlc/repository/ExternalSubRepository.kt | 2 +-
.../org/videolan/vlc/repository/SlaveRepository.kt | 2 +-
4 files changed, 21 insertions(+), 29 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/database/MediaDatabase.kt b/vlc-android/src/org/videolan/vlc/database/MediaDatabase.kt
index 62e8084f1..58cb16ecd 100644
--- a/vlc-android/src/org/videolan/vlc/database/MediaDatabase.kt
+++ b/vlc-android/src/org/videolan/vlc/database/MediaDatabase.kt
@@ -26,10 +26,13 @@ import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.arch.persistence.room.TypeConverters
import android.content.Context
+import org.videolan.tools.SingletonHolder
import org.videolan.vlc.database.models.BrowserFav
import org.videolan.vlc.database.models.ExternalSub
import org.videolan.vlc.database.models.Slave
+private const val DB_NAME = "vlc_database"
+
@Database(entities = [ExternalSub::class, Slave::class, BrowserFav::class], version = 27)
@TypeConverters(Converters::class)
abstract class MediaDatabase: RoomDatabase() {
@@ -37,31 +40,20 @@ abstract class MediaDatabase: RoomDatabase() {
abstract fun slaveDao(): SlaveDao
abstract fun browserFavDao(): BrowserFavDao
- companion object {
- private const val DB_NAME = "vlc_database"
-
- @Volatile private var instance: MediaDatabase? = null
- fun getDatabase(context: Context): MediaDatabase =
- instance ?: synchronized(this) {
- instance ?: buildDatabase(context).also { instance = it }
- }
-
- private fun buildDatabase(context: Context) =
- Room.databaseBuilder(context.applicationContext,
- MediaDatabase::class.java, DB_NAME)
- .addMigrations(migration_1_2, migration_2_3, migration_3_4, migration_4_5,
- migration_5_6, migration_6_7, migration_7_8, migration_8_9,
- migration_9_10, migration_10_11, migration_11_12, migration_12_13,
- migration_13_14, migration_14_15, migration_15_16, migration_16_17,
- migration_17_18, migration_18_19, migration_19_20, migration_20_21,
- migration_21_22, migration_22_23, migration_23_24, migration_24_25,
- migration_25_26, migration_26_27)
- .addCallback(object : RoomDatabase.Callback() {
- override fun onCreate(db: SupportSQLiteDatabase) {
- populateDB(context)
- }
- })
- .build()
- }
+ companion object : SingletonHolder<MediaDatabase, Context>({ buildDatabase(it.applicationContext) })
}
+private fun buildDatabase(context: Context) = Room.databaseBuilder(context.applicationContext,
+ MediaDatabase::class.java, DB_NAME)
+ .addMigrations(migration_1_2, migration_2_3, migration_3_4, migration_4_5,
+ migration_5_6, migration_6_7, migration_7_8, migration_8_9,
+ migration_9_10, migration_10_11, migration_11_12, migration_12_13,
+ migration_13_14, migration_14_15, migration_15_16, migration_16_17,
+ migration_17_18, migration_18_19, migration_19_20, migration_20_21,
+ migration_21_22, migration_22_23, migration_23_24, migration_24_25,
+ migration_25_26, migration_26_27)
+ .addCallback(object : RoomDatabase.Callback() {
+ override fun onCreate(db: SupportSQLiteDatabase) { populateDB(context) }
+ })
+ .build()
+
diff --git a/vlc-android/src/org/videolan/vlc/repository/BrowserFavRepository.kt b/vlc-android/src/org/videolan/vlc/repository/BrowserFavRepository.kt
index 8451a111c..f4a2fe2b7 100644
--- a/vlc-android/src/org/videolan/vlc/repository/BrowserFavRepository.kt
+++ b/vlc-android/src/org/videolan/vlc/repository/BrowserFavRepository.kt
@@ -85,5 +85,5 @@ class BrowserFavRepository(private val browserFavDao: BrowserFavDao) {
}
}
- companion object : SingletonHolder<BrowserFavRepository, Context>({ BrowserFavRepository(MediaDatabase.getDatabase(it.applicationContext).browserFavDao()) })
+ companion object : SingletonHolder<BrowserFavRepository, Context>({ BrowserFavRepository(MediaDatabase.getInstance(it).browserFavDao()) })
}
diff --git a/vlc-android/src/org/videolan/vlc/repository/ExternalSubRepository.kt b/vlc-android/src/org/videolan/vlc/repository/ExternalSubRepository.kt
index 36e6603f3..3820172dd 100644
--- a/vlc-android/src/org/videolan/vlc/repository/ExternalSubRepository.kt
+++ b/vlc-android/src/org/videolan/vlc/repository/ExternalSubRepository.kt
@@ -47,5 +47,5 @@ class ExternalSubRepository(private val externalSubDao: ExternalSubDao ) {
return existExternalSubs
}
- companion object : SingletonHolder<ExternalSubRepository, Context>({ ExternalSubRepository(MediaDatabase.getDatabase(it.applicationContext).externalSubDao()) })
+ companion object : SingletonHolder<ExternalSubRepository, Context>({ ExternalSubRepository(MediaDatabase.getInstance(it).externalSubDao()) })
}
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/repository/SlaveRepository.kt b/vlc-android/src/org/videolan/vlc/repository/SlaveRepository.kt
index 62f7004b3..42ad101f4 100644
--- a/vlc-android/src/org/videolan/vlc/repository/SlaveRepository.kt
+++ b/vlc-android/src/org/videolan/vlc/repository/SlaveRepository.kt
@@ -63,5 +63,5 @@ class SlaveRepository(private val slaveDao:SlaveDao){
}
}
- companion object : SingletonHolder<SlaveRepository, Context>({ SlaveRepository(MediaDatabase.getDatabase(it.applicationContext).slaveDao()) })
+ companion object : SingletonHolder<SlaveRepository, Context>({ SlaveRepository(MediaDatabase.getInstance(it).slaveDao()) })
}
More information about the Android
mailing list