Migrated Notes to room database.

* Created NotesRoomDao, and NotesRoomEntity for saving the data in room database.
* Refactored the code for saving the notes in room database.
This commit is contained in:
MohitMaliFtechiz 2024-06-05 12:46:52 +05:30 committed by Kelson
parent 9b1bb3da3c
commit 348440ebc0
8 changed files with 166 additions and 10 deletions

View File

@ -0,0 +1,57 @@
/*
* Kiwix Android
* Copyright (c) 2024 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 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 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 {
@Query("SELECT * FROM NotesRoomEntity ORDER BY NotesRoomEntity.noteTitle")
abstract fun notesAsEntity(): Flow<List<NotesRoomEntity>>
fun notes(): Flow<List<Page>> = notesAsEntity().map { it.map(::NoteListItem) }
override fun pages(): Flow<List<Page>> = notes()
override fun deletePages(pagesToDelete: List<Page>) =
deleteNotes(pagesToDelete as List<NoteListItem>)
fun saveNote(noteItem: NoteListItem) {
saveNote(NotesRoomEntity(noteItem))
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveNote(notesRoomEntity: NotesRoomEntity)
@Query("DELETE FROM NotesRoomEntity WHERE noteTitle=:noteUniqueKey")
abstract fun deleteNote(noteUniqueKey: String)
fun deleteNotes(notesList: List<NoteListItem>) {
notesList.forEachIndexed { _, note ->
val notesRoomEntity = NotesRoomEntity(note)
deleteNote(noteUniqueKey = notesRoomEntity.noteTitle)
}
}
}

View File

@ -0,0 +1,46 @@
/*
* Kiwix Android
* Copyright (c) 2024 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.Index
import androidx.room.PrimaryKey
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
@Entity(indices = [Index(value = ["noteTitle"], unique = true)])
data class NotesRoomEntity(
@PrimaryKey(autoGenerate = true)
var id: Long = 0L,
val zimId: String,
var zimFilePath: String?,
val zimUrl: String,
var noteTitle: String,
var noteFilePath: String,
var favicon: String?
) {
constructor(item: NoteListItem) : this(
id = item.databaseId,
zimId = item.zimId,
zimFilePath = item.zimFilePath,
zimUrl = item.zimUrl,
noteTitle = item.title,
noteFilePath = item.noteFilePath,
favicon = item.favicon
)
}

View File

@ -27,16 +27,27 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
@Suppress("UnnecessaryAbstractClass")
@Database(entities = [RecentSearchRoomEntity::class, HistoryRoomEntity::class], version = 2)
@Database(
entities = [
RecentSearchRoomEntity::class,
HistoryRoomEntity::class,
NotesRoomEntity::class
],
version = 3,
exportSchema = false
)
@TypeConverters(HistoryRoomDaoCoverts::class)
abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun recentSearchRoomDao(): RecentSearchRoomDao
abstract fun historyRoomDao(): HistoryRoomDao
abstract fun noteRoomDao(): NotesRoomDao
companion object {
private var db: KiwixRoomDatabase? = null
@ -46,7 +57,7 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
?: 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)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
.build()
}
}
@ -72,6 +83,31 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
}
}
@Suppress("MagicNumber")
private val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `NotesRoomEntity`(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`zimId` TEXT NOT NULL,
`zimFilePath` TEXT,
`zimUrl` TEXT NOT NULL,
`noteTitle` TEXT NOT NULL,
`noteFilePath` TEXT NOT NULL,
`favicon` TEXT
)
"""
)
database.execSQL(
"""
CREATE UNIQUE INDEX IF NOT EXISTS `index_NotesRoomEntity_noteTitle` ON `NotesRoomEntity` (`noteTitle`)
"""
)
}
}
fun destroyInstance() {
db = null
}

View File

@ -26,7 +26,7 @@ import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.core.dao.NewNoteDao
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
@ -55,7 +55,7 @@ class Repository @Inject internal constructor(
private val bookDao: NewBookDao,
private val libkiwixBookmarks: LibkiwixBookmarks,
private val historyRoomDao: HistoryRoomDao,
private val notesDao: NewNoteDao,
private val notesRoomDao: NotesRoomDao,
private val languageDao: NewLanguagesDao,
private val recentSearchRoomDao: RecentSearchRoomDao,
private val zimReaderContainer: ZimReaderContainer
@ -125,14 +125,14 @@ class Repository @Inject internal constructor(
.subscribeOn(io)
override fun saveNote(noteListItem: NoteListItem): Completable =
Completable.fromAction { notesDao.saveNote(noteListItem) }
Completable.fromAction { notesRoomDao.saveNote(noteListItem) }
.subscribeOn(io)
override fun deleteNotes(noteList: List<NoteListItem>) =
Completable.fromAction { notesDao.deleteNotes(noteList) }
Completable.fromAction { notesRoomDao.deleteNotes(noteList) }
.subscribeOn(io)
override fun deleteNote(noteUniqueKey: String): Completable =
Completable.fromAction { notesDao.deleteNote(noteUniqueKey) }
Completable.fromAction { notesRoomDao.deleteNote(noteUniqueKey) }
.subscribeOn(io)
}

View File

@ -36,6 +36,7 @@ import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.core.dao.NewNoteDao
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.data.DataModule
import org.kiwix.kiwixmobile.core.data.DataSource
@ -102,6 +103,7 @@ interface CoreComponent {
fun libkiwixBookmarks(): LibkiwixBookmarks
fun recentSearchRoomDao(): RecentSearchRoomDao
fun historyRoomDao(): HistoryRoomDao
fun noteRoomDao(): NotesRoomDao
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
fun context(): Context
fun downloader(): Downloader

View File

@ -92,4 +92,8 @@ open class DatabaseModule {
@Provides
@Singleton
fun provideHistoryDao(db: KiwixRoomDatabase) = db.historyRoomDao()
@Singleton
@Provides
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao()
}

View File

@ -1,6 +1,7 @@
package org.kiwix.kiwixmobile.core.page.notes.adapter
import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
@ -40,4 +41,14 @@ data class NoteListItem(
favicon = zimFileReader.favicon,
noteFilePath = noteFilePath
)
constructor(notesRoomEntity: NotesRoomEntity) : this(
notesRoomEntity.id,
notesRoomEntity.zimId,
notesRoomEntity.noteTitle,
notesRoomEntity.zimFilePath,
notesRoomEntity.zimUrl,
notesRoomEntity.noteFilePath,
notesRoomEntity.favicon
)
}

View File

@ -19,7 +19,7 @@
package org.kiwix.kiwixmobile.core.page.notes.viewmodel
import kotlinx.coroutines.CoroutineScope
import org.kiwix.kiwixmobile.core.dao.NewNoteDao
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowDeleteNotesDialog
@ -33,10 +33,10 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject
class NotesViewModel @Inject constructor(
notesDao: NewNoteDao,
notesRoomDao: NotesRoomDao,
zimReaderContainer: ZimReaderContainer,
sharedPrefs: SharedPreferenceUtil
) : PageViewModel<NoteListItem, NotesState>(notesDao, sharedPrefs, zimReaderContainer),
) : PageViewModel<NoteListItem, NotesState>(notesRoomDao, sharedPrefs, zimReaderContainer),
PageViewModelClickListener {
init {