diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt index 7cc0b4917..2e969726b 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt @@ -106,7 +106,7 @@ class KiwixRoomDatabaseTest { // test inserting into history database historyRoomDao.saveHistory(historyItem) - var historyList = historyRoomDao.historyRoomEntity().first() + var historyList = historyRoomDao.historyRoomEntity().blockingFirst() with(historyList.first()) { assertThat(historyTitle, equalTo(historyItem.title)) assertThat(zimId, equalTo(historyItem.zimId)) @@ -120,7 +120,7 @@ class KiwixRoomDatabaseTest { // test deleting the history historyRoomDao.deleteHistory(listOf(historyItem)) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertEquals(historyList.size, 0) // test deleting all history @@ -128,10 +128,10 @@ class KiwixRoomDatabaseTest { historyRoomDao.saveHistory( getHistoryItem(databaseId = 2) ) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertEquals(historyList.size, 2) historyRoomDao.deleteAllHistory() - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertEquals(historyList.size, 0) } @@ -139,7 +139,7 @@ class KiwixRoomDatabaseTest { fun testNoteRoomDao() = runBlocking { notesRoomDao = db.notesRoomDao() // delete all the notes from database to properly run the test cases. - notesRoomDao.deleteNotes(notesRoomDao.notes().first() as List) + notesRoomDao.deleteNotes(notesRoomDao.notes().blockingFirst() as List) val noteItem = getNoteListItem( zimUrl = "http://kiwix.app/MainPage", noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/MainPage.txt" @@ -147,7 +147,7 @@ class KiwixRoomDatabaseTest { // Save and retrieve a notes item notesRoomDao.saveNote(noteItem) - var notesList = notesRoomDao.notes().first() as List + var notesList = notesRoomDao.notes().blockingFirst() as List with(notesList.first()) { assertThat(zimId, equalTo(noteItem.zimId)) assertThat(zimUrl, equalTo(noteItem.zimUrl)) @@ -160,7 +160,7 @@ class KiwixRoomDatabaseTest { // test deleting the history notesRoomDao.deleteNotes(listOf(noteItem)) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 0) // test deleting all notes @@ -171,10 +171,10 @@ class KiwixRoomDatabaseTest { zimUrl = "http://kiwix.app/Installing" ) ) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 2) - notesRoomDao.deletePages(notesRoomDao.notes().first()) - notesList = notesRoomDao.notes().first() as List + notesRoomDao.deletePages(notesRoomDao.notes().blockingFirst()) + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 0) } diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt index 4adcc270b..195dd193c 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt @@ -169,7 +169,8 @@ class ObjectBoxToRoomMigratorTest { // delete history for testing other edge cases kiwixRoomDatabase.recentSearchRoomDao().deleteSearchHistory() kiwixRoomDatabase.historyRoomDao().deleteAllHistory() - kiwixRoomDatabase.notesRoomDao().deletePages(kiwixRoomDatabase.notesRoomDao().notes().first()) + kiwixRoomDatabase.notesRoomDao() + .deletePages(kiwixRoomDatabase.notesRoomDao().notes().blockingFirst()) box.removeAll() } @@ -190,7 +191,7 @@ class ObjectBoxToRoomMigratorTest { // migrate data into room database objectBoxToRoomMigrator.migrateHistory(box) // check if data successfully migrated to room - val actual = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + val actual = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() with(actual.first()) { assertThat(historyTitle, equalTo(historyItem.title)) assertThat(zimId, equalTo(historyItem.zimId)) @@ -206,7 +207,7 @@ class ObjectBoxToRoomMigratorTest { // Migrate data from empty ObjectBox database objectBoxToRoomMigrator.migrateHistory(box) - var actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + var actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() assertTrue(actualData.isEmpty()) // Test if data successfully migrated to Room and existing data is preserved @@ -214,7 +215,7 @@ class ObjectBoxToRoomMigratorTest { box.put(HistoryEntity(historyItem2)) // Migrate data into Room database objectBoxToRoomMigrator.migrateHistory(box) - actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() assertEquals(2, actualData.size) val existingItem = actualData.find { @@ -233,7 +234,7 @@ class ObjectBoxToRoomMigratorTest { kiwixRoomDatabase.historyRoomDao().saveHistory(historyItem) box.put(HistoryEntity(historyItem)) objectBoxToRoomMigrator.migrateHistory(box) - actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() assertEquals(1, actualData.size) clearRoomAndBoxStoreDatabases(box) @@ -247,7 +248,7 @@ class ObjectBoxToRoomMigratorTest { } catch (_: Exception) { } // Ensure Room database remains empty or unaffected by the invalid data - actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() assertTrue(actualData.isEmpty()) // Test large data migration for recent searches @@ -269,7 +270,7 @@ class ObjectBoxToRoomMigratorTest { val endTime = System.currentTimeMillis() val migrationTime = endTime - startTime // Check if data successfully migrated to Room - actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().blockingFirst() assertEquals(numEntities, actualData.size) // Assert that the migration completes within a reasonable time frame assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000) @@ -298,7 +299,7 @@ class ObjectBoxToRoomMigratorTest { // migrate data into room database objectBoxToRoomMigrator.migrateNotes(box) // check if data successfully migrated to room - var notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + var notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List with(notesList.first()) { assertThat(zimId, equalTo(noteItem.zimId)) assertThat(zimUrl, equalTo(noteItem.zimUrl)) @@ -313,7 +314,7 @@ class ObjectBoxToRoomMigratorTest { // Migrate data from empty ObjectBox database objectBoxToRoomMigrator.migrateNotes(box) - notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List assertTrue(notesList.isEmpty()) // Test if data successfully migrated to Room and existing data is preserved @@ -321,7 +322,7 @@ class ObjectBoxToRoomMigratorTest { box.put(NotesEntity(noteItem)) // Migrate data into Room database objectBoxToRoomMigrator.migrateNotes(box) - notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List assertEquals(noteItem.title, notesList.first().title) assertEquals(2, notesList.size) val existingItem = @@ -342,7 +343,7 @@ class ObjectBoxToRoomMigratorTest { box.put(NotesEntity(noteItem1)) // Migrate data into Room database objectBoxToRoomMigrator.migrateNotes(box) - notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List assertEquals(1, notesList.size) clearRoomAndBoxStoreDatabases(box) @@ -356,7 +357,7 @@ class ObjectBoxToRoomMigratorTest { } catch (_: Exception) { } // Ensure Room database remains empty or unaffected by the invalid data - notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List assertTrue(notesList.isEmpty()) // Test large data migration for recent searches @@ -378,7 +379,7 @@ class ObjectBoxToRoomMigratorTest { val endTime = System.currentTimeMillis() val migrationTime = endTime - startTime // Check if data successfully migrated to Room - notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List + notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List assertEquals(numEntities, notesList.size) // Assert that the migration completes within a reasonable time frame assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/HistoryRoomDaoTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/HistoryRoomDaoTest.kt index 7429dc6be..33dd11178 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/HistoryRoomDaoTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/HistoryRoomDaoTest.kt @@ -22,7 +22,6 @@ import android.content.Context import androidx.room.Room import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 -import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.core.IsEqual.equalTo @@ -65,7 +64,7 @@ class HistoryRoomDaoTest { // Save and retrieve a history item historyRoomDao.saveHistory(historyItem) - var historyList = historyRoomDao.historyRoomEntity().first() + var historyList = historyRoomDao.historyRoomEntity().blockingFirst() with(historyList.first()) { assertThat(historyTitle, equalTo(historyItem.title)) assertThat(zimId, equalTo(historyItem.zimId)) @@ -79,26 +78,26 @@ class HistoryRoomDaoTest { // Test to update the same day history for url historyRoomDao.saveHistory(historyItem) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertEquals(historyList.size, 1) // Delete the saved history item historyRoomDao.deleteHistory(listOf(historyItem)) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertEquals(historyList.size, 0) // Save and delete all history items historyRoomDao.saveHistory(historyItem) historyRoomDao.saveHistory(getHistoryItem(databaseId = 2, dateString = "31 May 2024")) historyRoomDao.deleteAllHistory() - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertThat(historyList.size, equalTo(0)) // Save history item with empty fields val emptyHistoryUrl = "" val emptyTitle = "" historyRoomDao.saveHistory(getHistoryItem(emptyTitle, emptyHistoryUrl, databaseId = 1)) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertThat(historyList.size, equalTo(1)) historyRoomDao.deleteAllHistory() @@ -118,13 +117,13 @@ class HistoryRoomDaoTest { val unicodeTitle = "title \u03A3" // Unicode character for Greek capital letter Sigma val historyItem2 = getHistoryItem(title = unicodeTitle, databaseId = 2) historyRoomDao.saveHistory(historyItem2) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertThat(historyList.first().historyTitle, equalTo("title Σ")) // Test deletePages function historyRoomDao.saveHistory(historyItem) historyRoomDao.deletePages(listOf(historyItem, historyItem2)) - historyList = historyRoomDao.historyRoomEntity().first() + historyList = historyRoomDao.historyRoomEntity().blockingFirst() assertThat(historyList.size, equalTo(0)) } } diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/NoteRoomDaoTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/NoteRoomDaoTest.kt index 62609b4a2..8f3d9b0ce 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/NoteRoomDaoTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/roomDao/NoteRoomDaoTest.kt @@ -64,7 +64,7 @@ class NoteRoomDaoTest { // Save and retrieve a notes item notesRoomDao.saveNote(noteItem) - var notesList = notesRoomDao.notes().first() as List + var notesList = notesRoomDao.notes().blockingFirst() as List with(notesList.first()) { assertThat(zimId, equalTo(noteItem.zimId)) assertThat(zimUrl, equalTo(noteItem.zimUrl)) @@ -77,25 +77,25 @@ class NoteRoomDaoTest { // Test update the existing note notesRoomDao.saveNote(noteItem) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 1) // Delete the saved note item with all delete methods available in NoteRoomDao. // delete via noteTitle notesRoomDao.deleteNote(noteItem.title) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 0) // delete with deletePages method notesRoomDao.saveNote(noteItem) notesRoomDao.deletePages(listOf(noteItem)) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 0) // delete with list of NoteListItem notesRoomDao.saveNote(noteItem) notesRoomDao.deleteNotes(listOf(noteItem)) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 0) // Save note with empty title @@ -106,7 +106,7 @@ class NoteRoomDaoTest { noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/Installing.txt" ) ) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertEquals(notesList.size, 1) clearNotes() @@ -127,11 +127,11 @@ class NoteRoomDaoTest { val noteListItem2 = getNoteListItem(title = unicodeTitle, zimUrl = "http://kiwix.app/Installing") notesRoomDao.saveNote(noteListItem2) - notesList = notesRoomDao.notes().first() as List + notesList = notesRoomDao.notes().blockingFirst() as List assertThat(notesList.first().title, equalTo("title Σ")) } private suspend fun clearNotes() { - notesRoomDao.deleteNotes(notesRoomDao.notes().first() as List) + notesRoomDao.deleteNotes(notesRoomDao.notes().blockingFirst() as List) } } diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index 92cebeb65..a245ad655 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -26,12 +26,6 @@ object Libs { const val kotlinx_coroutines_test: String = "org.jetbrains.kotlinx:kotlinx-coroutines-test:" + Versions.org_jetbrains_kotlinx_kotlinx_coroutines - /** - * https://github.com/Kotlin/kotlinx.coroutines - */ - const val kotlinx_coroutines_rx2: String = "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:" + - Versions.org_jetbrains_kotlinx_kotlinx_coroutines - /** * https://developer.android.com/testing */ @@ -363,4 +357,6 @@ object Libs { const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion const val roomRuntime = "androidx.room:room-runtime:" + 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 0c32d0ecd..ae6d84569 100644 --- a/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt +++ b/buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt @@ -204,12 +204,12 @@ class AllProjectConfigurer { implementation(Libs.fetch) implementation(Libs.rxandroid) implementation(Libs.rxjava) - implementation(Libs.kotlinx_coroutines_rx2) implementation(Libs.preference_ktx) implementation(Libs.material_show_case_view) implementation(Libs.roomKtx) annotationProcessor(Libs.roomCompiler) implementation(Libs.roomRuntime) + implementation(Libs.roomRxjava2) kapt(Libs.roomCompiler) } } 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 index 382d92c8d..6221bfdad 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt @@ -24,18 +24,17 @@ import androidx.room.Insert import androidx.room.Query import androidx.room.TypeConverter import androidx.room.Update -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map +import io.reactivex.Flowable 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 : PageRoomDao { +abstract class HistoryRoomDao : PageDao { @Query("SELECT * FROM HistoryRoomEntity ORDER BY HistoryRoomEntity.timeStamp DESC") - abstract fun historyRoomEntity(): Flow> + abstract fun historyRoomEntity(): Flowable> - fun history(): Flow> = historyRoomEntity().map { + fun history(): Flowable> = historyRoomEntity().map { it.map(HistoryListItem::HistoryItem) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt index 1acaaaaac..a8ac791c9 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt @@ -22,19 +22,18 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map +import io.reactivex.Flowable import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem @Dao -abstract class NotesRoomDao : PageRoomDao { +abstract class NotesRoomDao : PageDao { @Query("SELECT * FROM NotesRoomEntity ORDER BY NotesRoomEntity.noteTitle") - abstract fun notesAsEntity(): Flow> + abstract fun notesAsEntity(): Flowable> - fun notes(): Flow> = notesAsEntity().map { it.map(::NoteListItem) } - override fun pages(): Flow> = notes() + fun notes(): Flowable> = notesAsEntity().map { it.map(::NoteListItem) } + override fun pages(): Flowable> = notes() override fun deletePages(pagesToDelete: List) = deleteNotes(pagesToDelete as List) 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 10ef637cb..ac6091666 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,18 +19,9 @@ 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 : BasePageDao { - override fun pages(): Flowable> -} - -interface PageRoomDao : BasePageDao { - override fun pages(): Flow> -} - -interface BasePageDao { - fun pages(): Any +interface PageDao { + fun pages(): Flowable> fun deletePages(pagesToDelete: List) } 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 1db195653..f491ddc78 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 @@ -66,7 +66,7 @@ class BookmarkViewModel @Inject constructor( state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) override fun createDeletePageDialogEffect(state: BookmarkState, viewModelScope: CoroutineScope) = - ShowDeleteBookmarksDialog(effects, state, basePageDao, viewModelScope) + ShowDeleteBookmarksDialog(effects, state, pageDao, viewModelScope) override fun copyWithNewItems( state: BookmarkState, 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 91baeca3d..ac22cb83c 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 @@ -22,7 +22,7 @@ import androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import kotlinx.coroutines.CoroutineScope 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.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem import org.kiwix.kiwixmobile.core.page.viewmodel.PageState @@ -35,7 +35,7 @@ import javax.inject.Inject data class ShowDeleteBookmarksDialog( private val effects: PublishProcessor>, private val state: PageState, - private val basePageDao: BasePageDao, + private val pageDao: PageDao, private val viewModelScope: CoroutineScope ) : SideEffect { @Inject lateinit var dialogShower: DialogShower @@ -43,7 +43,7 @@ data class ShowDeleteBookmarksDialog( activity.cachedComponent.inject(this) dialogShower.show( if (state.isInSelectionState) DeleteSelectedBookmarks else DeleteAllBookmarks, - { effects.offer(DeletePageItems(state, basePageDao, viewModelScope)) } + { effects.offer(DeletePageItems(state, pageDao, viewModelScope)) } ) } } 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 7090dcb4a..5a9493cdd 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 @@ -62,7 +62,7 @@ class HistoryViewModel @Inject constructor( state: HistoryState, viewModelScope: CoroutineScope ) = - ShowDeleteHistoryDialog(effects, state, basePageDao, viewModelScope) + ShowDeleteHistoryDialog(effects, state, pageDao, viewModelScope) 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 190cf0fa2..2b85395b5 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 @@ -22,7 +22,7 @@ import androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import kotlinx.coroutines.CoroutineScope 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.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems @@ -34,14 +34,14 @@ import javax.inject.Inject data class ShowDeleteHistoryDialog( private val effects: PublishProcessor>, private val state: HistoryState, - private val basePageDao: BasePageDao, + private val pageDao: PageDao, private val viewModelScope: CoroutineScope ) : 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, basePageDao, viewModelScope)) + effects.offer(DeletePageItems(state, pageDao, viewModelScope)) }) } } 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 b9683deb1..11c47ecbf 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 @@ -67,7 +67,7 @@ class NotesViewModel @Inject constructor( state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) override fun createDeletePageDialogEffect(state: NotesState, viewModelScope: CoroutineScope) = - ShowDeleteNotesDialog(effects, state, basePageDao, viewModelScope) + ShowDeleteNotesDialog(effects, state, pageDao, viewModelScope) 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 c007e078d..78c2116dc 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 androidx.appcompat.app.AppCompatActivity import io.reactivex.processors.PublishProcessor import kotlinx.coroutines.CoroutineScope 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.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesState import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems @@ -35,7 +35,7 @@ import javax.inject.Inject data class ShowDeleteNotesDialog( private val effects: PublishProcessor>, private val state: NotesState, - private val basePageDao: BasePageDao, + private val pageDao: PageDao, private val viewModelScope: CoroutineScope ) : SideEffect { @Inject lateinit var dialogShower: DialogShower @@ -45,7 +45,7 @@ data class ShowDeleteNotesDialog( dialogShower.show( if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes, { - effects.offer(DeletePageItems(state, basePageDao, viewModelScope)) + effects.offer(DeletePageItems(state, pageDao, viewModelScope)) } ) } 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 cac32753a..95379f6c3 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 @@ -26,11 +26,8 @@ import io.reactivex.disposables.Disposable import io.reactivex.processors.PublishProcessor import io.reactivex.schedulers.Schedulers import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.rx2.asFlowable 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 @@ -47,7 +44,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil abstract class PageViewModel>( - protected val basePageDao: BasePageDao, + protected val pageDao: PageDao, val sharedPreferenceUtil: SharedPreferenceUtil, val zimReaderContainer: ZimReaderContainer ) : ViewModel() { @@ -75,23 +72,11 @@ abstract class PageViewModel>( .subscribe(state::postValue, Throwable::printStackTrace) protected fun addDisposablesToCompositeDisposable() { - when (basePageDao) { - is PageDao -> { - compositeDisposable.addAll( - viewStateReducer(), - basePageDao.pages().subscribeOn(Schedulers.io()) - .subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace) - ) - } - - is PageRoomDao -> { - compositeDisposable.addAll( - viewStateReducer(), - basePageDao.pages().asFlowable().subscribeOn(Schedulers.io()) - .subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace) - ) - } - } + compositeDisposable.addAll( + viewStateReducer(), + pageDao.pages().subscribeOn(Schedulers.io()) + .subscribe({ actions.offer(UpdatePages(it)) }, Throwable::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 35795c494..12dab8688 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 @@ -23,21 +23,21 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers 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.page.adapter.Page import org.kiwix.kiwixmobile.core.page.viewmodel.PageState data class DeletePageItems( private val state: PageState<*>, - private val basePageDao: BasePageDao, + private val pageDao: PageDao, private val viewModelScope: CoroutineScope ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { viewModelScope.launch(Dispatchers.IO) { if (state.isInSelectionState) { - basePageDao.deletePages(state.pageItems.filter(Page::isSelected)) + pageDao.deletePages(state.pageItems.filter(Page::isSelected)) } else { - basePageDao.deletePages(state.pageItems) + pageDao.deletePages(state.pageItems) } } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModelTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModelTest.kt index f2e9d66e2..90c87b7b4 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModelTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModelTest.kt @@ -4,12 +4,11 @@ import io.mockk.clearAllMocks import io.mockk.every import io.mockk.mockk import io.reactivex.plugins.RxJavaPlugins +import io.reactivex.processors.PublishProcessor import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.TestScheduler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flow import kotlinx.coroutines.test.runTest import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach @@ -44,8 +43,8 @@ internal class HistoryViewModelTest { RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } } - private val itemsFromDb: Flow> = - flow { } + private val itemsFromDb: PublishProcessor> = + PublishProcessor.create() @BeforeEach fun init() {