Skip to content

Commit

Permalink
Removed the BasePageDao and RoomPageDao.
Browse files Browse the repository at this point in the history
* We have refactored our code to use PageDao for managing data related to Bookmarks, Notes, and History. With the ability to return Flowable from the Room database, the BasePageDao interface has become unnecessary. Consequently, we have removed BasePageDao and streamlined our code to use PageDao, providing a unified approach for these functionalities. This change reduces code complexity and enhances readability, making the codebase easier to understand and maintain.
  • Loading branch information
MohitMaliDeveloper committed Jun 5, 2024
1 parent dc1ee9a commit 7966089
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -120,34 +120,34 @@ 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
historyRoomDao.saveHistory(historyItem)
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)
}

@Test
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<NoteListItem>)
notesRoomDao.deleteNotes(notesRoomDao.notes().blockingFirst() as List<NoteListItem>)
val noteItem = getNoteListItem(
zimUrl = "http://kiwix.app/MainPage",
noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/MainPage.txt"
)

// Save and retrieve a notes item
notesRoomDao.saveNote(noteItem)
var notesList = notesRoomDao.notes().first() as List<NoteListItem>
var notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
with(notesList.first()) {
assertThat(zimId, equalTo(noteItem.zimId))
assertThat(zimUrl, equalTo(noteItem.zimUrl))
Expand All @@ -160,7 +160,7 @@ class KiwixRoomDatabaseTest {

// test deleting the history
notesRoomDao.deleteNotes(listOf(noteItem))
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 0)

// test deleting all notes
Expand All @@ -171,10 +171,10 @@ class KiwixRoomDatabaseTest {
zimUrl = "http://kiwix.app/Installing"
)
)
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 2)
notesRoomDao.deletePages(notesRoomDao.notes().first())
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesRoomDao.deletePages(notesRoomDao.notes().blockingFirst())
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 0)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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))
Expand All @@ -206,15 +207,15 @@ 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
kiwixRoomDatabase.historyRoomDao().saveHistory(historyItem3)
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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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<NoteListItem>
var notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
with(notesList.first()) {
assertThat(zimId, equalTo(noteItem.zimId))
assertThat(zimUrl, equalTo(noteItem.zimUrl))
Expand All @@ -313,15 +314,15 @@ class ObjectBoxToRoomMigratorTest {

// Migrate data from empty ObjectBox database
objectBoxToRoomMigrator.migrateNotes(box)
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
assertTrue(notesList.isEmpty())

// Test if data successfully migrated to Room and existing data is preserved
kiwixRoomDatabase.notesRoomDao().saveNote(noteItem1)
box.put(NotesEntity(noteItem))
// Migrate data into Room database
objectBoxToRoomMigrator.migrateNotes(box)
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
assertEquals(noteItem.title, notesList.first().title)
assertEquals(2, notesList.size)
val existingItem =
Expand All @@ -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<NoteListItem>
notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
assertEquals(1, notesList.size)

clearRoomAndBoxStoreDatabases(box)
Expand All @@ -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<NoteListItem>
notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
assertTrue(notesList.isEmpty())

// Test large data migration for recent searches
Expand All @@ -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<NoteListItem>
notesList = kiwixRoomDatabase.notesRoomDao().notes().blockingFirst() as List<NoteListItem>
assertEquals(numEntities, notesList.size)
// Assert that the migration completes within a reasonable time frame
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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()

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NoteRoomDaoTest {

// Save and retrieve a notes item
notesRoomDao.saveNote(noteItem)
var notesList = notesRoomDao.notes().first() as List<NoteListItem>
var notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
with(notesList.first()) {
assertThat(zimId, equalTo(noteItem.zimId))
assertThat(zimUrl, equalTo(noteItem.zimUrl))
Expand All @@ -77,25 +77,25 @@ class NoteRoomDaoTest {

// Test update the existing note
notesRoomDao.saveNote(noteItem)
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
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<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 0)

// delete with deletePages method
notesRoomDao.saveNote(noteItem)
notesRoomDao.deletePages(listOf(noteItem))
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 0)

// delete with list of NoteListItem
notesRoomDao.saveNote(noteItem)
notesRoomDao.deleteNotes(listOf(noteItem))
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 0)

// Save note with empty title
Expand All @@ -106,7 +106,7 @@ class NoteRoomDaoTest {
noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/Installing.txt"
)
)
notesList = notesRoomDao.notes().first() as List<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertEquals(notesList.size, 1)
clearNotes()

Expand All @@ -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<NoteListItem>
notesList = notesRoomDao.notes().blockingFirst() as List<NoteListItem>
assertThat(notesList.first().title, equalTo("title Σ"))
}

private suspend fun clearNotes() {
notesRoomDao.deleteNotes(notesRoomDao.notes().first() as List<NoteListItem>)
notesRoomDao.deleteNotes(notesRoomDao.notes().blockingFirst() as List<NoteListItem>)
}
}
8 changes: 2 additions & 6 deletions buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/plugin/AllProjectConfigurer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<HistoryRoomEntity>>
abstract fun historyRoomEntity(): Flowable<List<HistoryRoomEntity>>

fun history(): Flow<List<Page>> = historyRoomEntity().map {
fun history(): Flowable<List<Page>> = historyRoomEntity().map {
it.map(HistoryListItem::HistoryItem)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<NotesRoomEntity>>
abstract fun notesAsEntity(): Flowable<List<NotesRoomEntity>>

fun notes(): Flow<List<Page>> = notesAsEntity().map { it.map(::NoteListItem) }
override fun pages(): Flow<List<Page>> = notes()
fun notes(): Flowable<List<Page>> = notesAsEntity().map { it.map(::NoteListItem) }
override fun pages(): Flowable<List<Page>> = notes()
override fun deletePages(pagesToDelete: List<Page>) =
deleteNotes(pagesToDelete as List<NoteListItem>)

Expand Down
Loading

0 comments on commit 7966089

Please sign in to comment.