Skip to content

Commit

Permalink
Fixed: infinite loop and improve error handling for fetching random a…
Browse files Browse the repository at this point in the history
…rticle.

* Fixed an issue where the app would enter an infinite loop when the zimFileReader was null, causing a crash.
* Improved the condition to check if zimFileReader is null and show a proper error message to the user without retrying.
* Added a retry mechanism to attempt fetching the random article twice in case of an internal error in libzim.
* After two failed attempts, an error message is displayed to the user if the random article is not found.
  • Loading branch information
MohitMaliDeveloper committed Jan 6, 2025
1 parent 359a1fc commit 36f9be8
Showing 1 changed file with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.dao.LibkiwixBookmarks
import org.kiwix.kiwixmobile.core.databinding.FragmentReaderBinding
import org.kiwix.kiwixmobile.core.downloader.downloadManager.ZERO
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.consumeObservable
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isLandScapeMode
Expand Down Expand Up @@ -2173,13 +2174,39 @@ abstract class CoreReaderFragment :
}
}

private fun openRandomArticle() {
/**
* Attempts to open a random article from the ZIM file. If the article URL cannot be retrieved
* due to internal errors or a missing ZIM file reader, the method will retry up to a certain
* number of times (default: 2). If the article URL is still unavailable after retries,
* an error message will be displayed to the user. The method ensures that the user does not
* see a blank or previously loaded page, but instead receives an appropriate error message
* if the random article cannot be fetched.
*
* @param retryCount The number of attempts left to retry fetching the random article.
* Default is 2. The method decreases this count with each retry attempt.
*/
private fun openRandomArticle(retryCount: Int = 2) {
// Check if the ZIM file reader is available, if not show an error and exit.
if (zimReaderContainer?.zimFileReader == null) {
toast(R.string.error_loading_random_article_zim_not_loaded)
return
}
val articleUrl = zimReaderContainer?.getRandomArticleUrl()
if (articleUrl == null) {
// Check if the random url is null due to some internal error in libzim(See #3926)
// then again try to get the random article. So that the user can see the random article
// instead of a (blank/same page) currently loaded in the webView.
openRandomArticle()
// then try one more time to get the random article. So that the user can see the
// random article instead of a (blank/same page) currently loaded in the webView.
if (retryCount > ZERO) {
Log.e(
TAG_KIWIX,
"Random article URL is null, retrying... Remaining attempts: $retryCount"
)
openRandomArticle(retryCount - 1)
} else {
// if it is failed to find the random article two times then show a error to user.
Log.e(TAG_KIWIX, "Failed to load random article after multiple attempts")
toast(R.string.could_not_find_random_article)
}
return
}
Log.d(TAG_KIWIX, "openRandomArticle: $articleUrl")
Expand Down

0 comments on commit 36f9be8

Please sign in to comment.