Improved the saving of tabs.

This commit is contained in:
MohitMaliFtechiz 2024-12-06 11:46:31 +05:30
parent 83337ac5f1
commit d3f40f2219

View File

@ -99,6 +99,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.kiwix.kiwixmobile.core.BuildConfig
import org.kiwix.kiwixmobile.core.CoreApp
@ -286,7 +288,7 @@ abstract class CoreReaderFragment :
private var isFirstTimeMainPageLoaded = true
private var isFromManageExternalLaunch = false
private var shouldSaveTabsOnPause = true
private val savingTabsMutex = Mutex()
private var searchItemToOpen: SearchItemToOpen? = null
private var findInPageTitle: String? = null
@ -424,9 +426,6 @@ abstract class CoreReaderFragment :
savedInstanceState: Bundle?
) {
super.onViewCreated(view, savedInstanceState)
// Set this to true to enable saving the tab history
// when the fragment goes into the paused state.
shouldSaveTabsOnPause = true
setupMenu()
donationDialogHandler?.setDonationDialogCallBack(this)
val activity = requireActivity() as AppCompatActivity?
@ -1556,9 +1555,6 @@ abstract class CoreReaderFragment :
}
override fun onSearchMenuClickedMenuClicked() {
// Set this to false to prevent saving the tab history in onPause
// when opening the search fragment.
shouldSaveTabsOnPause = false
saveTabStates {
// Pass this function to saveTabStates so that after saving
// the tab state in the database, it will open the search fragment.
@ -2465,6 +2461,7 @@ abstract class CoreReaderFragment :
*/
private fun saveTabStates(onComplete: () -> Unit = {}) {
CoroutineScope(Dispatchers.Main).launch {
savingTabsMutex.withLock {
// clear the previous history saved in database
withContext(Dispatchers.IO) {
repositoryActions?.clearWebViewPageHistory()
@ -2494,6 +2491,7 @@ abstract class CoreReaderFragment :
onComplete.invoke()
}
}
}
/**
* Retrieves a `WebViewHistoryEntity` from the given `KiwixWebView` instance.
@ -2543,16 +2541,6 @@ abstract class CoreReaderFragment :
return null
}
/**
* @see shouldSaveTabsOnPause
*/
override fun onPause() {
super.onPause()
if (shouldSaveTabsOnPause) {
saveTabStates()
}
}
override fun webViewUrlLoading() {
if (isFirstRun && !BuildConfig.DEBUG) {
contentsDrawerHint()
@ -2639,6 +2627,7 @@ abstract class CoreReaderFragment :
showProgressBarWithProgress(progress)
if (progress == 100) {
hideProgressBar()
saveTabStates()
Log.d(TAG_KIWIX, "Loaded URL: " + getCurrentWebView()?.url)
}
(webView.context as AppCompatActivity).invalidateOptionsMenu()