Fixed: Links not working in restored tabs after closing all tabs and restoring them.

* Improved the tab closing and restoring process. Previously, when the user closed all tabs, the `ZimFileReader` was set to null. If the user restored the tabs, a new `ZimFileReader` was created, which was a resource-intensive operation, especially for large ZIM files.
* Now, the `ZimFileReader` is not set to null while the "restore tab" snackbar is visible, allowing users to restore tabs without recreating the ZimFileReader.
* Once the snackbar is dismissed, the `ZimFileReader` is set to null to free up resources since it is no longer required.
This commit is contained in:
MohitMaliFtechiz 2025-01-20 16:33:31 +05:30
parent c9895206ed
commit 0d18419f7a
2 changed files with 49 additions and 11 deletions

View File

@ -159,12 +159,25 @@ class KiwixReaderFragment : CoreReaderFragment() {
override fun openHomeScreen() {
Handler(Looper.getMainLooper()).postDelayed({
if (webViewList.size == 0) {
hideTabSwitcher()
hideTabSwitcher(false)
}
}, HIDE_TAB_SWITCHER_DELAY)
}
override fun hideTabSwitcher() {
/**
* Hides the tab switcher and optionally closes the ZIM book based on the `shouldCloseZimBook` parameter.
*
* @param shouldCloseZimBook If `true`, the ZIM book will be closed, and the `ZimFileReader` will be set to `null`.
* If `false`, it skips setting the `ZimFileReader` to `null`. This is particularly useful when restoring tabs,
* as setting the `ZimFileReader` to `null` would require re-creating it, which is a resource-intensive operation,
* especially for large ZIM files.
*
* Refer to the following methods for more details:
* @See exitBook
* @see closeTab
* @see closeAllTabs
*/
override fun hideTabSwitcher(shouldCloseZimBook: Boolean) {
actionBar?.let { actionBar ->
actionBar.setDisplayShowTitleEnabled(true)
toolbar?.let { activity?.setupDrawerToggle(it, true) }
@ -181,7 +194,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
}
mainMenu?.showWebViewOptions(true)
if (webViewList.isEmpty()) {
exitBook()
exitBook(shouldCloseZimBook)
} else {
// Reset the top margin of web views to 0 to remove any previously set margin
// This ensures that the web views are displayed without any additional

View File

@ -860,7 +860,13 @@ abstract class CoreReaderFragment :
view?.startAnimation(AnimationUtils.loadAnimation(view.context, anim))
}
protected open fun hideTabSwitcher() {
/**
* @param shouldCloseZimBook A flag to indicate whether the ZIM book should be closed.
* - Default is `true`, which ensures normal behavior for most scenarios.
* - If `false`, the ZIM book is not closed. This is useful in cases where the user restores tabs,
* as closing the ZIM book would require reloading the ZIM file, which can be a resource-intensive operation.
*/
protected open fun hideTabSwitcher(shouldCloseZimBook: Boolean = true) {
actionBar?.apply {
setDisplayShowTitleEnabled(true)
}
@ -1388,7 +1394,17 @@ abstract class CoreReaderFragment :
.setAction(R.string.undo) { undoButton ->
undoButton.isEnabled = false
restoreDeletedTab(index)
}.show()
}.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
// If the undo button is not clicked and no tabs are left, exit the book and
// clean up resources.
if (event != DISMISS_EVENT_ACTION && webViewList.isEmpty()) {
closeZimBook()
}
}
})
.show()
}
openHomeScreen()
}
@ -1399,18 +1415,20 @@ abstract class CoreReaderFragment :
mainMenu?.showBookSpecificMenuItems()
}
protected fun exitBook() {
protected fun exitBook(shouldCloseZimBook: Boolean = true) {
showNoBookOpenViews()
bottomToolbar?.visibility = View.GONE
actionBar?.title = getString(R.string.reader)
contentFrame?.visibility = View.GONE
hideProgressBar()
mainMenu?.hideBookSpecificMenuItems()
closeZimBook()
if (shouldCloseZimBook) {
closeZimBook()
}
}
fun closeZimBook() {
coreReaderLifeCycleScope?.launch {
lifecycleScope.launch {
zimReaderContainer?.setZimReaderSource(null)
}
}
@ -1445,7 +1463,6 @@ abstract class CoreReaderFragment :
LinearLayout.LayoutParams.MATCH_PARENT
)
}
// zimReaderContainer?.setZimReaderSource(tempZimSourceForUndo)
webViewList.add(index, it)
tabsAdapter?.notifyDataSetChanged()
snackBarRoot?.let { root ->
@ -1878,7 +1895,16 @@ abstract class CoreReaderFragment :
setIsCloseAllTabButtonClickable(true)
restoreDeletedTabs()
}
}.show()
}.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
// If the undo button is not clicked and no tabs are left, exit the book and
// clean up resources.
if (event != DISMISS_EVENT_ACTION && webViewList.isEmpty()) {
closeZimBook()
}
}
}).show()
}
}
@ -1888,7 +1914,6 @@ abstract class CoreReaderFragment :
private fun restoreDeletedTabs() {
if (tempWebViewListForUndo.isNotEmpty()) {
// zimReaderContainer?.setZimReaderSource(tempZimSourceForUndo)
webViewList.addAll(tempWebViewListForUndo)
tabsAdapter?.notifyDataSetChanged()
snackBarRoot?.let { root ->