From b49200d891daa700dc1a6a7cd2682c5f27057cac Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Sat, 31 Dec 2022 19:50:50 +0530 Subject: [PATCH] LanguageDao migration complted --- .../language/viewmodel/LanguageViewModel.kt | 3 +- .../viewmodel/SaveLanguagesAndFinish.kt | 3 +- .../zimManager/ZimManageViewModel.kt | 8 +- buildSrc/src/main/kotlin/Libs.kt | 2 + .../kotlin/plugin/AllProjectConfigurer.kt | 1 + .../kiwixmobile/core/dao/LanguageRoomDao.kt | 77 +++++++++++++++++++ .../core/dao/entities/LanguageRoomEntity.kt | 44 +++++++++++ .../kiwix/kiwixmobile/core/data/Repository.kt | 3 +- .../core/data/local/KiwixRoomDatabase.kt | 12 ++- .../core/di/components/CoreComponent.kt | 3 + .../core/di/modules/DatabaseModule.kt | 4 + 11 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/LanguageRoomDao.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/LanguageRoomEntity.kt diff --git a/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/LanguageViewModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/LanguageViewModel.kt index 40cc8fd96..e93cdcd0e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/LanguageViewModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/LanguageViewModel.kt @@ -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().apply { value = Loading } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/SaveLanguagesAndFinish.kt b/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/SaveLanguagesAndFinish.kt index 33a731d55..9a76e7474 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/SaveLanguagesAndFinish.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/language/viewmodel/SaveLanguagesAndFinish.kt @@ -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, - val languageDao: NewLanguagesDao + val languageDao: LanguageRoomDao ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt index a614c6c1c..bd9a84a06 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/ZimManageViewModel.kt @@ -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({ diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index c1acb6fb1..bdb692cd2 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -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 } diff --git a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt index cc52c1b66..a2bca5efb 100644 --- a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt +++ b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt @@ -203,6 +203,7 @@ class AllProjectConfigurer { implementation(Libs.rxjava) implementation(Libs.preference_ktx) implementation(Libs.roomKtx) + implementation(Libs.roomRxjava2) kapt(Libs.roomCompiler) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LanguageRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LanguageRoomDao.kt new file mode 100644 index 000000000..921be6b4d --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/LanguageRoomDao.kt @@ -0,0 +1,77 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * 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 . + * + */ + +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> + + fun languages(): Flowable> = languageEntityList().map { + it.map(LanguageRoomEntity::toLanguageModel) + } + @Query("DELETE FROM LanguageRoomEntity") + abstract fun deleteLanguages() + + + @Insert + abstract fun insert(languageRoomEntity: LanguageRoomEntity) + + fun insert(languages: List) { + 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") + + } + } +} + + + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/LanguageRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/LanguageRoomEntity.kt new file mode 100644 index 000000000..d7460f307 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/LanguageRoomEntity.kt @@ -0,0 +1,44 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * 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 . + * + */ + +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) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index a4dbc7de4..222b71760 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -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 { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabase.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabase.kt index b654434db..22067f54d 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabase.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabase.kt @@ -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 diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index 626dfb166..5cf11ee4f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -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 diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt index f2a2d94a5..b9d8d0a64 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt @@ -92,4 +92,8 @@ open class DatabaseModule { @Singleton @Provides fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao() + + @Singleton + @Provides + fun provideLanguageRoomDao(db: KiwixRoomDatabase) = db.languageRoomDao() }