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