Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving and Restoring the Web View Navigation History Across Sessions #3996

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class HistoryRoomDao : PageDao {
historyItem.dateString
)?.let {
it.apply {
// update the exiting entity
// update the existing entity
historyUrl = historyItem.historyUrl
historyTitle = historyItem.title
timeStamp = historyItem.timeStamp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.Insert
import androidx.room.Query
import io.reactivex.Flowable
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity

@Dao
abstract class WebViewHistoryRoomDao {
@Insert
abstract fun insertWebViewPageHistoryItem(webViewHistoryEntity: WebViewHistoryEntity)

@Query("SELECT * FROM WebViewHistoryEntity ORDER BY isForward ASC,timestamp ASC")
abstract fun getAllWebViewPagesHistory(): Flowable<List<WebViewHistoryEntity>>

@Query("Delete from WebViewHistoryEntity")
abstract fun clearWebViewPagesHistory()

fun clearPageHistoryWithPrimaryKey() {
clearWebViewPagesHistory()
}

@Query("DELETE FROM sqlite_sequence WHERE name='PageHistoryRoomEntity'")
abstract fun resetPrimaryKey()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.WebViewHistoryItem

@Entity
data class WebViewHistoryEntity(
@PrimaryKey(autoGenerate = true) var id: Long = 0L,
val zimId: String,
val title: String,
val pageUrl: String,
val isForward: Boolean = false,
val timeStamp: Long
) {
constructor(webViewHistoryItem: WebViewHistoryItem) : this(
webViewHistoryItem.databaseId,
webViewHistoryItem.zimId,
webViewHistoryItem.title,
webViewHistoryItem.pageUrl,
webViewHistoryItem.isForward,
webViewHistoryItem.timeStamp
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ package org.kiwix.kiwixmobile.core.data
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Single
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import org.kiwix.kiwixmobile.core.zim_manager.Language
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
Expand Down Expand Up @@ -51,4 +53,8 @@ interface DataSource {
fun saveNote(noteListItem: NoteListItem): Completable
fun deleteNote(noteTitle: String): Completable
fun deleteNotes(noteList: List<NoteListItem>): Completable

fun insertWebViewHistoryItem(pageHistory: WebViewHistoryItem): Completable
fun getAllWebViewPagesHistory(): Single<List<WebViewHistoryEntity>>
fun clearWebViewPagesHistory(): Completable
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.HistoryRoomDaoCoverts
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.WebViewHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.DownloadRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.HistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.ZimSourceRoomConverter

Expand All @@ -42,9 +44,10 @@ import org.kiwix.kiwixmobile.core.dao.entities.ZimSourceRoomConverter
RecentSearchRoomEntity::class,
HistoryRoomEntity::class,
NotesRoomEntity::class,
DownloadRoomEntity::class
DownloadRoomEntity::class,
WebViewHistoryEntity::class
],
version = 5,
version = 7,
exportSchema = false
)
@TypeConverters(HistoryRoomDaoCoverts::class, ZimSourceRoomConverter::class)
Expand All @@ -53,6 +56,7 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun historyRoomDao(): HistoryRoomDao
abstract fun notesRoomDao(): NotesRoomDao
abstract fun downloadRoomDao(): DownloadRoomDao
abstract fun pageHistoryRoomDao(): WebViewHistoryRoomDao

companion object {
private var db: KiwixRoomDatabase? = null
Expand All @@ -62,7 +66,14 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
?: Room.databaseBuilder(context, KiwixRoomDatabase::class.java, "KiwixRoom.db")
// We have already database name called kiwix.db in order to avoid complexity we named
// as kiwixRoom.db
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
.addMigrations(
MIGRATION_1_2,
MIGRATION_2_3,
MIGRATION_3_4,
MIGRATION_4_5,
MIGRATION_5_6,
MIGRATION_6_7
)
.build().also { db = it }
}
}
Expand Down Expand Up @@ -202,6 +213,43 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
}
}

@Suppress("MagicNumber")
private val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `PageHistoryRoomEntity` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`zimId` TEXT NOT NULL,
`title` TEXT NOT NULL,
`pageUrl` TEXT NOT NULL,
`isForward` INTEGER NOT NULL DEFAULT 0
`timeStamp` INTEGER NOT NULL
)
"""
)
}
}

@Suppress("MagicNumber")
private val MIGRATION_6_7 = object : Migration(6, 7) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE PageHistoryRoomEntity")
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `WebViewHistoryEntity` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`zimId` TEXT NOT NULL,
`title` TEXT NOT NULL,
`pageUrl` TEXT NOT NULL,
`isForward` INTEGER NOT NULL DEFAULT 0,
`timeStamp` INTEGER NOT NULL
)
"""
)
}
}

fun destroyInstance() {
db = null
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.core.dao.NotesRoomDao
import org.kiwix.kiwixmobile.core.dao.WebViewHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
import org.kiwix.kiwixmobile.core.extensions.HeaderizableList
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.zim_manager.Language
Expand All @@ -55,6 +58,7 @@ class Repository @Inject internal constructor(
private val bookDao: NewBookDao,
private val libkiwixBookmarks: LibkiwixBookmarks,
private val historyRoomDao: HistoryRoomDao,
private val webViewHistoryRoomDao: WebViewHistoryRoomDao,
private val notesRoomDao: NotesRoomDao,
private val languageDao: NewLanguagesDao,
private val recentSearchRoomDao: RecentSearchRoomDao,
Expand Down Expand Up @@ -144,6 +148,24 @@ class Repository @Inject internal constructor(
Completable.fromAction { notesRoomDao.deleteNotes(noteList) }
.subscribeOn(io)

override fun insertWebViewHistoryItem(pageHistory: WebViewHistoryItem): Completable =
Completable.fromAction {
webViewHistoryRoomDao.insertWebViewPageHistoryItem(
WebViewHistoryEntity(pageHistory)
)
}
.subscribeOn(io)

override fun getAllWebViewPagesHistory() =
webViewHistoryRoomDao.getAllWebViewPagesHistory()
.first(emptyList())
.subscribeOn(io)
.observeOn(mainThread)

override fun clearWebViewPagesHistory(): Completable =
Completable.fromAction(webViewHistoryRoomDao::clearPageHistoryWithPrimaryKey)
.subscribeOn(io)

override fun deleteNote(noteTitle: String): Completable =
Completable.fromAction { notesRoomDao.deleteNote(noteTitle) }
.subscribeOn(io)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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.WebViewHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.data.DataModule
import org.kiwix.kiwixmobile.core.data.DataSource
Expand Down Expand Up @@ -109,6 +110,7 @@ interface CoreComponent {
fun libkiwixBookmarks(): LibkiwixBookmarks
fun recentSearchRoomDao(): RecentSearchRoomDao
fun historyRoomDao(): HistoryRoomDao
fun pageHistoryRoomDao(): WebViewHistoryRoomDao
fun noteRoomDao(): NotesRoomDao
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
fun context(): Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ open class DatabaseModule {
@Singleton
fun provideHistoryDao(db: KiwixRoomDatabase) = db.historyRoomDao()

@Provides
@Singleton
fun providePageHistoryRoomDao(db: KiwixRoomDatabase) = db.pageHistoryRoomDao()

@Singleton
@Provides
fun provideNoteRoomDao(db: KiwixRoomDatabase) = db.notesRoomDao()
Expand Down
Loading