From 66d17844aa72eeb7a0dfd1254e13366a542a649b Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Wed, 28 Dec 2022 16:51:44 +0530 Subject: [PATCH] runtime added maybe worked or not :( --- buildSrc/src/main/kotlin/Libs.kt | 1 + .../kotlin/plugin/AllProjectConfigurer.kt | 3 + .../kotlin/plugin/ConvenienceExtensions.kt | 6 ++ .../kiwixmobile/core/dao/HistoryRoomDao.kt | 94 +++++++++++++++++++ .../org/kiwix/kiwixmobile/core/dao/PageDao.kt | 14 ++- .../core/dao/entities/HistroryRoomEntity.kt | 48 ++++++++++ .../kiwix/kiwixmobile/core/data/DataModule.kt | 3 + .../kiwix/kiwixmobile/core/data/Repository.kt | 10 +- .../core/data/local/KiwixRoomDatabase.kt | 31 +++++- .../core/di/components/CoreComponent.kt | 4 + .../core/di/modules/ApplicationModule.kt | 13 +++ .../core/di/modules/DatabaseModule.kt | 26 +++-- .../bookmark/viewmodel/BookmarkViewModel.kt | 2 +- .../effects/ShowDeleteBookmarksDialog.kt | 6 +- .../page/history/adapter/HistoryListItem.kt | 85 ++++++++++++++++- .../history/viewmodel/HistoryViewModel.kt | 8 +- .../effects/ShowDeleteHistoryDialog.kt | 6 +- .../page/notes/viewmodel/NotesViewModel.kt | 2 +- .../effects/ShowDeleteNotesDialog.kt | 6 +- .../core/page/viewmodel/PageViewModel.kt | 31 ++++-- .../page/viewmodel/effects/DeletePageItems.kt | 8 +- 21 files changed, 363 insertions(+), 44 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistroryRoomEntity.kt diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index c1acb6fb1..28f7c1199 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -423,4 +423,5 @@ object Libs { const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion + const val roomRuntime = "androidx.room:room-runtime:" + Versions.roomVersion } diff --git a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt index cc52c1b66..3177cc8ff 100644 --- a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt +++ b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt @@ -203,7 +203,10 @@ class AllProjectConfigurer { implementation(Libs.rxjava) implementation(Libs.preference_ktx) implementation(Libs.roomKtx) + annotationProcessor(Libs.roomCompiler) + implementation(Libs.roomRuntime) kapt(Libs.roomCompiler) + // ksp(Libs.roomCompiler) } } } diff --git a/buildSrc/src/main/kotlin/plugin/ConvenienceExtensions.kt b/buildSrc/src/main/kotlin/plugin/ConvenienceExtensions.kt index 33b2f7fb4..9d085bbcd 100644 --- a/buildSrc/src/main/kotlin/plugin/ConvenienceExtensions.kt +++ b/buildSrc/src/main/kotlin/plugin/ConvenienceExtensions.kt @@ -54,5 +54,11 @@ internal fun DependencyHandlerScope.testImplementation(dependency: String) = internal fun DependencyHandlerScope.implementation(dependency: String) = addDependency("implementation", dependency) +internal fun DependencyHandlerScope.annotationProcessor(dependency: String) = + addDependency("annotationProcessor", dependency) + +internal fun DependencyHandlerScope.ksp(dependency: String) = + addDependency("ksp", dependency) + private fun DependencyHandlerScope.addDependency(configurationName: String, dependency: String) = add(configurationName, dependency) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt new file mode 100644 index 000000000..00510b702 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt @@ -0,0 +1,94 @@ +/* + * 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 androidx.lifecycle.Transformations.map +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.ProvidedTypeConverter +import androidx.room.Query +import androidx.room.TypeConverter +import androidx.room.Update +import io.objectbox.Box +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch +import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity +import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem + +@Dao +abstract class HistoryRoomDao : BasePageDao { + @Query("SELECT * FROM HistoryRoomEntity ORDER BY HistoryRoomEntity.timeStamp DESC") + abstract fun historyRoomEntity(): Flow> + + fun history(): Flow> = historyRoomEntity().map { + it.map(HistoryListItem::HistoryItem) + } + + // // TODO: After refactoring all the database we should implement [PageDao] + override fun pages() = history() + override fun deletePages(pagesToDelete: List) = + deleteHistory(pagesToDelete as List) + + @Query("SELECT * FROM HistoryRoomEntity WHERE historyUrl LIKE :url AND dateString LIKE :date") + abstract fun getHistoryItem(url: String, date: String): HistoryListItem.HistoryItem + + fun getHistoryItem(historyItem: HistoryListItem.HistoryItem): HistoryListItem.HistoryItem = + getHistoryItem(historyItem.historyUrl, historyItem.dateString) + + @Update + abstract fun updateHistoryItem(historyItem: HistoryListItem.HistoryItem) + + fun saveHistory(historyItem: HistoryListItem.HistoryItem) { + val item = getHistoryItem(historyItem) + updateHistoryItem(item) + } + + @Delete + abstract fun deleteHistory(historyList: List) + + @Query("DELETE FROM HistoryRoomEntity") + abstract fun deleteAllHistory() + + fun migrationToRoomHistory( + box: Box + ) { + val historyEntityList = box.all + historyEntityList.forEachIndexed { _, item -> + CoroutineScope(Dispatchers.IO).launch { + // saveHistory(HistoryListItem.HistoryItem(item)) + // Todo Should we remove object store data now? + } + } + } +} + +class HistoryRoomDaoCoverts { + @TypeConverter + fun fromHistoryRoomEntity(historyRoomEntity: HistoryRoomEntity): HistoryListItem = + HistoryListItem.HistoryItem(historyRoomEntity) + + @TypeConverter + fun historyItemToHistoryListItem(historyItem: HistoryListItem.HistoryItem): HistoryRoomEntity = + HistoryRoomEntity(historyItem) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/PageDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/PageDao.kt index ac6091666..dc80bf5f0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/PageDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/PageDao.kt @@ -19,9 +19,19 @@ package org.kiwix.kiwixmobile.core.dao import io.reactivex.Flowable +import kotlinx.coroutines.flow.Flow import org.kiwix.kiwixmobile.core.page.adapter.Page -interface PageDao { - fun pages(): Flowable> +interface PageDao : BasePageDao { + override fun pages(): Flowable> +} + +interface PageRoomDao : BasePageDao { + override fun pages(): Flow> +} + +interface BasePageDao { + fun pages(): Any fun deletePages(pagesToDelete: List) } + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistroryRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistroryRoomEntity.kt new file mode 100644 index 000000000..63b4b9bf7 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistroryRoomEntity.kt @@ -0,0 +1,48 @@ +/* + * 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.page.history.adapter.HistoryListItem + +@Entity +data class HistoryRoomEntity( + @PrimaryKey var id: Long = 0L, + val zimId: String, + val zimName: String, + val zimFilePath: String, + val favicon: String?, + val historyUrl: String, + val historyTitle: String, + val dateString: String, + val timeStamp: Long +) { + constructor(historyItem: HistoryListItem.HistoryItem) : this( + historyItem.databaseId, + historyItem.zimId, + historyItem.zimName, + historyItem.zimFilePath, + historyItem.favicon, + historyItem.historyUrl, + historyItem.title, + historyItem.dateString, + historyItem.timeStamp + ) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataModule.kt index b3eba3420..9f241aaa8 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataModule.kt @@ -17,8 +17,11 @@ */ package org.kiwix.kiwixmobile.core.data +import android.content.Context import dagger.Module import dagger.Provides +import io.objectbox.BoxStore +import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase import javax.inject.Singleton @Module 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 3c1fd31eb..480a9a5aa 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 @@ -22,7 +22,7 @@ import io.reactivex.Completable 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.HistoryRoomDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao @@ -54,7 +54,7 @@ class Repository @Inject internal constructor( @param:MainThread private val mainThread: Scheduler, private val bookDao: NewBookDao, private val bookmarksDao: NewBookmarksDao, - private val historyDao: HistoryDao, + private val historyRoomDao: HistoryRoomDao, private val notesDao: NewNoteDao, private val languageDao: NewLanguagesDao, private val recentSearchDao: NewRecentSearchRoomDao, @@ -90,17 +90,17 @@ class Repository @Inject internal constructor( .subscribeOn(io) override fun saveHistory(history: HistoryItem) = - Completable.fromAction { historyDao.saveHistory(history) } + Completable.fromAction { historyRoomDao.saveHistory(history) } .subscribeOn(io) override fun deleteHistory(historyList: List) = Completable.fromAction { - historyDao.deleteHistory(historyList.filterIsInstance(HistoryItem::class.java)) + historyRoomDao.deleteHistory(historyList.filterIsInstance(HistoryItem::class.java)) } .subscribeOn(io) override fun clearHistory() = Completable.fromAction { - historyDao.deleteAllHistory() + historyRoomDao.deleteAllHistory() recentSearchDao.deleteSearchHistory() }.subscribeOn(io) 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 675ec4733..7614f25de 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 @@ -19,29 +19,46 @@ package org.kiwix.kiwixmobile.core.data.local import android.content.Context +import android.util.Log import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +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.HistoryRoomDao +import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao +import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity +import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem @Suppress("UnnecessaryAbstractClass") -@Database(entities = [RecentSearchRoomEntity::class], version = 1) +@Database( + entities = [RecentSearchRoomEntity::class, HistoryRoomEntity::class], + version = 2 +) +@TypeConverters(HistoryRoomDaoCoverts::class) abstract class KiwixRoomDatabase : RoomDatabase() { abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao + abstract fun historyRoomDao(): HistoryRoomDao companion object { private var db: KiwixRoomDatabase? = null + private var box: BoxStore? = null fun getInstance(context: Context, boxStore: BoxStore): KiwixRoomDatabase { + box = boxStore return db ?: synchronized(KiwixRoomDatabase::class) { return@getInstance 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 // kiwixRoom.db + // .addMigrations(MIGRATION_1_2) .build().also { it.migrateRecentSearch(boxStore) + it.migrateHistory(boxStore) } } } @@ -49,9 +66,21 @@ abstract class KiwixRoomDatabase : RoomDatabase() { fun destroyInstance() { db = null } + + val MIGRATION_1_2 = object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + db!!::migrateHistory.let { box?.let(it) } ?: run { + Log.d("gouri", "box store is null") + } + } + } } fun migrateRecentSearch(boxStore: BoxStore) { newRecentSearchRoomDao().migrationToRoomInsert(boxStore.boxFor()) } + + fun migrateHistory(boxStore: BoxStore) { + historyRoomDao().migrationToRoomHistory(boxStore.boxFor()) + } } 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 91c4876c9..ee907586c 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.HistoryRoomDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao @@ -36,6 +37,7 @@ import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao import org.kiwix.kiwixmobile.core.data.DataModule import org.kiwix.kiwixmobile.core.data.DataSource +import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase import org.kiwix.kiwixmobile.core.data.local.dao.BookDao import org.kiwix.kiwixmobile.core.data.local.dao.BookmarksDao import org.kiwix.kiwixmobile.core.data.remote.KiwixService @@ -77,6 +79,7 @@ interface CoreComponent { fun build(): CoreComponent } + fun kiwixRoomDataBase(): KiwixRoomDatabase fun activityComponentBuilder(): CoreActivityComponent.Builder fun zimReaderContainer(): ZimReaderContainer fun sharedPrefUtil(): SharedPreferenceUtil @@ -93,6 +96,7 @@ interface CoreComponent { fun newLanguagesDao(): NewLanguagesDao fun recentSearchDao(): NewRecentSearchDao fun recentSearchRoomDao(): NewRecentSearchRoomDao + fun historyRoomDao(): HistoryRoomDao fun newBookmarksDao(): NewBookmarksDao fun connectivityManager(): ConnectivityManager fun context(): Context diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt index cf5460579..67d2297c7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt @@ -25,10 +25,12 @@ import android.net.ConnectivityManager import android.os.storage.StorageManager import dagger.Module import dagger.Provides +import io.objectbox.BoxStore import io.reactivex.Scheduler import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.NightModeConfig +import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase import org.kiwix.kiwixmobile.core.di.qualifiers.Computation import org.kiwix.kiwixmobile.core.di.qualifiers.IO import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread @@ -96,4 +98,15 @@ class ApplicationModule { @Singleton fun provideConnectivityManager(context: Context): ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + + @Singleton + @Provides + fun provideYourDatabase( + context: Context, + boxStore: BoxStore + ) = + KiwixRoomDatabase.getInstance( + context = context, + boxStore + ) } 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 3ce6ef0ab..9210b2c73 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 @@ -48,6 +48,8 @@ open class DatabaseModule { return boxStore!! } + // The reason we can construct a database for the repo + @Provides @Singleton fun providesNewBookDao(boxStore: BoxStore): NewBookDao = NewBookDao(boxStore.boxFor()) @@ -74,18 +76,22 @@ open class DatabaseModule { ): FetchDownloadDao = FetchDownloadDao(boxStore.boxFor(), newBookDao) - @Singleton - @Provides - fun provideYourDatabase( - context: Context, - boxStore: BoxStore - ) = - KiwixRoomDatabase.getInstance( - context = context, - boxStore - ) // The reason we can construct a database for the repo + // @Singleton + // @Provides + // fun provideYourDatabase( + // context: Context, + // boxStore: BoxStore + // ) = + // KiwixRoomDatabase.getInstance( + // context = context, + // boxStore + // ) // The reason we can construct a database for the repo @Singleton @Provides fun provideNewRecentSearchRoomDao(db: KiwixRoomDatabase) = db.newRecentSearchRoomDao() + + @Provides + @Singleton + fun provideHistoryDao(db: KiwixRoomDatabase) = db.historyRoomDao() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt index ebdee2849..f79f3117c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt @@ -61,7 +61,7 @@ class BookmarkViewModel @Inject constructor( state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) override fun createDeletePageDialogEffect(state: BookmarkState) = - ShowDeleteBookmarksDialog(effects, state, pageDao) + ShowDeleteBookmarksDialog(effects, state, basePageDao) override fun copyWithNewItems(state: BookmarkState, newItems: List): BookmarkState = state.copy(pageItems = newItems) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/effects/ShowDeleteBookmarksDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/effects/ShowDeleteBookmarksDialog.kt index 68591834d..5c37715e5 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/effects/ShowDeleteBookmarksDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/effects/ShowDeleteBookmarksDialog.kt @@ -21,7 +21,7 @@ package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects import androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.dao.BasePageDao import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.viewmodel.PageState @@ -34,14 +34,14 @@ import javax.inject.Inject data class ShowDeleteBookmarksDialog( private val effects: PublishProcessor>, private val state: PageState, - private val pageDao: PageDao + private val basePageDao: BasePageDao ) : SideEffect { @Inject lateinit var dialogShower: DialogShower override fun invokeWith(activity: AppCompatActivity) { activity.cachedComponent.inject(this) dialogShower.show( if (state.isInSelectionState) DeleteSelectedBookmarks else DeleteAllBookmarks, - { effects.offer(DeletePageItems(state, pageDao)) } + { effects.offer(DeletePageItems(state, basePageDao)) } ) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt index eee4fca52..f242d74f6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt @@ -17,13 +17,15 @@ */ package org.kiwix.kiwixmobile.core.page.history.adapter +import androidx.room.Entity +import androidx.room.PrimaryKey import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity +import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.adapter.PageRelated import org.kiwix.kiwixmobile.core.reader.ZimFileReader sealed class HistoryListItem : PageRelated { - data class HistoryItem constructor( val databaseId: Long = 0L, override val zimId: String, @@ -68,6 +70,19 @@ sealed class HistoryListItem : PageRelated { historyEntity.timeStamp, false ) + + constructor(historyRoomEntity: HistoryRoomEntity) : this( + historyRoomEntity.id, + historyRoomEntity.zimId, + historyRoomEntity.zimName, + historyRoomEntity.zimFilePath, + historyRoomEntity.favicon, + historyRoomEntity.historyUrl, + historyRoomEntity.historyTitle, + historyRoomEntity.dateString, + historyRoomEntity.timeStamp, + false + ) } data class DateItem( @@ -75,3 +90,71 @@ sealed class HistoryListItem : PageRelated { override val id: Long = dateString.hashCode().toLong() ) : HistoryListItem() } + +// @Entity +// sealed class HistoryListRoomItem : PageRelated { +// @Entity +// data class HistoryItem constructor( +// val databaseId: Long = 0L, +// override val zimId: String, +// val zimName: String, +// override val zimFilePath: String, +// override val favicon: String?, +// val historyUrl: String, +// override val title: String, +// val dateString: String, +// val timeStamp: Long, +// override var isSelected: Boolean = false, +// override val id: Long = databaseId, +// override val url: String = historyUrl +// ) : HistoryListItem(), Page { +// +// constructor( +// url: String, +// title: String, +// dateString: String, +// timeStamp: Long, +// zimFileReader: ZimFileReader +// ) : this( +// zimId = zimFileReader.id, +// zimName = zimFileReader.name, +// zimFilePath = zimFileReader.zimFile.canonicalPath, +// favicon = zimFileReader.favicon, +// historyUrl = url, +// title = title, +// dateString = dateString, +// timeStamp = timeStamp +// ) +// +// constructor(historyEntity: HistoryEntity) : this( +// historyEntity.id, +// historyEntity.zimId, +// historyEntity.zimName, +// historyEntity.zimFilePath, +// historyEntity.favicon, +// historyEntity.historyUrl, +// historyEntity.historyTitle, +// historyEntity.dateString, +// historyEntity.timeStamp, +// false +// ) +// +// constructor(historyRoomEntity: HistoryRoomEntity) : this( +// historyRoomEntity.id, +// historyRoomEntity.zimId, +// historyRoomEntity.zimName, +// historyRoomEntity.zimFilePath, +// historyRoomEntity.favicon, +// historyRoomEntity.historyUrl, +// historyRoomEntity.historyTitle, +// historyRoomEntity.dateString, +// historyRoomEntity.timeStamp, +// false +// ) +// } +// +// data class DateItem( +// val dateString: String, +// override val id: Long = dateString.hashCode().toLong() +// ) : HistoryListItem() +// } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt index d7ca9a5c3..cb201e76c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt @@ -18,7 +18,7 @@ package org.kiwix.kiwixmobile.core.page.history.viewmodel -import org.kiwix.kiwixmobile.core.dao.HistoryDao +import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.UpdateAllHistoryPreference @@ -29,10 +29,10 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import javax.inject.Inject class HistoryViewModel @Inject constructor( - historyDao: HistoryDao, + historyRoomDao: HistoryRoomDao, zimReaderContainer: ZimReaderContainer, sharedPrefs: SharedPreferenceUtil -) : PageViewModel(historyDao, sharedPrefs, zimReaderContainer) { +) : PageViewModel(historyRoomDao, sharedPrefs, zimReaderContainer) { override fun initialState(): HistoryState = HistoryState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) @@ -58,7 +58,7 @@ class HistoryViewModel @Inject constructor( } override fun createDeletePageDialogEffect(state: HistoryState) = - ShowDeleteHistoryDialog(effects, state, pageDao) + ShowDeleteHistoryDialog(effects, state, basePageDao) override fun deselectAllPages(state: HistoryState): HistoryState = state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/effects/ShowDeleteHistoryDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/effects/ShowDeleteHistoryDialog.kt index 9d833fd6e..7874ab1c9 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/effects/ShowDeleteHistoryDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/effects/ShowDeleteHistoryDialog.kt @@ -21,7 +21,7 @@ package org.kiwix.kiwixmobile.core.page.history.viewmodel.effects import androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.dao.BasePageDao import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems @@ -33,13 +33,13 @@ import javax.inject.Inject data class ShowDeleteHistoryDialog( private val effects: PublishProcessor>, private val state: HistoryState, - private val pageDao: PageDao + private val basePageDao: BasePageDao ) : SideEffect { @Inject lateinit var dialogShower: DialogShower override fun invokeWith(activity: AppCompatActivity) { activity.cachedComponent.inject(this) dialogShower.show(if (state.isInSelectionState) DeleteSelectedHistory else DeleteAllHistory, { - effects.offer(DeletePageItems(state, pageDao)) + effects.offer(DeletePageItems(state, basePageDao)) }) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt index ad7aa7462..d06ff606b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt @@ -66,7 +66,7 @@ class NotesViewModel @Inject constructor( state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) override fun createDeletePageDialogEffect(state: NotesState) = - ShowDeleteNotesDialog(effects, state, pageDao) + ShowDeleteNotesDialog(effects, state, basePageDao) override fun onItemClick(page: Page) = ShowOpenNoteDialog(effects, page, zimReaderContainer) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt index 957c4ded2..dbc212ecd 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt @@ -22,7 +22,7 @@ import android.util.Log import androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.dao.BasePageDao import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesState import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems @@ -34,7 +34,7 @@ import javax.inject.Inject data class ShowDeleteNotesDialog( private val effects: PublishProcessor>, private val state: NotesState, - private val pageDao: PageDao + private val basePageDao: BasePageDao ) : SideEffect { @Inject lateinit var dialogShower: DialogShower override fun invokeWith(activity: AppCompatActivity) { @@ -43,7 +43,7 @@ data class ShowDeleteNotesDialog( dialogShower.show( if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes, { - effects.offer(DeletePageItems(state, pageDao)) + effects.offer(DeletePageItems(state, basePageDao)) } ) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt index edda2f164..f6fed3dae 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt @@ -20,12 +20,17 @@ package org.kiwix.kiwixmobile.core.page.viewmodel import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable import io.reactivex.processors.PublishProcessor import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.dao.BasePageDao import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.dao.PageRoomDao import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu @@ -42,7 +47,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil abstract class PageViewModel>( - protected val pageDao: PageDao, + protected val basePageDao: BasePageDao, val sharedPreferenceUtil: SharedPreferenceUtil, val zimReaderContainer: ZimReaderContainer ) : ViewModel() { @@ -70,11 +75,25 @@ abstract class PageViewModel>( .subscribe(state::postValue, Throwable::printStackTrace) protected fun addDisposablesToCompositeDisposable() { - compositeDisposable.addAll( - viewStateReducer(), - pageDao.pages().subscribeOn(Schedulers.io()) - .subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace) - ) + when (basePageDao) { + is PageDao -> { + compositeDisposable.addAll( + viewStateReducer(), + basePageDao.pages().subscribeOn(Schedulers.io()) + .subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace) + ) + } + is PageRoomDao -> { + viewModelScope.launch { + try { + // basePageDao.pages().collect(::UpdatePages) + } catch (exception: Exception) { + exception.printStackTrace() + } + } + } + + } } private fun reduce(action: Action, state: S): S = when (action) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/DeletePageItems.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/DeletePageItems.kt index 3e22c083b..899ec66b3 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/DeletePageItems.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/DeletePageItems.kt @@ -20,19 +20,19 @@ package org.kiwix.kiwixmobile.core.page.viewmodel.effects import androidx.appcompat.app.AppCompatActivity import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.dao.BasePageDao import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.viewmodel.PageState data class DeletePageItems( private val state: PageState<*>, - private val pageDao: PageDao + private val basePageDao: BasePageDao ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { if (state.isInSelectionState) { - pageDao.deletePages(state.pageItems.filter(Page::isSelected)) + basePageDao.deletePages(state.pageItems.filter(Page::isSelected)) } else { - pageDao.deletePages(state.pageItems) + basePageDao.deletePages(state.pageItems) } } }