diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt new file mode 100644 index 000000000..5251ec065 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryRoomDao.kt @@ -0,0 +1,91 @@ +/* + * Kiwix Android + * Copyright (c) 2024 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.Delete +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> + + fun history(): Flow> = historyRoomEntity().map { + it.map(HistoryListItem::HistoryItem) + } + + override fun pages() = history() + override fun deletePages(pagesToDelete: List) = + deleteHistory(pagesToDelete as List) + + @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) + + @Query("DELETE FROM HistoryRoomEntity") + abstract fun deleteAllHistory() + + fun migrationToRoomHistory( + box: Box + ) { + 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) +} 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/HistoryRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt new file mode 100644 index 000000000..5a8337d25 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/HistoryRoomEntity.kt @@ -0,0 +1,48 @@ +/* + * Kiwix Android + * Copyright (c) 2024 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.PrimaryKey +import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem + +@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: HistoryItem) : this( + historyItem.databaseId, + historyItem.zimId, + historyItem.zimName, + historyItem.zimFilePath, + historyItem.favicon, + historyItem.historyUrl, + historyItem.title, + historyItem.dateString, + historyItem.timeStamp + ) +} 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 d16ad1696..737fb3c6f 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,11 +22,15 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +import androidx.room.TypeConverters +import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao +import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity @Suppress("UnnecessaryAbstractClass") -@Database(entities = [RecentSearchRoomEntity::class], version = 1) +@Database(entities = [RecentSearchRoomEntity::class, HistoryRoomEntity::class], version = 2) +@TypeConverters(HistoryRoomDaoCoverts::class) abstract class KiwixRoomDatabase : RoomDatabase() { abstract fun recentSearchRoomDao(): RecentSearchRoomDao diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt index e74b228b7..a51ad97a4 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.core.page.history.adapter 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.PageRelated import org.kiwix.kiwixmobile.core.reader.ZimFileReader @@ -70,6 +71,19 @@ sealed class HistoryListItem : PageRelated { 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(