Migrated Note to Room

This commit is contained in:
MohitMaliFtechiz 2023-06-29 17:38:52 +05:30
parent a759e09065
commit a8f8b25681
17 changed files with 210 additions and 30 deletions

View File

@ -28,6 +28,7 @@ import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import javax.inject.Inject
@Deprecated("Replaced with the Room")
class NewNoteDao @Inject constructor(val box: Box<NotesEntity>) : PageDao {
fun notes(): Flowable<List<Page>> = box.asFlowable(
box.query {

View File

@ -0,0 +1,73 @@
/*
* Kiwix Android
* Copyright (c) 2023 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 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.NotesEntity
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)
}
}
fun migrationToRoomInsert(
box: Box<NotesEntity>
) {
val notesEntities = box.all
notesEntities.forEachIndexed { _, notesEntity ->
CoroutineScope(Dispatchers.IO).launch {
saveNote(NoteListItem(notesEntity))
}
}
}
}

View File

@ -19,9 +19,18 @@
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<List<Page>>
interface PageDao : BasePageDao {
override fun pages(): Flowable<List<Page>>
}
interface PageRoomDao : BasePageDao {
override fun pages(): Flow<List<Page>>
}
interface BasePageDao {
fun pages(): Any
fun deletePages(pagesToDelete: List<Page>)
}

View File

@ -0,0 +1,46 @@
/*
* Kiwix Android
* Copyright (c) 2023 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

@ -22,13 +22,16 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
@Suppress("UnnecessaryAbstractClass")
@Database(entities = [RecentSearchRoomEntity::class], version = 1)
@Database(entities = [RecentSearchRoomEntity::class, NotesRoomEntity::class], version = 2)
abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun recentSearchRoomDao(): RecentSearchRoomDao
abstract fun noteRoomDao(): NotesRoomDao
companion object {
private var db: KiwixRoomDatabase? = null

View File

@ -26,7 +26,7 @@ import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao
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.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 bookmarksDao: NewBookmarksDao,
private val historyDao: HistoryDao,
private val notesDao: NewNoteDao,
private val notesRoomDao: NotesRoomDao,
private val languageDao: NewLanguagesDao,
private val recentSearchRoomDao: RecentSearchRoomDao,
private val zimReaderContainer: ZimReaderContainer
@ -124,14 +124,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

@ -33,6 +33,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
@ -95,6 +96,7 @@ interface CoreComponent {
fun connectivityManager(): ConnectivityManager
fun recentSearchRoomDao(): RecentSearchRoomDao
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
fun noteRoomDao(): NotesRoomDao
fun context(): Context
fun downloader(): Downloader

View File

@ -86,4 +86,8 @@ open class DatabaseModule {
@Provides
@Singleton
fun provideNewRecentSearchRoomDao(db: KiwixRoomDatabase) = db.recentSearchRoomDao()
@Singleton
@Provides
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao()
}

View File

@ -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<BookmarkItem>): BookmarkState =
state.copy(pageItems = newItems)

View File

@ -19,9 +19,10 @@ package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects
*/
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
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 +35,14 @@ import javax.inject.Inject
data class ShowDeleteBookmarksDialog(
private val effects: PublishProcessor<SideEffect<*>>,
private val state: PageState<BookmarkItem>,
private val pageDao: PageDao
private val pageDao: BasePageDao
) : SideEffect<Unit> {
@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, pageDao, activity.lifecycleScope)) }
)
}
}

View File

@ -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) })

View File

@ -19,8 +19,10 @@ package org.kiwix.kiwixmobile.core.page.history.viewmodel.effects
*/
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import io.reactivex.processors.PublishProcessor
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
@ -33,13 +35,13 @@ import javax.inject.Inject
data class ShowDeleteHistoryDialog(
private val effects: PublishProcessor<SideEffect<*>>,
private val state: HistoryState,
private val pageDao: PageDao
private val pageDao: BasePageDao
) : SideEffect<Unit> {
@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, pageDao, activity.lifecycleScope))
})
}
}

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,6 +19,7 @@
package org.kiwix.kiwixmobile.core.page.notes.viewmodel
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
@ -32,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 {
@ -66,7 +67,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)

View File

@ -20,9 +20,10 @@ package org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
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 +35,7 @@ import javax.inject.Inject
data class ShowDeleteNotesDialog(
private val effects: PublishProcessor<SideEffect<*>>,
private val state: NotesState,
private val pageDao: PageDao
private val pageDao: BasePageDao
) : SideEffect<Unit> {
@Inject lateinit var dialogShower: DialogShower
override fun invokeWith(activity: AppCompatActivity) {
@ -43,7 +44,7 @@ data class ShowDeleteNotesDialog(
dialogShower.show(
if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes,
{
effects.offer(DeletePageItems(state, pageDao))
effects.offer(DeletePageItems(state, pageDao, activity.lifecycleScope))
}
)
}

View File

@ -20,12 +20,16 @@ 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.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 +46,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
abstract class PageViewModel<T : Page, S : PageState<T>>(
protected val pageDao: PageDao,
protected val basePageDao: BasePageDao,
val sharedPreferenceUtil: SharedPreferenceUtil,
val zimReaderContainer: ZimReaderContainer
) : ViewModel() {
@ -70,11 +74,23 @@ abstract class PageViewModel<T : Page, S : PageState<T>>(
.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 -> {
compositeDisposable.add(viewStateReducer())
viewModelScope.launch {
basePageDao.pages().collect {
actions.offer(UpdatePages(it))
}
}
}
}
}
private fun reduce(action: Action, state: S): S = when (action) {

View File

@ -19,20 +19,30 @@
package org.kiwix.kiwixmobile.core.page.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity
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.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 pageDao: BasePageDao,
private val coroutineScope: CoroutineScope
) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) {
if (state.isInSelectionState) {
pageDao.deletePages(state.pageItems.filter(Page::isSelected))
} else {
pageDao.deletePages(state.pageItems)
coroutineScope.launch(Dispatchers.IO) {
if (state.isInSelectionState) {
pageDao.deletePages(state.pageItems.filter(Page::isSelected))
} else {
pageDao.deletePages(state.pageItems)
}
}
}
}
}