Skip to content

Commit

Permalink
Fixed: Restore web view history in Kiwix app.
Browse files Browse the repository at this point in the history
* Improved restoration of web view history after opening a searched article.
* Refactored `restoreViewStateOnValidJSON` and `restoreViewStateOnInvalidJSON` methods to save and retrieve web view history from the Room database.
* Added detailed comments to methods for better understanding, including guidance for developers to check subclass implementations before making modifications.
* Fixed the `static analysis` errors.
  • Loading branch information
MohitMaliDeveloper committed Oct 25, 2024
1 parent b8e9f2d commit 3569070
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.kiwix.kiwixmobile.core.main.ToolbarScrollingKiwixWebView
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource.Companion.fromDatabaseValue
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.TAG_CURRENT_FILE
import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX
Expand All @@ -70,6 +71,7 @@ private const val HIDE_TAB_SWITCHER_DELAY: Long = 300

class KiwixReaderFragment : CoreReaderFragment() {
private var isFullScreenVideo: Boolean = false
private var searchItemToOpen: SearchItemToOpen? = null

override fun inject(baseActivity: BaseActivity) {
baseActivity.cachedComponent.inject(this)
Expand Down Expand Up @@ -110,7 +112,16 @@ class KiwixReaderFragment : CoreReaderFragment() {
} else {
val restoreOrigin =
if (args.searchItemTitle.isNotEmpty()) FromSearchScreen else FromExternalLaunch
manageExternalLaunchAndRestoringViewState(restoreOrigin)
manageExternalLaunchAndRestoringViewState(restoreOrigin) {
// This lambda function is invoked after restoring the tabs. It checks if there is a
// search item to open. If `searchItemToOpen` is not null, it will call the superclass
// method to open the specified search item. After opening, it sets `searchItemToOpen`
// to null to prevent any unexpected behavior on subsequent calls.
searchItemToOpen?.let {
super.openSearchItem(it)
}
searchItemToOpen = null
}
}
}
requireArguments().clear()
Expand Down Expand Up @@ -145,6 +156,18 @@ class KiwixReaderFragment : CoreReaderFragment() {
openZimFile(zimReaderSource)
}

/**
* Stores the specified search item to be opened later.
*
* This method saves the provided `SearchItemToOpen` object, which will be used to
* open the searched item after the tabs have been restored.
*
* @param item The search item to be opened after restoring the tabs.
*/
override fun openSearchItem(item: SearchItemToOpen) {
searchItemToOpen = item
}

override fun loadDrawerViews() {
drawerLayout = requireActivity().findViewById(R.id.navigation_container)
tableDrawerRightContainer = requireActivity().findViewById(R.id.reader_drawer_nav_view)
Expand Down Expand Up @@ -226,7 +249,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
}
}

override fun restoreViewStateOnInvalidJSON() {
override fun restoreViewStateOnInvalidWebViewHistory() {
Log.d(TAG_KIWIX, "Kiwix normal start, no zimFile loaded last time -> display home page")
exitBook()
}
Expand All @@ -239,15 +262,16 @@ class KiwixReaderFragment : CoreReaderFragment() {
* as the ZIM file is already set in the reader. The method handles setting up the ZIM file and bookmarks,
* and restores the tabs and positions from the provided data.
*
* @param webViewHistoryItemList JSON string representing the list of articles to be restored.
* @param currentTab Index of the tab to be restored as the currently active one.
* @param webViewHistoryItemList WebViewHistoryItem list representing the list of articles to be restored.
* @param currentTab Index of the tab to be restored as the currently active one.
* @param restoreOrigin Indicates whether the restoration is triggered from an external launch or the search screen.
* @param onComplete Callback to be invoked upon completion of the restoration process.
*/

override fun restoreViewStateOnValidJSON(
override fun restoreViewStateOnValidWebViewHistory(
webViewHistoryItemList: List<WebViewHistoryItem>,
currentTab: Int,
restoreOrigin: RestoreOrigin
restoreOrigin: RestoreOrigin,
onComplete: () -> Unit
) {
when (restoreOrigin) {
FromExternalLaunch -> {
Expand All @@ -257,15 +281,15 @@ class KiwixReaderFragment : CoreReaderFragment() {
val zimReaderSource = fromDatabaseValue(settings.getString(TAG_CURRENT_FILE, null))
if (zimReaderSource?.canOpenInLibkiwix() == true) {
if (zimReaderContainer?.zimReaderSource == null) {
openZimFile(zimReaderSource)
openZimFile(zimReaderSource, isFromManageExternalLaunch = true)
Log.d(
TAG_KIWIX,
"Kiwix normal start, Opened last used zimFile: -> ${zimReaderSource.toDatabase()}"
)
} else {
zimReaderContainer?.zimFileReader?.let(::setUpBookmarks)
}
restoreTabs(webViewHistoryItemList, currentTab)
restoreTabs(webViewHistoryItemList, currentTab, onComplete)
} else {
getCurrentWebView()?.snack(string.zim_not_opened)
exitBook() // hide the options for zim file to avoid unexpected UI behavior
Expand All @@ -274,7 +298,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
}

FromSearchScreen -> {
restoreTabs(webViewHistoryItemList, currentTab)
restoreTabs(webViewHistoryItemList, currentTab, onComplete)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ 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.WebViewHistoryRoomDao
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 Down Expand Up @@ -148,7 +147,9 @@ class Repository @Inject internal constructor(
Completable.fromAction { notesRoomDao.deleteNotes(noteList) }
.subscribeOn(ioThread)

override suspend fun insertWebViewPageHistoryItems(webViewHistoryEntityList: List<WebViewHistoryEntity>) {
override suspend fun insertWebViewPageHistoryItems(
webViewHistoryEntityList: List<WebViewHistoryEntity>
) {
webViewHistoryRoomDao.insertWebViewPageHistoryItems(webViewHistoryEntityList)
}

Expand All @@ -159,7 +160,7 @@ class Repository @Inject internal constructor(
.observeOn(mainThread)

override suspend fun clearWebViewPagesHistory() {
webViewHistoryRoomDao.clearPageHistoryWithPrimaryKey()
webViewHistoryRoomDao.clearWebViewPagesHistory()
}

override fun deleteNote(noteTitle: String): Completable =
Expand Down
Loading

0 comments on commit 3569070

Please sign in to comment.