mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 10:46:53 -04:00
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.
This commit is contained in:
parent
563e06e302
commit
b8d1b8a4c7
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user