From b8d1b8a4c746fd5e2fc6bd6165c98a966af4538f Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 9 Apr 2025 18:07:41 +0530 Subject: [PATCH] Fixed: `Input dispatching timed out` error while downloading files from ZIM files. * Moved the download logic to the IO thread to prevent blocking the main thread. * Also refactored the code structure slightly to reduce nesting and resolve the NestedBlockDepth lint warning. --- .../kiwixmobile/core/main/KiwixWebView.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.kt index 0904e25a7..f85ce8401 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.kt @@ -31,6 +31,7 @@ import io.reactivex.disposables.CompositeDisposable import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.kiwix.kiwixmobile.core.BuildConfig import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance @@ -168,18 +169,19 @@ open class KiwixWebView @SuppressLint("SetJavaScriptEnabled") constructor( private val sharedPreferenceUtil: SharedPreferenceUtil ) : Handler(Looper.getMainLooper()) { - @SuppressWarnings("NestedBlockDepth") + @SuppressWarnings("InjectDispatcher") override fun handleMessage(msg: Message) { val url = msg.data.getString("url", null) val src = msg.data.getString("src", null) - if (url != null || src != null) { - CoroutineScope(Dispatchers.Main.immediate).launch { - val savedFile = - FileUtils.downloadFileFromUrl(url, src, zimReaderContainer, sharedPreferenceUtil) + if (url == null && src == null) return + CoroutineScope(Dispatchers.IO).launch { + val savedFile = + FileUtils.downloadFileFromUrl(url, src, zimReaderContainer, sharedPreferenceUtil) + + withContext(Dispatchers.Main) { savedFile?.let { - instance.toast(instance.getString(R.string.save_media_saved, it.name)).also { - Log.e("savedFile", "handleMessage: ${savedFile.isFile} ${savedFile.path}") - } + instance.toast(instance.getString(R.string.save_media_saved, it.name)) + Log.e("savedFile", "handleMessage: ${savedFile.isFile} ${savedFile.path}") } ?: run { instance.toast(R.string.save_media_error) }