LanguageDao migration complted

This commit is contained in:
Gouri Panda 2022-12-31 19:50:50 +05:30
parent 7d5b985726
commit b49200d891
11 changed files with 154 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
import org.kiwix.kiwixmobile.language.viewmodel.Action.Filter
@ -35,7 +36,7 @@ import org.kiwix.kiwixmobile.language.viewmodel.State.Saving
import javax.inject.Inject
class LanguageViewModel @Inject constructor(
private val languageDao: NewLanguagesDao
private val languageDao: LanguageRoomDao
) : ViewModel() {
val state = MutableLiveData<State>().apply { value = Loading }

View File

@ -21,12 +21,13 @@ import androidx.appcompat.app.AppCompatActivity
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.core.zim_manager.Language
data class SaveLanguagesAndFinish(
val languages: List<Language>,
val languageDao: NewLanguagesDao
val languageDao: LanguageRoomDao
) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) {

View File

@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.zimManager
import android.app.Application
import android.net.ConnectivityManager
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
@ -35,6 +36,7 @@ import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.StorageObserver
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.core.data.DataSource
@ -80,7 +82,7 @@ import javax.inject.Inject
class ZimManageViewModel @Inject constructor(
private val downloadDao: FetchDownloadDao,
private val bookDao: NewBookDao,
private val languageDao: NewLanguagesDao,
private val languageDao: LanguageRoomDao,
private val storageObserver: StorageObserver,
private val kiwixService: KiwixService,
private val context: Application,
@ -152,7 +154,9 @@ class ZimManageViewModel @Inject constructor(
updateNetworkStates(),
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
fileSelectActions()
)
).also {
Log.d("gouriz", " languages ${languages.toList()}")
}
}
private fun fileSelectActions() = fileSelectActions.subscribe({

View File

@ -423,4 +423,6 @@ object Libs {
const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion
const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion
const val roomRxjava2 = "androidx.room:room-rxjava2:" + Versions.roomVersion
}

View File

@ -203,6 +203,7 @@ class AllProjectConfigurer {
implementation(Libs.rxjava)
implementation(Libs.preference_ktx)
implementation(Libs.roomKtx)
implementation(Libs.roomRxjava2)
kapt(Libs.roomCompiler)
}
}

View File

@ -0,0 +1,77 @@
/*
* Kiwix Android
* Copyright (c) 2022 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.dao
import android.util.Log
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.TypeConverter
import io.reactivex.Flowable
import org.kiwix.kiwixmobile.core.dao.entities.LanguageRoomEntity
import org.kiwix.kiwixmobile.core.zim_manager.Language
import java.util.Locale
@Dao
abstract class LanguageRoomDao {
@Query("SELECT * FROM LanguageRoomEntity")
abstract fun languageEntityList(): Flowable<List<LanguageRoomEntity>>
fun languages(): Flowable<List<Language>> = languageEntityList().map {
it.map(LanguageRoomEntity::toLanguageModel)
}
@Query("DELETE FROM LanguageRoomEntity")
abstract fun deleteLanguages()
@Insert
abstract fun insert(languageRoomEntity: LanguageRoomEntity)
fun insert(languages: List<Language>) {
deleteLanguages()
languages.map {
insert(LanguageRoomEntity(it))
}
}
}
class StringToLocalConverterDao {
@TypeConverter
fun convertToDatabaseValue(entityProperty: Locale?): String {
val foo = entityProperty?.isO3Language ?: Locale.ENGLISH.isO3Language
return foo.also {
Log.d("gouri", "convertToDatabaseValue success")
}
}
@TypeConverter
fun convertToEntityProperty(databaseValue: String?): Locale {
val foo = databaseValue?.let(::Locale) ?: Locale.ENGLISH
return foo.also {
Log.d("gouri", "convertToEntityProperty success")
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Kiwix Android
* Copyright (c) 2022 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.dao.entities
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.kiwix.kiwixmobile.core.zim_manager.Language
import java.util.Locale
@Entity
data class LanguageRoomEntity(
@PrimaryKey(autoGenerate = true)
var id: Long = 0,
val locale: Locale = Locale.ENGLISH,
val active: Boolean = false,
val occurencesOfLanguage: Int = 0
) {
constructor(language: Language) : this(
0,
Locale(language.languageCode),
language.active,
language.occurencesOfLanguage
)
fun toLanguageModel() =
Language(locale, active, occurencesOfLanguage, id)
}

View File

@ -23,6 +23,7 @@ import io.reactivex.Flowable
import io.reactivex.Scheduler
import io.reactivex.Single
import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
@ -56,7 +57,7 @@ class Repository @Inject internal constructor(
private val bookmarksDao: NewBookmarksDao,
private val historyDao: HistoryDao,
private val notesDao: NotesRoomDao,
private val languageDao: NewLanguagesDao,
private val languageDao: LanguageRoomDao,
private val recentSearchDao: NewRecentSearchRoomDao,
private val zimReaderContainer: ZimReaderContainer
) : DataSource {

View File

@ -23,20 +23,30 @@ import android.util.Log
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import io.objectbox.BoxStore
import io.objectbox.kotlin.boxFor
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.StringToLocalConverterDao
import org.kiwix.kiwixmobile.core.dao.entities.LanguageRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
@Suppress("UnnecessaryAbstractClass")
@Database(entities = [RecentSearchRoomEntity::class, NotesRoomEntity::class], version = 1)
@Database(
entities = [RecentSearchRoomEntity::class, NotesRoomEntity::class, LanguageRoomEntity::class],
version = 2
)
@TypeConverters(StringToLocalConverterDao::class)
abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao
abstract fun noteRoomDao(): NotesRoomDao
abstract fun languageRoomDao(): LanguageRoomDao
companion object {
private var db: KiwixRoomDatabase? = null

View File

@ -28,6 +28,7 @@ import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.StorageObserver
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.dao.LanguageRoomDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
@ -89,7 +90,9 @@ interface CoreComponent {
fun fetchDownloadDao(): FetchDownloadDao
fun newBookDao(): NewBookDao
fun historyDao(): HistoryDao
// fun noteDao(): NewNoteDao
fun languageRoomDao(): LanguageRoomDao
fun newLanguagesDao(): NewLanguagesDao
fun recentSearchDao(): NewRecentSearchDao
fun recentSearchRoomDao(): NewRecentSearchRoomDao

View File

@ -92,4 +92,8 @@ open class DatabaseModule {
@Singleton
@Provides
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao()
@Singleton
@Provides
fun provideLanguageRoomDao(db: KiwixRoomDatabase) = db.languageRoomDao()
}