mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-09 07:16:04 -04:00
separating room accesor and migration code
This commit is contained in:
parent
1bd468fd58
commit
954c4f5d55
@ -22,11 +22,8 @@ import android.content.Context
|
|||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import org.kiwix.kiwixmobile.core.BuildConfig
|
|
||||||
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
|
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.FdroidDatabaseCallback
|
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.RoomDatabaseCallback
|
|
||||||
|
|
||||||
@Suppress("UnnecessaryAbstractClass")
|
@Suppress("UnnecessaryAbstractClass")
|
||||||
@Database(entities = [RecentSearchRoomEntity::class], version = 1)
|
@Database(entities = [RecentSearchRoomEntity::class], version = 1)
|
||||||
@ -36,16 +33,11 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
|
|||||||
companion object {
|
companion object {
|
||||||
private var db: KiwixRoomDatabase? = null
|
private var db: KiwixRoomDatabase? = null
|
||||||
fun getInstance(context: Context): KiwixRoomDatabase {
|
fun getInstance(context: Context): KiwixRoomDatabase {
|
||||||
val callback =
|
|
||||||
if (BuildConfig.BUILD_TYPE != "fdroid")
|
|
||||||
RoomDatabaseCallback(context)
|
|
||||||
else FdroidDatabaseCallback()
|
|
||||||
return db ?: synchronized(KiwixRoomDatabase::class) {
|
return db ?: synchronized(KiwixRoomDatabase::class) {
|
||||||
return@getInstance db
|
return@getInstance db
|
||||||
?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db")
|
?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db")
|
||||||
// We have already database name called kiwix.db in order to avoid complexity we named as
|
// We have already database name called kiwix.db in order to avoid complexity we named as
|
||||||
// kiwixRoom.db
|
// kiwixRoom.db
|
||||||
.addCallback(callback)
|
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,43 +18,39 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.data.remote
|
package org.kiwix.kiwixmobile.core.data.remote
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
|
||||||
import io.objectbox.Box
|
import io.objectbox.Box
|
||||||
import io.objectbox.BoxStore
|
import io.objectbox.BoxStore
|
||||||
import io.objectbox.kotlin.boxFor
|
import io.objectbox.kotlin.boxFor
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.kiwix.kiwixmobile.core.BuildConfig
|
||||||
import org.kiwix.kiwixmobile.core.CoreApp
|
import org.kiwix.kiwixmobile.core.CoreApp
|
||||||
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity
|
||||||
import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase
|
import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class RoomDatabaseCallback(val context: Context) :
|
class ObjectBoxToRoomMigrator {
|
||||||
RoomDatabase.Callback() {
|
|
||||||
@Inject lateinit var kiwixRoomDatabase: KiwixRoomDatabase
|
@Inject lateinit var kiwixRoomDatabase: KiwixRoomDatabase
|
||||||
@Inject lateinit var boxStore: BoxStore
|
@Inject lateinit var boxStore: BoxStore
|
||||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
|
||||||
super.onCreate(db)
|
init {
|
||||||
CoreApp.coreComponent.inject(this)
|
// Migrate data for non-fdroid variant
|
||||||
boxStore.let(::migrateRecentSearch)
|
if (BuildConfig.BUILD_TYPE != "fdroid") {
|
||||||
|
CoreApp.coreComponent.inject(this)
|
||||||
|
migrateRecentSearch(boxStore.boxFor())
|
||||||
|
// TODO we will migrate here for other entities
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun migrateRecentSearch(boxStore: BoxStore) {
|
private fun migrateRecentSearch(box: Box<RecentSearchEntity>) {
|
||||||
migrationToRoomInsert(boxStore.boxFor())
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun migrationToRoomInsert(
|
|
||||||
box: Box<RecentSearchEntity>
|
|
||||||
) {
|
|
||||||
val searchRoomEntityList = box.all
|
val searchRoomEntityList = box.all
|
||||||
searchRoomEntityList.forEachIndexed { _, recentSearchEntity ->
|
searchRoomEntityList.forEachIndexed { _, recentSearchEntity ->
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
kiwixRoomDatabase.recentSearchRoomDao()
|
kiwixRoomDatabase.recentSearchRoomDao()
|
||||||
.saveSearch(recentSearchEntity.searchTerm, recentSearchEntity.zimId)
|
.saveSearch(recentSearchEntity.searchTerm, recentSearchEntity.zimId)
|
||||||
// Todo Should we remove object store data now?
|
// removing the single entity from the object box after migration.
|
||||||
|
box.remove(recentSearchEntity.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -37,7 +37,7 @@ import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
|
|||||||
import org.kiwix.kiwixmobile.core.data.DataModule
|
import org.kiwix.kiwixmobile.core.data.DataModule
|
||||||
import org.kiwix.kiwixmobile.core.data.DataSource
|
import org.kiwix.kiwixmobile.core.data.DataSource
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.KiwixService
|
import org.kiwix.kiwixmobile.core.data.remote.KiwixService
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.RoomDatabaseCallback
|
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
|
||||||
import org.kiwix.kiwixmobile.core.di.modules.ApplicationModule
|
import org.kiwix.kiwixmobile.core.di.modules.ApplicationModule
|
||||||
import org.kiwix.kiwixmobile.core.di.modules.CoreViewModelModule
|
import org.kiwix.kiwixmobile.core.di.modules.CoreViewModelModule
|
||||||
import org.kiwix.kiwixmobile.core.di.modules.JNIModule
|
import org.kiwix.kiwixmobile.core.di.modules.JNIModule
|
||||||
@ -94,6 +94,7 @@ interface CoreComponent {
|
|||||||
fun newBookmarksDao(): NewBookmarksDao
|
fun newBookmarksDao(): NewBookmarksDao
|
||||||
fun connectivityManager(): ConnectivityManager
|
fun connectivityManager(): ConnectivityManager
|
||||||
fun recentSearchRoomDao(): RecentSearchRoomDao
|
fun recentSearchRoomDao(): RecentSearchRoomDao
|
||||||
|
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
|
||||||
|
|
||||||
fun context(): Context
|
fun context(): Context
|
||||||
fun downloader(): Downloader
|
fun downloader(): Downloader
|
||||||
@ -106,7 +107,7 @@ interface CoreComponent {
|
|||||||
|
|
||||||
fun inject(errorActivity: ErrorActivity)
|
fun inject(errorActivity: ErrorActivity)
|
||||||
fun inject(searchFragment: SearchFragment)
|
fun inject(searchFragment: SearchFragment)
|
||||||
fun inject(roomDatabaseCallback: RoomDatabaseCallback)
|
fun inject(objectBoxToRoomMigrator: ObjectBoxToRoomMigrator)
|
||||||
|
|
||||||
fun inject(settingsFragment: CoreSettingsFragment)
|
fun inject(settingsFragment: CoreSettingsFragment)
|
||||||
fun coreServiceComponent(): CoreServiceComponent.Builder
|
fun coreServiceComponent(): CoreServiceComponent.Builder
|
||||||
|
@ -29,6 +29,7 @@ import io.reactivex.Scheduler
|
|||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import org.kiwix.kiwixmobile.core.NightModeConfig
|
import org.kiwix.kiwixmobile.core.NightModeConfig
|
||||||
|
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
|
||||||
import org.kiwix.kiwixmobile.core.di.qualifiers.Computation
|
import org.kiwix.kiwixmobile.core.di.qualifiers.Computation
|
||||||
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
|
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
|
||||||
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
|
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
|
||||||
@ -65,6 +66,10 @@ class ApplicationModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
internal fun provideBookUtils(): BookUtils = BookUtils()
|
internal fun provideBookUtils(): BookUtils = BookUtils()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideObjectBoxToRoomMigrator() = ObjectBoxToRoomMigrator()
|
||||||
|
|
||||||
@IO
|
@IO
|
||||||
@Provides
|
@Provides
|
||||||
fun provideIoThread(): Scheduler = Schedulers.io()
|
fun provideIoThread(): Scheduler = Schedulers.io()
|
||||||
|
@ -40,6 +40,7 @@ import org.kiwix.kiwixmobile.core.BuildConfig
|
|||||||
import org.kiwix.kiwixmobile.core.R
|
import org.kiwix.kiwixmobile.core.R
|
||||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||||
|
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
|
||||||
import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent
|
import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent
|
||||||
import org.kiwix.kiwixmobile.core.error.ErrorActivity
|
import org.kiwix.kiwixmobile.core.error.ErrorActivity
|
||||||
import org.kiwix.kiwixmobile.core.extensions.browserIntent
|
import org.kiwix.kiwixmobile.core.extensions.browserIntent
|
||||||
@ -78,6 +79,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
|||||||
abstract val cachedComponent: CoreActivityComponent
|
abstract val cachedComponent: CoreActivityComponent
|
||||||
abstract val topLevelDestinations: Set<Int>
|
abstract val topLevelDestinations: Set<Int>
|
||||||
abstract val navHostContainer: FragmentContainerView
|
abstract val navHostContainer: FragmentContainerView
|
||||||
|
@Inject lateinit var objectBoxToRoomMigrator: ObjectBoxToRoomMigrator
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
setTheme(R.style.KiwixTheme)
|
setTheme(R.style.KiwixTheme)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user