runtime added maybe worked or not :(

This commit is contained in:
Gouri Panda 2022-12-28 16:51:44 +05:30
parent 6cab5e924f
commit 66d17844aa
21 changed files with 363 additions and 44 deletions

View File

@ -423,4 +423,5 @@ object Libs {
const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion const val roomKtx = "androidx.room:room-ktx:" + Versions.roomVersion
const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion const val roomCompiler = "androidx.room:room-compiler:" + Versions.roomVersion
const val roomRuntime = "androidx.room:room-runtime:" + Versions.roomVersion
} }

View File

@ -203,7 +203,10 @@ class AllProjectConfigurer {
implementation(Libs.rxjava) implementation(Libs.rxjava)
implementation(Libs.preference_ktx) implementation(Libs.preference_ktx)
implementation(Libs.roomKtx) implementation(Libs.roomKtx)
annotationProcessor(Libs.roomCompiler)
implementation(Libs.roomRuntime)
kapt(Libs.roomCompiler) kapt(Libs.roomCompiler)
// ksp(Libs.roomCompiler)
} }
} }
} }

View File

@ -54,5 +54,11 @@ internal fun DependencyHandlerScope.testImplementation(dependency: String) =
internal fun DependencyHandlerScope.implementation(dependency: String) = internal fun DependencyHandlerScope.implementation(dependency: String) =
addDependency("implementation", dependency) addDependency("implementation", dependency)
internal fun DependencyHandlerScope.annotationProcessor(dependency: String) =
addDependency("annotationProcessor", dependency)
internal fun DependencyHandlerScope.ksp(dependency: String) =
addDependency("ksp", dependency)
private fun DependencyHandlerScope.addDependency(configurationName: String, dependency: String) = private fun DependencyHandlerScope.addDependency(configurationName: String, dependency: String) =
add(configurationName, dependency) add(configurationName, dependency)

View File

@ -0,0 +1,94 @@
/*
* Kiwix Android
* Copyright (c) 2022 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.lifecycle.Transformations.map
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.ProvidedTypeConverter
import androidx.room.Query
import androidx.room.TypeConverter
import androidx.room.Update
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.HistoryEntity
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 : BasePageDao {
@Query("SELECT * FROM HistoryRoomEntity ORDER BY HistoryRoomEntity.timeStamp DESC")
abstract fun historyRoomEntity(): Flow<List<HistoryRoomEntity>>
fun history(): Flow<List<Page>> = historyRoomEntity().map {
it.map(HistoryListItem::HistoryItem)
}
// // TODO: After refactoring all the database we should implement [PageDao]
override fun pages() = history()
override fun deletePages(pagesToDelete: List<Page>) =
deleteHistory(pagesToDelete as List<HistoryListItem.HistoryItem>)
@Query("SELECT * FROM HistoryRoomEntity WHERE historyUrl LIKE :url AND dateString LIKE :date")
abstract fun getHistoryItem(url: String, date: String): HistoryListItem.HistoryItem
fun getHistoryItem(historyItem: HistoryListItem.HistoryItem): HistoryListItem.HistoryItem =
getHistoryItem(historyItem.historyUrl, historyItem.dateString)
@Update
abstract fun updateHistoryItem(historyItem: HistoryListItem.HistoryItem)
fun saveHistory(historyItem: HistoryListItem.HistoryItem) {
val item = getHistoryItem(historyItem)
updateHistoryItem(item)
}
@Delete
abstract fun deleteHistory(historyList: List<HistoryListItem.HistoryItem>)
@Query("DELETE FROM HistoryRoomEntity")
abstract fun deleteAllHistory()
fun migrationToRoomHistory(
box: Box<HistoryEntity>
) {
val historyEntityList = box.all
historyEntityList.forEachIndexed { _, item ->
CoroutineScope(Dispatchers.IO).launch {
// saveHistory(HistoryListItem.HistoryItem(item))
// Todo Should we remove object store data now?
}
}
}
}
class HistoryRoomDaoCoverts {
@TypeConverter
fun fromHistoryRoomEntity(historyRoomEntity: HistoryRoomEntity): HistoryListItem =
HistoryListItem.HistoryItem(historyRoomEntity)
@TypeConverter
fun historyItemToHistoryListItem(historyItem: HistoryListItem.HistoryItem): HistoryRoomEntity =
HistoryRoomEntity(historyItem)
}

View File

@ -19,9 +19,19 @@
package org.kiwix.kiwixmobile.core.dao package org.kiwix.kiwixmobile.core.dao
import io.reactivex.Flowable import io.reactivex.Flowable
import kotlinx.coroutines.flow.Flow
import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.adapter.Page
interface PageDao { interface PageDao : BasePageDao {
fun pages(): Flowable<List<Page>> 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>) fun deletePages(pagesToDelete: List<Page>)
} }

View File

@ -0,0 +1,48 @@
/*
* Kiwix Android
* Copyright (c) 2022 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.PrimaryKey
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem
@Entity
data class HistoryRoomEntity(
@PrimaryKey var id: Long = 0L,
val zimId: String,
val zimName: String,
val zimFilePath: String,
val favicon: String?,
val historyUrl: String,
val historyTitle: String,
val dateString: String,
val timeStamp: Long
) {
constructor(historyItem: HistoryListItem.HistoryItem) : this(
historyItem.databaseId,
historyItem.zimId,
historyItem.zimName,
historyItem.zimFilePath,
historyItem.favicon,
historyItem.historyUrl,
historyItem.title,
historyItem.dateString,
historyItem.timeStamp
)
}

View File

@ -17,8 +17,11 @@
*/ */
package org.kiwix.kiwixmobile.core.data package org.kiwix.kiwixmobile.core.data
import android.content.Context
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.objectbox.BoxStore
import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase
import javax.inject.Singleton import javax.inject.Singleton
@Module @Module

View File

@ -22,7 +22,7 @@ import io.reactivex.Completable
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Scheduler import io.reactivex.Scheduler
import io.reactivex.Single import io.reactivex.Single
import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
@ -54,7 +54,7 @@ class Repository @Inject internal constructor(
@param:MainThread private val mainThread: Scheduler, @param:MainThread private val mainThread: Scheduler,
private val bookDao: NewBookDao, private val bookDao: NewBookDao,
private val bookmarksDao: NewBookmarksDao, private val bookmarksDao: NewBookmarksDao,
private val historyDao: HistoryDao, private val historyRoomDao: HistoryRoomDao,
private val notesDao: NewNoteDao, private val notesDao: NewNoteDao,
private val languageDao: NewLanguagesDao, private val languageDao: NewLanguagesDao,
private val recentSearchDao: NewRecentSearchRoomDao, private val recentSearchDao: NewRecentSearchRoomDao,
@ -90,17 +90,17 @@ class Repository @Inject internal constructor(
.subscribeOn(io) .subscribeOn(io)
override fun saveHistory(history: HistoryItem) = override fun saveHistory(history: HistoryItem) =
Completable.fromAction { historyDao.saveHistory(history) } Completable.fromAction { historyRoomDao.saveHistory(history) }
.subscribeOn(io) .subscribeOn(io)
override fun deleteHistory(historyList: List<HistoryListItem>) = override fun deleteHistory(historyList: List<HistoryListItem>) =
Completable.fromAction { Completable.fromAction {
historyDao.deleteHistory(historyList.filterIsInstance(HistoryItem::class.java)) historyRoomDao.deleteHistory(historyList.filterIsInstance(HistoryItem::class.java))
} }
.subscribeOn(io) .subscribeOn(io)
override fun clearHistory() = Completable.fromAction { override fun clearHistory() = Completable.fromAction {
historyDao.deleteAllHistory() historyRoomDao.deleteAllHistory()
recentSearchDao.deleteSearchHistory() recentSearchDao.deleteSearchHistory()
}.subscribeOn(io) }.subscribeOn(io)

View File

@ -19,29 +19,46 @@
package org.kiwix.kiwixmobile.core.data.local package org.kiwix.kiwixmobile.core.data.local
import android.content.Context import android.content.Context
import android.util.Log
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import io.objectbox.BoxStore import io.objectbox.BoxStore
import io.objectbox.kotlin.boxFor import io.objectbox.kotlin.boxFor
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem
@Suppress("UnnecessaryAbstractClass") @Suppress("UnnecessaryAbstractClass")
@Database(entities = [RecentSearchRoomEntity::class], version = 1) @Database(
entities = [RecentSearchRoomEntity::class, HistoryRoomEntity::class],
version = 2
)
@TypeConverters(HistoryRoomDaoCoverts::class)
abstract class KiwixRoomDatabase : RoomDatabase() { abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao abstract fun newRecentSearchRoomDao(): NewRecentSearchRoomDao
abstract fun historyRoomDao(): HistoryRoomDao
companion object { companion object {
private var db: KiwixRoomDatabase? = null private var db: KiwixRoomDatabase? = null
private var box: BoxStore? = null
fun getInstance(context: Context, boxStore: BoxStore): KiwixRoomDatabase { fun getInstance(context: Context, boxStore: BoxStore): KiwixRoomDatabase {
box = boxStore
return db ?: synchronized(KiwixRoomDatabase::class) { return db ?: synchronized(KiwixRoomDatabase::class) {
return@getInstance db return@getInstance db
?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db") ?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db")
// We have already database name called kiwix.db in order to avoid complexity we named as // We have already database name called kiwix.db in order to avoid complexity we named as
// kiwixRoom.db // kiwixRoom.db
// .addMigrations(MIGRATION_1_2)
.build().also { .build().also {
it.migrateRecentSearch(boxStore) it.migrateRecentSearch(boxStore)
it.migrateHistory(boxStore)
} }
} }
} }
@ -49,9 +66,21 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
fun destroyInstance() { fun destroyInstance() {
db = null db = null
} }
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
db!!::migrateHistory.let { box?.let(it) } ?: run {
Log.d("gouri", "box store is null")
}
}
}
} }
fun migrateRecentSearch(boxStore: BoxStore) { fun migrateRecentSearch(boxStore: BoxStore) {
newRecentSearchRoomDao().migrationToRoomInsert(boxStore.boxFor()) newRecentSearchRoomDao().migrationToRoomInsert(boxStore.boxFor())
} }
fun migrateHistory(boxStore: BoxStore) {
historyRoomDao().migrationToRoomHistory(boxStore.boxFor())
}
} }

View File

@ -28,6 +28,7 @@ import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.StorageObserver import org.kiwix.kiwixmobile.core.StorageObserver
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
@ -36,6 +37,7 @@ import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao
import org.kiwix.kiwixmobile.core.data.DataModule import org.kiwix.kiwixmobile.core.data.DataModule
import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.data.DataSource
import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase
import org.kiwix.kiwixmobile.core.data.local.dao.BookDao import org.kiwix.kiwixmobile.core.data.local.dao.BookDao
import org.kiwix.kiwixmobile.core.data.local.dao.BookmarksDao import org.kiwix.kiwixmobile.core.data.local.dao.BookmarksDao
import org.kiwix.kiwixmobile.core.data.remote.KiwixService import org.kiwix.kiwixmobile.core.data.remote.KiwixService
@ -77,6 +79,7 @@ interface CoreComponent {
fun build(): CoreComponent fun build(): CoreComponent
} }
fun kiwixRoomDataBase(): KiwixRoomDatabase
fun activityComponentBuilder(): CoreActivityComponent.Builder fun activityComponentBuilder(): CoreActivityComponent.Builder
fun zimReaderContainer(): ZimReaderContainer fun zimReaderContainer(): ZimReaderContainer
fun sharedPrefUtil(): SharedPreferenceUtil fun sharedPrefUtil(): SharedPreferenceUtil
@ -93,6 +96,7 @@ interface CoreComponent {
fun newLanguagesDao(): NewLanguagesDao fun newLanguagesDao(): NewLanguagesDao
fun recentSearchDao(): NewRecentSearchDao fun recentSearchDao(): NewRecentSearchDao
fun recentSearchRoomDao(): NewRecentSearchRoomDao fun recentSearchRoomDao(): NewRecentSearchRoomDao
fun historyRoomDao(): HistoryRoomDao
fun newBookmarksDao(): NewBookmarksDao fun newBookmarksDao(): NewBookmarksDao
fun connectivityManager(): ConnectivityManager fun connectivityManager(): ConnectivityManager
fun context(): Context fun context(): Context

View File

@ -25,10 +25,12 @@ import android.net.ConnectivityManager
import android.os.storage.StorageManager import android.os.storage.StorageManager
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.objectbox.BoxStore
import io.reactivex.Scheduler import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import org.kiwix.kiwixmobile.core.NightModeConfig import org.kiwix.kiwixmobile.core.NightModeConfig
import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase
import org.kiwix.kiwixmobile.core.di.qualifiers.Computation import org.kiwix.kiwixmobile.core.di.qualifiers.Computation
import org.kiwix.kiwixmobile.core.di.qualifiers.IO import org.kiwix.kiwixmobile.core.di.qualifiers.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
@ -96,4 +98,15 @@ class ApplicationModule {
@Singleton @Singleton
fun provideConnectivityManager(context: Context): ConnectivityManager = fun provideConnectivityManager(context: Context): ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@Singleton
@Provides
fun provideYourDatabase(
context: Context,
boxStore: BoxStore
) =
KiwixRoomDatabase.getInstance(
context = context,
boxStore
)
} }

View File

@ -48,6 +48,8 @@ open class DatabaseModule {
return boxStore!! return boxStore!!
} }
// The reason we can construct a database for the repo
@Provides @Singleton fun providesNewBookDao(boxStore: BoxStore): NewBookDao = @Provides @Singleton fun providesNewBookDao(boxStore: BoxStore): NewBookDao =
NewBookDao(boxStore.boxFor()) NewBookDao(boxStore.boxFor())
@ -74,18 +76,22 @@ open class DatabaseModule {
): FetchDownloadDao = ): FetchDownloadDao =
FetchDownloadDao(boxStore.boxFor(), newBookDao) FetchDownloadDao(boxStore.boxFor(), newBookDao)
@Singleton // @Singleton
@Provides // @Provides
fun provideYourDatabase( // fun provideYourDatabase(
context: Context, // context: Context,
boxStore: BoxStore // boxStore: BoxStore
) = // ) =
KiwixRoomDatabase.getInstance( // KiwixRoomDatabase.getInstance(
context = context, // context = context,
boxStore // boxStore
) // The reason we can construct a database for the repo // ) // The reason we can construct a database for the repo
@Singleton @Singleton
@Provides @Provides
fun provideNewRecentSearchRoomDao(db: KiwixRoomDatabase) = db.newRecentSearchRoomDao() fun provideNewRecentSearchRoomDao(db: KiwixRoomDatabase) = db.newRecentSearchRoomDao()
@Provides
@Singleton
fun provideHistoryDao(db: KiwixRoomDatabase) = db.historyRoomDao()
} }

View File

@ -61,7 +61,7 @@ class BookmarkViewModel @Inject constructor(
state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) })
override fun createDeletePageDialogEffect(state: BookmarkState) = override fun createDeletePageDialogEffect(state: BookmarkState) =
ShowDeleteBookmarksDialog(effects, state, pageDao) ShowDeleteBookmarksDialog(effects, state, basePageDao)
override fun copyWithNewItems(state: BookmarkState, newItems: List<BookmarkItem>): BookmarkState = override fun copyWithNewItems(state: BookmarkState, newItems: List<BookmarkItem>): BookmarkState =
state.copy(pageItems = newItems) state.copy(pageItems = newItems)

View File

@ -21,7 +21,7 @@ package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import io.reactivex.processors.PublishProcessor import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect 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.extensions.ActivityExtensions.cachedComponent
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem
import org.kiwix.kiwixmobile.core.page.viewmodel.PageState import org.kiwix.kiwixmobile.core.page.viewmodel.PageState
@ -34,14 +34,14 @@ import javax.inject.Inject
data class ShowDeleteBookmarksDialog( data class ShowDeleteBookmarksDialog(
private val effects: PublishProcessor<SideEffect<*>>, private val effects: PublishProcessor<SideEffect<*>>,
private val state: PageState<BookmarkItem>, private val state: PageState<BookmarkItem>,
private val pageDao: PageDao private val basePageDao: BasePageDao
) : SideEffect<Unit> { ) : SideEffect<Unit> {
@Inject lateinit var dialogShower: DialogShower @Inject lateinit var dialogShower: DialogShower
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
activity.cachedComponent.inject(this) activity.cachedComponent.inject(this)
dialogShower.show( dialogShower.show(
if (state.isInSelectionState) DeleteSelectedBookmarks else DeleteAllBookmarks, if (state.isInSelectionState) DeleteSelectedBookmarks else DeleteAllBookmarks,
{ effects.offer(DeletePageItems(state, pageDao)) } { effects.offer(DeletePageItems(state, basePageDao)) }
) )
} }
} }

View File

@ -17,13 +17,15 @@
*/ */
package org.kiwix.kiwixmobile.core.page.history.adapter package org.kiwix.kiwixmobile.core.page.history.adapter
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity
import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity
import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.adapter.PageRelated import org.kiwix.kiwixmobile.core.page.adapter.PageRelated
import org.kiwix.kiwixmobile.core.reader.ZimFileReader import org.kiwix.kiwixmobile.core.reader.ZimFileReader
sealed class HistoryListItem : PageRelated { sealed class HistoryListItem : PageRelated {
data class HistoryItem constructor( data class HistoryItem constructor(
val databaseId: Long = 0L, val databaseId: Long = 0L,
override val zimId: String, override val zimId: String,
@ -68,6 +70,19 @@ sealed class HistoryListItem : PageRelated {
historyEntity.timeStamp, historyEntity.timeStamp,
false false
) )
constructor(historyRoomEntity: HistoryRoomEntity) : this(
historyRoomEntity.id,
historyRoomEntity.zimId,
historyRoomEntity.zimName,
historyRoomEntity.zimFilePath,
historyRoomEntity.favicon,
historyRoomEntity.historyUrl,
historyRoomEntity.historyTitle,
historyRoomEntity.dateString,
historyRoomEntity.timeStamp,
false
)
} }
data class DateItem( data class DateItem(
@ -75,3 +90,71 @@ sealed class HistoryListItem : PageRelated {
override val id: Long = dateString.hashCode().toLong() override val id: Long = dateString.hashCode().toLong()
) : HistoryListItem() ) : HistoryListItem()
} }
// @Entity
// sealed class HistoryListRoomItem : PageRelated {
// @Entity
// data class HistoryItem constructor(
// val databaseId: Long = 0L,
// override val zimId: String,
// val zimName: String,
// override val zimFilePath: String,
// override val favicon: String?,
// val historyUrl: String,
// override val title: String,
// val dateString: String,
// val timeStamp: Long,
// override var isSelected: Boolean = false,
// override val id: Long = databaseId,
// override val url: String = historyUrl
// ) : HistoryListItem(), Page {
//
// constructor(
// url: String,
// title: String,
// dateString: String,
// timeStamp: Long,
// zimFileReader: ZimFileReader
// ) : this(
// zimId = zimFileReader.id,
// zimName = zimFileReader.name,
// zimFilePath = zimFileReader.zimFile.canonicalPath,
// favicon = zimFileReader.favicon,
// historyUrl = url,
// title = title,
// dateString = dateString,
// timeStamp = timeStamp
// )
//
// constructor(historyEntity: HistoryEntity) : this(
// historyEntity.id,
// historyEntity.zimId,
// historyEntity.zimName,
// historyEntity.zimFilePath,
// historyEntity.favicon,
// historyEntity.historyUrl,
// historyEntity.historyTitle,
// historyEntity.dateString,
// historyEntity.timeStamp,
// false
// )
//
// constructor(historyRoomEntity: HistoryRoomEntity) : this(
// historyRoomEntity.id,
// historyRoomEntity.zimId,
// historyRoomEntity.zimName,
// historyRoomEntity.zimFilePath,
// historyRoomEntity.favicon,
// historyRoomEntity.historyUrl,
// historyRoomEntity.historyTitle,
// historyRoomEntity.dateString,
// historyRoomEntity.timeStamp,
// false
// )
// }
//
// data class DateItem(
// val dateString: String,
// override val id: Long = dateString.hashCode().toLong()
// ) : HistoryListItem()
// }

View File

@ -18,7 +18,7 @@
package org.kiwix.kiwixmobile.core.page.history.viewmodel package org.kiwix.kiwixmobile.core.page.history.viewmodel
import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog
import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.UpdateAllHistoryPreference import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.UpdateAllHistoryPreference
@ -29,10 +29,10 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject import javax.inject.Inject
class HistoryViewModel @Inject constructor( class HistoryViewModel @Inject constructor(
historyDao: HistoryDao, historyRoomDao: HistoryRoomDao,
zimReaderContainer: ZimReaderContainer, zimReaderContainer: ZimReaderContainer,
sharedPrefs: SharedPreferenceUtil sharedPrefs: SharedPreferenceUtil
) : PageViewModel<HistoryItem, HistoryState>(historyDao, sharedPrefs, zimReaderContainer) { ) : PageViewModel<HistoryItem, HistoryState>(historyRoomDao, sharedPrefs, zimReaderContainer) {
override fun initialState(): HistoryState = override fun initialState(): HistoryState =
HistoryState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) HistoryState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
@ -58,7 +58,7 @@ class HistoryViewModel @Inject constructor(
} }
override fun createDeletePageDialogEffect(state: HistoryState) = override fun createDeletePageDialogEffect(state: HistoryState) =
ShowDeleteHistoryDialog(effects, state, pageDao) ShowDeleteHistoryDialog(effects, state, basePageDao)
override fun deselectAllPages(state: HistoryState): HistoryState = override fun deselectAllPages(state: HistoryState): HistoryState =
state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) })

View File

@ -21,7 +21,7 @@ package org.kiwix.kiwixmobile.core.page.history.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import io.reactivex.processors.PublishProcessor import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect 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.extensions.ActivityExtensions.cachedComponent
import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState
import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems
@ -33,13 +33,13 @@ import javax.inject.Inject
data class ShowDeleteHistoryDialog( data class ShowDeleteHistoryDialog(
private val effects: PublishProcessor<SideEffect<*>>, private val effects: PublishProcessor<SideEffect<*>>,
private val state: HistoryState, private val state: HistoryState,
private val pageDao: PageDao private val basePageDao: BasePageDao
) : SideEffect<Unit> { ) : SideEffect<Unit> {
@Inject lateinit var dialogShower: DialogShower @Inject lateinit var dialogShower: DialogShower
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
activity.cachedComponent.inject(this) activity.cachedComponent.inject(this)
dialogShower.show(if (state.isInSelectionState) DeleteSelectedHistory else DeleteAllHistory, { dialogShower.show(if (state.isInSelectionState) DeleteSelectedHistory else DeleteAllHistory, {
effects.offer(DeletePageItems(state, pageDao)) effects.offer(DeletePageItems(state, basePageDao))
}) })
} }
} }

View File

@ -66,7 +66,7 @@ class NotesViewModel @Inject constructor(
state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) })
override fun createDeletePageDialogEffect(state: NotesState) = override fun createDeletePageDialogEffect(state: NotesState) =
ShowDeleteNotesDialog(effects, state, pageDao) ShowDeleteNotesDialog(effects, state, basePageDao)
override fun onItemClick(page: Page) = override fun onItemClick(page: Page) =
ShowOpenNoteDialog(effects, page, zimReaderContainer) ShowOpenNoteDialog(effects, page, zimReaderContainer)

View File

@ -22,7 +22,7 @@ import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import io.reactivex.processors.PublishProcessor import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect 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.extensions.ActivityExtensions.cachedComponent
import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesState import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesState
import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems
@ -34,7 +34,7 @@ import javax.inject.Inject
data class ShowDeleteNotesDialog( data class ShowDeleteNotesDialog(
private val effects: PublishProcessor<SideEffect<*>>, private val effects: PublishProcessor<SideEffect<*>>,
private val state: NotesState, private val state: NotesState,
private val pageDao: PageDao private val basePageDao: BasePageDao
) : SideEffect<Unit> { ) : SideEffect<Unit> {
@Inject lateinit var dialogShower: DialogShower @Inject lateinit var dialogShower: DialogShower
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
@ -43,7 +43,7 @@ data class ShowDeleteNotesDialog(
dialogShower.show( dialogShower.show(
if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes, if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes,
{ {
effects.offer(DeletePageItems(state, pageDao)) effects.offer(DeletePageItems(state, basePageDao))
} }
) )
} }

View File

@ -20,12 +20,17 @@ package org.kiwix.kiwixmobile.core.page.viewmodel
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.processors.PublishProcessor import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.base.SideEffect 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.PageDao
import org.kiwix.kiwixmobile.core.dao.PageRoomDao
import org.kiwix.kiwixmobile.core.page.adapter.Page 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.Exit
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu
@ -42,7 +47,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
abstract class PageViewModel<T : Page, S : PageState<T>>( abstract class PageViewModel<T : Page, S : PageState<T>>(
protected val pageDao: PageDao, protected val basePageDao: BasePageDao,
val sharedPreferenceUtil: SharedPreferenceUtil, val sharedPreferenceUtil: SharedPreferenceUtil,
val zimReaderContainer: ZimReaderContainer val zimReaderContainer: ZimReaderContainer
) : ViewModel() { ) : ViewModel() {
@ -70,11 +75,25 @@ abstract class PageViewModel<T : Page, S : PageState<T>>(
.subscribe(state::postValue, Throwable::printStackTrace) .subscribe(state::postValue, Throwable::printStackTrace)
protected fun addDisposablesToCompositeDisposable() { protected fun addDisposablesToCompositeDisposable() {
compositeDisposable.addAll( when (basePageDao) {
viewStateReducer(), is PageDao -> {
pageDao.pages().subscribeOn(Schedulers.io()) compositeDisposable.addAll(
.subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace) viewStateReducer(),
) basePageDao.pages().subscribeOn(Schedulers.io())
.subscribe({ actions.offer(UpdatePages(it)) }, Throwable::printStackTrace)
)
}
is PageRoomDao -> {
viewModelScope.launch {
try {
// basePageDao.pages().collect(::UpdatePages)
} catch (exception: Exception) {
exception.printStackTrace()
}
}
}
}
} }
private fun reduce(action: Action, state: S): S = when (action) { private fun reduce(action: Action, state: S): S = when (action) {

View File

@ -20,19 +20,19 @@ package org.kiwix.kiwixmobile.core.page.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import org.kiwix.kiwixmobile.core.base.SideEffect 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.adapter.Page
import org.kiwix.kiwixmobile.core.page.viewmodel.PageState import org.kiwix.kiwixmobile.core.page.viewmodel.PageState
data class DeletePageItems( data class DeletePageItems(
private val state: PageState<*>, private val state: PageState<*>,
private val pageDao: PageDao private val basePageDao: BasePageDao
) : SideEffect<Unit> { ) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
if (state.isInSelectionState) { if (state.isInSelectionState) {
pageDao.deletePages(state.pageItems.filter(Page::isSelected)) basePageDao.deletePages(state.pageItems.filter(Page::isSelected))
} else { } else {
pageDao.deletePages(state.pageItems) basePageDao.deletePages(state.pageItems)
} }
} }
} }