diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt index 9e7483460..0b8c1c43a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt @@ -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) : PageDao { fun notes(): Flowable> = box.asFlowable( box.query { 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 new file mode 100644 index 000000000..40ae3641e --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt @@ -0,0 +1,73 @@ +/* + * Kiwix Android + * Copyright (c) 2023 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.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> + + fun notes(): Flow> = notesAsEntity().map { it.map(::NoteListItem) } + override fun pages(): Flow> = notes() + override fun deletePages(pagesToDelete: List) = + deleteNotes(pagesToDelete as List) + + 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) { + notesList.forEachIndexed { _, note -> + val notesRoomEntity = NotesRoomEntity(note) + deleteNote(noteUniqueKey = notesRoomEntity.noteTitle) + } + } + + fun migrationToRoomInsert( + box: Box + ) { + val notesEntities = box.all + notesEntities.forEachIndexed { _, notesEntity -> + CoroutineScope(Dispatchers.IO).launch { + saveNote(NoteListItem(notesEntity)) + } + } + } +} 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..10ef637cb 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,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> +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/NotesRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesRoomEntity.kt new file mode 100644 index 000000000..feb3fd63d --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesRoomEntity.kt @@ -0,0 +1,46 @@ +/* + * Kiwix Android + * Copyright (c) 2023 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.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 + ) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/KiwixRoomDatabase.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/KiwixRoomDatabase.kt index d0eefc8b7..81eb2d5d1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/KiwixRoomDatabase.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/KiwixRoomDatabase.kt @@ -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 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 f3c94fce7..346b85d7a 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 @@ -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) = - 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) } 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 9eb7ced7e..177a62cbe 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 @@ -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 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 901193532..8e54e62d1 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 @@ -86,4 +86,8 @@ open class DatabaseModule { @Provides @Singleton fun provideNewRecentSearchRoomDao(db: KiwixRoomDatabase) = db.recentSearchRoomDao() + + @Singleton + @Provides + fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.noteRoomDao() } 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..f10e3bf8d 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 @@ -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>, private val state: PageState, - private val pageDao: PageDao + private val pageDao: 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, pageDao, activity.lifecycleScope)) } ) } } 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..2a992bc79 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 @@ -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..028af3dc2 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 @@ -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>, private val state: HistoryState, - private val pageDao: PageDao + private val pageDao: 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, pageDao, activity.lifecycleScope)) }) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt index 4f40d24cd..1fba91fd0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt @@ -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 + ) } 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..be05634a5 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 @@ -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(notesDao, sharedPrefs, zimReaderContainer), +) : PageViewModel(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) 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..b10f643e3 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 @@ -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>, private val state: NotesState, - private val pageDao: PageDao + private val pageDao: BasePageDao ) : SideEffect { @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)) } ) } 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..b1921490f 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,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>( - protected val pageDao: PageDao, + protected val basePageDao: BasePageDao, val sharedPreferenceUtil: SharedPreferenceUtil, val zimReaderContainer: ZimReaderContainer ) : ViewModel() { @@ -70,11 +74,23 @@ 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 -> { + compositeDisposable.add(viewStateReducer()) + viewModelScope.launch { + basePageDao.pages().collect { + actions.offer(UpdatePages(it)) + } + } + } + } } 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..9140c9c47 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 @@ -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 { 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) + } + } } } }