mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-04 03:06:41 -04:00
LanguageDao migration complted
This commit is contained in:
parent
7d5b985726
commit
b49200d891
@ -23,6 +23,7 @@ import androidx.lifecycle.ViewModel
|
|||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.processors.PublishProcessor
|
import io.reactivex.processors.PublishProcessor
|
||||||
import org.kiwix.kiwixmobile.core.base.SideEffect
|
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.dao.NewLanguagesDao
|
||||||
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
|
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
|
||||||
import org.kiwix.kiwixmobile.language.viewmodel.Action.Filter
|
import org.kiwix.kiwixmobile.language.viewmodel.Action.Filter
|
||||||
@ -35,7 +36,7 @@ import org.kiwix.kiwixmobile.language.viewmodel.State.Saving
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LanguageViewModel @Inject constructor(
|
class LanguageViewModel @Inject constructor(
|
||||||
private val languageDao: NewLanguagesDao
|
private val languageDao: LanguageRoomDao
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
val state = MutableLiveData<State>().apply { value = Loading }
|
val state = MutableLiveData<State>().apply { value = Loading }
|
||||||
|
@ -21,12 +21,13 @@ import androidx.appcompat.app.AppCompatActivity
|
|||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import org.kiwix.kiwixmobile.core.base.SideEffect
|
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.dao.NewLanguagesDao
|
||||||
import org.kiwix.kiwixmobile.core.zim_manager.Language
|
import org.kiwix.kiwixmobile.core.zim_manager.Language
|
||||||
|
|
||||||
data class SaveLanguagesAndFinish(
|
data class SaveLanguagesAndFinish(
|
||||||
val languages: List<Language>,
|
val languages: List<Language>,
|
||||||
val languageDao: NewLanguagesDao
|
val languageDao: LanguageRoomDao
|
||||||
) : SideEffect<Unit> {
|
) : SideEffect<Unit> {
|
||||||
|
|
||||||
override fun invokeWith(activity: AppCompatActivity) {
|
override fun invokeWith(activity: AppCompatActivity) {
|
||||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.zimManager
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
|
import android.util.Log
|
||||||
import androidx.annotation.VisibleForTesting
|
import androidx.annotation.VisibleForTesting
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
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.StorageObserver
|
||||||
import org.kiwix.kiwixmobile.core.base.SideEffect
|
import org.kiwix.kiwixmobile.core.base.SideEffect
|
||||||
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
|
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.NewBookDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
||||||
import org.kiwix.kiwixmobile.core.data.DataSource
|
import org.kiwix.kiwixmobile.core.data.DataSource
|
||||||
@ -80,7 +82,7 @@ import javax.inject.Inject
|
|||||||
class ZimManageViewModel @Inject constructor(
|
class ZimManageViewModel @Inject constructor(
|
||||||
private val downloadDao: FetchDownloadDao,
|
private val downloadDao: FetchDownloadDao,
|
||||||
private val bookDao: NewBookDao,
|
private val bookDao: NewBookDao,
|
||||||
private val languageDao: NewLanguagesDao,
|
private val languageDao: LanguageRoomDao,
|
||||||
private val storageObserver: StorageObserver,
|
private val storageObserver: StorageObserver,
|
||||||
private val kiwixService: KiwixService,
|
private val kiwixService: KiwixService,
|
||||||
private val context: Application,
|
private val context: Application,
|
||||||
@ -152,7 +154,9 @@ class ZimManageViewModel @Inject constructor(
|
|||||||
updateNetworkStates(),
|
updateNetworkStates(),
|
||||||
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
|
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
|
||||||
fileSelectActions()
|
fileSelectActions()
|
||||||
)
|
).also {
|
||||||
|
Log.d("gouriz", " languages ${languages.toList()}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fileSelectActions() = fileSelectActions.subscribe({
|
private fun fileSelectActions() = fileSelectActions.subscribe({
|
||||||
|
@ -423,4 +423,6 @@ object Libs {
|
|||||||
const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion
|
const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion
|
||||||
|
|
||||||
const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion
|
const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion
|
||||||
|
|
||||||
|
const val roomRxjava2 = "androidx.room:room-rxjava2:" + Versions.roomVersion
|
||||||
}
|
}
|
||||||
|
@ -203,6 +203,7 @@ class AllProjectConfigurer {
|
|||||||
implementation(Libs.rxjava)
|
implementation(Libs.rxjava)
|
||||||
implementation(Libs.preference_ktx)
|
implementation(Libs.preference_ktx)
|
||||||
implementation(Libs.roomKtx)
|
implementation(Libs.roomKtx)
|
||||||
|
implementation(Libs.roomRxjava2)
|
||||||
kapt(Libs.roomCompiler)
|
kapt(Libs.roomCompiler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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")
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
@ -23,6 +23,7 @@ import io.reactivex.Flowable
|
|||||||
import io.reactivex.Scheduler
|
import io.reactivex.Scheduler
|
||||||
import io.reactivex.Single
|
import io.reactivex.Single
|
||||||
import org.kiwix.kiwixmobile.core.dao.HistoryDao
|
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.NewBookDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
||||||
@ -56,7 +57,7 @@ class Repository @Inject internal constructor(
|
|||||||
private val bookmarksDao: NewBookmarksDao,
|
private val bookmarksDao: NewBookmarksDao,
|
||||||
private val historyDao: HistoryDao,
|
private val historyDao: HistoryDao,
|
||||||
private val notesDao: NotesRoomDao,
|
private val notesDao: NotesRoomDao,
|
||||||
private val languageDao: NewLanguagesDao,
|
private val languageDao: LanguageRoomDao,
|
||||||
private val recentSearchDao: NewRecentSearchRoomDao,
|
private val recentSearchDao: NewRecentSearchRoomDao,
|
||||||
private val zimReaderContainer: ZimReaderContainer
|
private val zimReaderContainer: ZimReaderContainer
|
||||||
) : DataSource {
|
) : DataSource {
|
||||||
|
@ -23,20 +23,30 @@ import android.util.Log
|
|||||||
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 androidx.room.TypeConverter
|
||||||
|
import androidx.room.TypeConverters
|
||||||
import androidx.room.migration.Migration
|
import androidx.room.migration.Migration
|
||||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
import io.objectbox.BoxStore
|
import io.objectbox.BoxStore
|
||||||
import io.objectbox.kotlin.boxFor
|
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.NewRecentSearchRoomDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
|
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.NotesRoomEntity
|
||||||
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
|
||||||
|
|
||||||
@Suppress("UnnecessaryAbstractClass")
|
@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 class KiwixRoomDatabase : RoomDatabase() {
|
||||||
abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao
|
abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao
|
||||||
abstract fun noteRoomDao(): NotesRoomDao
|
abstract fun noteRoomDao(): NotesRoomDao
|
||||||
|
abstract fun languageRoomDao(): LanguageRoomDao
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private var db: KiwixRoomDatabase? = null
|
private var db: KiwixRoomDatabase? = null
|
||||||
|
@ -28,6 +28,7 @@ import org.kiwix.kiwixmobile.core.CoreApp
|
|||||||
import org.kiwix.kiwixmobile.core.StorageObserver
|
import org.kiwix.kiwixmobile.core.StorageObserver
|
||||||
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
|
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.HistoryDao
|
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.NewBookDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
|
||||||
@ -89,7 +90,9 @@ interface CoreComponent {
|
|||||||
fun fetchDownloadDao(): FetchDownloadDao
|
fun fetchDownloadDao(): FetchDownloadDao
|
||||||
fun newBookDao(): NewBookDao
|
fun newBookDao(): NewBookDao
|
||||||
fun historyDao(): HistoryDao
|
fun historyDao(): HistoryDao
|
||||||
|
|
||||||
// fun noteDao(): NewNoteDao
|
// fun noteDao(): NewNoteDao
|
||||||
|
fun languageRoomDao(): LanguageRoomDao
|
||||||
fun newLanguagesDao(): NewLanguagesDao
|
fun newLanguagesDao(): NewLanguagesDao
|
||||||
fun recentSearchDao(): NewRecentSearchDao
|
fun recentSearchDao(): NewRecentSearchDao
|
||||||
fun recentSearchRoomDao(): NewRecentSearchRoomDao
|
fun recentSearchRoomDao(): NewRecentSearchRoomDao
|
||||||
|
@ -92,4 +92,8 @@ open class DatabaseModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao()
|
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao()
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideLanguageRoomDao(db: KiwixRoomDatabase) = db.languageRoomDao()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user