Created HistoryRoomDao, and HistoryRoomEntity for saving the data in room database.

This commit is contained in:
MohitMaliFtechiz 2024-05-30 11:12:31 +05:30
parent 09c482102f
commit e32e3e6371
5 changed files with 169 additions and 3 deletions

View File

@ -0,0 +1,91 @@
/*
* 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.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<List<HistoryRoomEntity>>
fun history(): Flow<List<Page>> = historyRoomEntity().map {
it.map(HistoryListItem::HistoryItem)
}
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,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,48 @@
/*
* 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.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
)
}

View File

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

View File

@ -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(