Naming convention improved

This commit is contained in:
MohitMaliFtechiz 2024-10-22 14:22:05 +05:30
parent 775d9439f3
commit b81e4b5c97
9 changed files with 92 additions and 99 deletions

View File

@ -22,21 +22,21 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import io.reactivex.Flowable
import org.kiwix.kiwixmobile.core.dao.entities.PageHistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
@Dao
abstract class PageHistoryRoomDao {
abstract class WebViewHistoryRoomDao {
@Insert
abstract fun insertPageHistoryItem(pageHistoryRoomEntity: PageHistoryRoomEntity)
abstract fun insertWebViewPageHistoryItem(webViewHistoryEntity: WebViewHistoryEntity)
@Query("SELECT * FROM PageHistoryRoomEntity ORDER BY isForward ASC,timestamp ASC")
abstract fun getAllPageHistory(): Flowable<List<PageHistoryRoomEntity>>
@Query("SELECT * FROM WebViewHistoryEntity ORDER BY isForward ASC,timestamp ASC")
abstract fun getAllWebViewPagesHistory(): Flowable<List<WebViewHistoryEntity>>
@Query("Delete from PageHistoryRoomEntity")
abstract fun clearPageHistory()
@Query("Delete from WebViewHistoryEntity")
abstract fun clearWebViewPagesHistory()
fun clearPageHistoryWithPrimaryKey() {
clearPageHistory()
clearWebViewPagesHistory()
}
@Query("DELETE FROM sqlite_sequence WHERE name='PageHistoryRoomEntity'")

View File

@ -20,10 +20,10 @@ package org.kiwix.kiwixmobile.core.dao.entities
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.kiwix.kiwixmobile.core.page.history.adapter.PageHistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
@Entity
data class PageHistoryRoomEntity(
data class WebViewHistoryEntity(
@PrimaryKey(autoGenerate = true) var id: Long = 0L,
val zimId: String,
val title: String,
@ -31,12 +31,12 @@ data class PageHistoryRoomEntity(
val isForward: Boolean = false,
val timeStamp: Long
) {
constructor(pageHistoryItem: PageHistoryItem) : this(
pageHistoryItem.databaseId,
pageHistoryItem.zimId,
pageHistoryItem.title,
pageHistoryItem.pageUrl,
pageHistoryItem.isForward,
pageHistoryItem.timeStamp
constructor(webViewHistoryItem: WebViewHistoryItem) : this(
webViewHistoryItem.databaseId,
webViewHistoryItem.zimId,
webViewHistoryItem.title,
webViewHistoryItem.pageUrl,
webViewHistoryItem.isForward,
webViewHistoryItem.timeStamp
)
}

View File

@ -20,11 +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.PageHistoryRoomEntity
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.PageHistoryItem
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
@ -56,7 +56,7 @@ interface DataSource {
fun deleteNote(noteTitle: String): Completable
fun deleteNotes(noteList: List<NoteListItem>): Completable
fun insertPageHistoryItem(pageHistory: PageHistoryItem): Completable
fun getAllPageHistory(): Single<List<PageHistoryRoomEntity>>
fun clearPageHistory(): Completable
fun insertWebViewHistoryItem(pageHistory: WebViewHistoryItem): Completable
fun getAllWebViewPagesHistory(): Single<List<WebViewHistoryEntity>>
fun clearWebViewPagesHistory(): Completable
}

View File

@ -29,13 +29,13 @@ 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.PageHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.WebViewHistoryRoomDao
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.PageHistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
import org.kiwix.kiwixmobile.core.dao.entities.ZimSourceRoomConverter
@Suppress("UnnecessaryAbstractClass")
@ -45,7 +45,7 @@ import org.kiwix.kiwixmobile.core.dao.entities.ZimSourceRoomConverter
HistoryRoomEntity::class,
NotesRoomEntity::class,
DownloadRoomEntity::class,
PageHistoryRoomEntity::class
WebViewHistoryEntity::class
],
version = 8,
exportSchema = false
@ -56,7 +56,7 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun historyRoomDao(): HistoryRoomDao
abstract fun notesRoomDao(): NotesRoomDao
abstract fun downloadRoomDao(): DownloadRoomDao
abstract fun pageHistoryRoomDao(): PageHistoryRoomDao
abstract fun pageHistoryRoomDao(): WebViewHistoryRoomDao
companion object {
private var db: KiwixRoomDatabase? = null
@ -279,14 +279,15 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
@Suppress("MagicNumber")
private val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE PageHistoryRoomEntity")
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `PageHistoryRoomEntity` (
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
`isForward` INTEGER NOT NULL DEFAULT 0,
`timeStamp` INTEGER NOT NULL
)
"""

View File

@ -28,16 +28,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.PageHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.WebViewHistoryRoomDao
import org.kiwix.kiwixmobile.core.dao.RecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.PageHistoryRoomEntity
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.PageHistoryItem
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
@ -58,7 +58,7 @@ class Repository @Inject internal constructor(
private val bookDao: NewBookDao,
private val libkiwixBookmarks: LibkiwixBookmarks,
private val historyRoomDao: HistoryRoomDao,
private val pageHistoryRoomDao: PageHistoryRoomDao,
private val webViewHistoryRoomDao: WebViewHistoryRoomDao,
private val notesRoomDao: NotesRoomDao,
private val languageDao: NewLanguagesDao,
private val recentSearchRoomDao: RecentSearchRoomDao,
@ -148,22 +148,22 @@ class Repository @Inject internal constructor(
Completable.fromAction { notesRoomDao.deleteNotes(noteList) }
.subscribeOn(ioThread)
override fun insertPageHistoryItem(pageHistory: PageHistoryItem): Completable =
override fun insertWebViewHistoryItem(pageHistory: WebViewHistoryItem): Completable =
Completable.fromAction {
pageHistoryRoomDao.insertPageHistoryItem(
PageHistoryRoomEntity(pageHistory)
webViewHistoryRoomDao.insertWebViewPageHistoryItem(
WebViewHistoryEntity(pageHistory)
)
}
.subscribeOn(ioThread)
override fun getAllPageHistory() =
pageHistoryRoomDao.getAllPageHistory()
override fun getAllWebViewPagesHistory() =
webViewHistoryRoomDao.getAllWebViewPagesHistory()
.first(emptyList())
.subscribeOn(ioThread)
.observeOn(mainThread)
override fun clearPageHistory(): Completable =
Completable.fromAction(pageHistoryRoomDao::clearPageHistoryWithPrimaryKey)
override fun clearWebViewPagesHistory(): Completable =
Completable.fromAction(webViewHistoryRoomDao::clearPageHistoryWithPrimaryKey)
.subscribeOn(ioThread)
override fun deleteNote(noteTitle: String): Completable =

View File

@ -37,7 +37,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.PageHistoryRoomDao
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
@ -106,7 +106,7 @@ interface CoreComponent {
fun libkiwixBookmarks(): LibkiwixBookmarks
fun recentSearchRoomDao(): RecentSearchRoomDao
fun historyRoomDao(): HistoryRoomDao
fun pageHistoryRoomDao(): PageHistoryRoomDao
fun pageHistoryRoomDao(): WebViewHistoryRoomDao
fun noteRoomDao(): NotesRoomDao
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
fun context(): Context

View File

@ -136,7 +136,7 @@ import org.kiwix.kiwixmobile.core.page.history.NavigationHistoryDialog
import org.kiwix.kiwixmobile.core.page.history.adapter.DataCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.NavigationHistoryListItem
import org.kiwix.kiwixmobile.core.page.history.adapter.PageHistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudCallbacks
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_PAUSE_OR_RESUME_TTS
@ -991,7 +991,7 @@ abstract class CoreReaderFragment :
override fun clearHistory() {
getCurrentWebView()?.clearHistory()
repositoryActions?.clearPageHistory()
repositoryActions?.clearWebViewPagesHistory()
updateBottomToolbarArrowsAlpha()
toast(R.string.navigation_history_cleared)
}
@ -1975,23 +1975,19 @@ abstract class CoreReaderFragment :
}
@Suppress("MagicNumber")
private fun loadWebViewHistory(pageHistory: List<PageHistoryItem>) {
private fun loadWebViewHistory(pageHistory: List<WebViewHistoryItem>) {
val backStack = pageHistory.filter { !it.isForward }
val forwardStack = pageHistory.filter(PageHistoryItem::isForward)
val forwardStack = pageHistory.filter(WebViewHistoryItem::isForward)
val currentWebView = getCurrentWebView() ?: return
val currentPageUrl = currentWebView.copyBackForwardList().currentItem?.url
val currentZimId = zimReaderContainer?.zimFileReader?.id
// If backStack and forwardStack are empty, return
if (backStack.isEmpty() && forwardStack.isEmpty()) {
if (backStack.isEmpty() && forwardStack.isEmpty() || currentZimId != pageHistory[0].zimId) {
repositoryActions?.clearWebViewPagesHistory()
return
}
// Debug info: Toast to check the size of back and forward stack
activity.toast(
"${pageHistory.size} items loaded. Back stack size: ${backStack.size}",
Toast.LENGTH_LONG
)
if (backStack.isNotEmpty()) {
// Step 1: Load the first item immediately (0th index)
currentWebView.loadUrl(backStack[0].pageUrl)
@ -2037,7 +2033,7 @@ abstract class CoreReaderFragment :
) // Initial delay to allow the first page to load
}
override fun onDataFetched(pageHistory: List<PageHistoryItem>) {
override fun onDataFetched(pageHistory: List<WebViewHistoryItem>) {
loadWebViewHistory(pageHistory)
}
@ -2047,7 +2043,7 @@ abstract class CoreReaderFragment :
override fun onResume() {
super.onResume()
repositoryActions?.loadPageHistory(this)
repositoryActions?.loadWebViewPagesHistory(this)
updateBottomToolbarVisibility()
updateNightMode()
if (tts == null) {
@ -2448,43 +2444,39 @@ abstract class CoreReaderFragment :
if (currentIndex != null) {
// Save BackStack
webBackForwardList.let { historyList ->
(0 until historyList.currentIndex)
.asSequence()
.forEach {
val historyItem = webBackForwardList.getItemAtIndex(it)
val pageHistory = PageHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = false,
timeStamp = it.toLong()
)
repositoryActions?.savePageHistory(pageHistory)
}
for (index in 0 until historyList.currentIndex) {
val historyItem = historyList.getItemAtIndex(index)
val pageHistory = WebViewHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = false,
timeStamp = index.toLong()
)
repositoryActions?.saveWebViewPageHistory(pageHistory)
}
}
// Save ForwardStack
webBackForwardList.let { historyList ->
(historyList.currentIndex + 1 until webBackForwardList.size)
.asSequence()
.forEach {
val historyItem = webBackForwardList.getItemAtIndex(it)
val pageHistory = PageHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = true,
timeStamp = it.toLong()
)
repositoryActions?.savePageHistory(pageHistory)
}
for (index in (historyList.currentIndex + 1) until historyList.size) {
val historyItem = historyList.getItemAtIndex(index)
val pageHistory = WebViewHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = true,
timeStamp = index.toLong()
)
repositoryActions?.saveWebViewPageHistory(pageHistory)
}
}
}
}
override fun onPause() {
super.onPause()
repositoryActions?.clearPageHistory()
repositoryActions?.clearWebViewPagesHistory()
saveWebBackForwardListToRoom()
saveTabStates()
Log.d(

View File

@ -23,7 +23,7 @@ import org.kiwix.kiwixmobile.core.di.ActivityScope
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.page.history.adapter.DataCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.PageHistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
@ -73,20 +73,20 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour
.subscribe({}, { e -> Log.e(TAG, "Unable to save book", e) })
}
fun savePageHistory(pageHistory: PageHistoryItem) {
savePageHistoryDisposable = dataSource.insertPageHistoryItem(pageHistory)
fun saveWebViewPageHistory(pageHistory: WebViewHistoryItem) {
savePageHistoryDisposable = dataSource.insertWebViewHistoryItem(pageHistory)
.subscribe({}, { e -> Log.e(TAG, "Unable to save page history", e) })
}
fun clearPageHistory() {
clearPageHistoryDisposable = dataSource.clearPageHistory()
fun clearWebViewPagesHistory() {
clearPageHistoryDisposable = dataSource.clearWebViewPagesHistory()
.subscribe({}, { e -> Log.e(TAG, "Unable to clear page history", e) })
}
fun loadPageHistory(callBack: DataCallback) {
getPageHistoryDisposable = dataSource.getAllPageHistory()
fun loadWebViewPagesHistory(callBack: DataCallback) {
getPageHistoryDisposable = dataSource.getAllWebViewPagesHistory()
.map { roomEntities ->
roomEntities.map(::PageHistoryItem)
roomEntities.map(::WebViewHistoryItem)
}
.subscribe(callBack::onDataFetched) { e -> Log.e(TAG, "Unable to load page history", e) }
}

View File

@ -18,9 +18,9 @@
package org.kiwix.kiwixmobile.core.page.history.adapter
import org.kiwix.kiwixmobile.core.dao.entities.PageHistoryRoomEntity
import org.kiwix.kiwixmobile.core.dao.entities.WebViewHistoryEntity
data class PageHistoryItem(
data class WebViewHistoryItem(
val databaseId: Long = 0L,
val zimId: String,
val title: String,
@ -43,17 +43,17 @@ data class PageHistoryItem(
timeStamp
)
constructor(pageHistoryRoomEntity: PageHistoryRoomEntity) : this(
pageHistoryRoomEntity.id,
pageHistoryRoomEntity.zimId,
pageHistoryRoomEntity.title,
pageHistoryRoomEntity.pageUrl,
pageHistoryRoomEntity.isForward,
pageHistoryRoomEntity.timeStamp
constructor(webViewHistoryEntity: WebViewHistoryEntity) : this(
webViewHistoryEntity.id,
webViewHistoryEntity.zimId,
webViewHistoryEntity.title,
webViewHistoryEntity.pageUrl,
webViewHistoryEntity.isForward,
webViewHistoryEntity.timeStamp
)
}
interface DataCallback {
fun onDataFetched(pageHistory: List<PageHistoryItem>)
fun onDataFetched(pageHistory: List<WebViewHistoryItem>)
fun onError(error: Throwable)
}