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 667ffc309c..16a3998779 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 @@ -56,7 +56,7 @@ abstract class KiwixRoomDatabase : RoomDatabase() { abstract fun historyRoomDao(): HistoryRoomDao abstract fun notesRoomDao(): NotesRoomDao abstract fun downloadRoomDao(): DownloadRoomDao - abstract fun pageHistoryRoomDao(): WebViewHistoryRoomDao + abstract fun webViewHistoryRoomDao(): WebViewHistoryRoomDao companion object { private var db: KiwixRoomDatabase? = null diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index 8656965ca1..08246d4d97 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -108,7 +108,7 @@ interface CoreComponent { fun libkiwixBookmarks(): LibkiwixBookmarks fun recentSearchRoomDao(): RecentSearchRoomDao fun historyRoomDao(): HistoryRoomDao - fun pageHistoryRoomDao(): WebViewHistoryRoomDao + fun webViewHistoryRoomDao(): WebViewHistoryRoomDao fun noteRoomDao(): NotesRoomDao fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator fun context(): Context diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt index 667807cafc..31eb596cb7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt @@ -88,7 +88,7 @@ open class DatabaseModule { @Provides @Singleton - fun providePageHistoryRoomDao(db: KiwixRoomDatabase) = db.pageHistoryRoomDao() + fun provideWebViewHistoryRoomDao(db: KiwixRoomDatabase) = db.webViewHistoryRoomDao() @Singleton @Provides diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt index 78c12158f1..e686a68f72 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt @@ -127,7 +127,7 @@ import org.kiwix.kiwixmobile.core.navigateToAppSettings import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem import org.kiwix.kiwixmobile.core.page.history.NavigationHistoryClickListener 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.WebViewHistoryCallback 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.WebViewHistoryItem @@ -190,7 +190,7 @@ abstract class CoreReaderFragment : ReadAloudCallbacks, NavigationHistoryClickListener, ShowDonationDialogCallback, - DataCallback { + WebViewHistoryCallback { protected val webViewList: MutableList = ArrayList() private val webUrlsProcessor = BehaviorProcessor.create() private var fragmentReaderBinding: FragmentReaderBinding? = null @@ -979,7 +979,7 @@ abstract class CoreReaderFragment : override fun clearHistory() { getCurrentWebView()?.clearHistory() - repositoryActions?.clearWebViewPagesHistory() + repositoryActions?.clearWebViewPageHistory() updateBottomToolbarArrowsAlpha() toast(R.string.navigation_history_cleared) } @@ -1884,7 +1884,7 @@ abstract class CoreReaderFragment : // If backStack and forwardStack are empty, return if (backStack.isEmpty() && forwardStack.isEmpty() || currentZimId != pageHistory[0].zimId) { - repositoryActions?.clearWebViewPagesHistory() + repositoryActions?.clearWebViewPageHistory() return } @@ -1938,7 +1938,7 @@ abstract class CoreReaderFragment : } override fun onError(error: Throwable) { - activity.toast(R.string.could_not_restore_tabs, Toast.LENGTH_LONG) + activity.toast(R.string.could_not_restore_web_view_history, Toast.LENGTH_LONG) } override fun onResume() { @@ -2310,6 +2310,26 @@ abstract class CoreReaderFragment : editor.apply() } + private fun saveWebViewHistoryItems( + startIndex: Int, + endIndex: Int, + zimId: String, + isForward: Boolean, + historyList: WebBackForwardList + ) { + for (index in startIndex until endIndex) { + val historyItem = historyList.getItemAtIndex(index) + val pageHistory = WebViewHistoryItem( + zimId = zimId, + title = historyItem.title, + pageUrl = historyItem.url, + isForward = isForward, + timeStamp = index.toLong() + ) + repositoryActions?.saveWebViewPageHistory(pageHistory) + } + } + private fun saveWebBackForwardListToRoom() { val webBackForwardList = getCurrentWebView()?.copyBackForwardList() val currentIndex = webBackForwardList?.currentIndex @@ -2317,40 +2337,28 @@ abstract class CoreReaderFragment : if (currentIndex != null) { // Save BackStack - webBackForwardList.let { historyList -> - 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) - } - } + saveWebViewHistoryItems( + 0, + webBackForwardList.currentIndex, + zimId, + false, + webBackForwardList + ) // Save ForwardStack - webBackForwardList.let { historyList -> - 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) - } - } + saveWebViewHistoryItems( + currentIndex + 1, + webBackForwardList.size, + zimId, + true, + webBackForwardList + ) } } override fun onPause() { super.onPause() - repositoryActions?.clearWebViewPagesHistory() + repositoryActions?.clearWebViewPageHistory() saveWebBackForwardListToRoom() saveTabStates() Log.d( diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt index 8872398d30..3fb5d5abf0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt @@ -21,7 +21,7 @@ import io.reactivex.disposables.Disposable import org.kiwix.kiwixmobile.core.data.DataSource 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.WebViewHistoryCallback 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 @@ -38,9 +38,9 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour private var saveNoteDisposable: Disposable? = null private var saveBookDisposable: Disposable? = null private var deleteNoteDisposable: Disposable? = null - private var savePageHistoryDisposable: Disposable? = null - private var clearPageHistoryDisposable: Disposable? = null - private var getPageHistoryDisposable: Disposable? = null + private var saveWebViewHistoryDisposable: Disposable? = null + private var clearWebViewHistoryDisposable: Disposable? = null + private var getWebViewHistoryDisposable: Disposable? = null fun saveHistory(history: HistoryItem) { saveHistoryDisposable = dataSource.saveHistory(history) @@ -74,17 +74,17 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour } fun saveWebViewPageHistory(pageHistory: WebViewHistoryItem) { - savePageHistoryDisposable = dataSource.insertWebViewHistoryItem(pageHistory) + saveWebViewHistoryDisposable = dataSource.insertWebViewHistoryItem(pageHistory) .subscribe({}, { e -> Log.e(TAG, "Unable to save page history", e) }) } - fun clearWebViewPagesHistory() { - clearPageHistoryDisposable = dataSource.clearWebViewPagesHistory() + fun clearWebViewPageHistory() { + clearWebViewHistoryDisposable = dataSource.clearWebViewPagesHistory() .subscribe({}, { e -> Log.e(TAG, "Unable to clear page history", e) }) } - fun loadWebViewPagesHistory(callBack: DataCallback) { - getPageHistoryDisposable = dataSource.getAllWebViewPagesHistory() + fun loadWebViewPagesHistory(callBack: WebViewHistoryCallback) { + getWebViewHistoryDisposable = dataSource.getAllWebViewPagesHistory() .map { roomEntities -> roomEntities.map(::WebViewHistoryItem) } @@ -97,8 +97,8 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour saveNoteDisposable?.dispose() deleteNoteDisposable?.dispose() saveBookDisposable?.dispose() - savePageHistoryDisposable?.dispose() - clearPageHistoryDisposable?.dispose() - getPageHistoryDisposable?.dispose() + saveWebViewHistoryDisposable?.dispose() + clearWebViewHistoryDisposable?.dispose() + getWebViewHistoryDisposable?.dispose() } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/WebViewHistoryItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/WebViewHistoryItem.kt index aa4e912861..fdcf7c8e4c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/WebViewHistoryItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/WebViewHistoryItem.kt @@ -53,7 +53,7 @@ data class WebViewHistoryItem( ) } -interface DataCallback { +interface WebViewHistoryCallback { fun onDataFetched(pageHistory: List) fun onError(error: Throwable) } diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index db7e5aaa9f..e94744f154 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -226,6 +226,7 @@ Close all tabs Close tab Could not restore tabs. + Could not restore web view history Pending In Progress Complete