mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 06:42:21 -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.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import org.kiwix.kiwixmobile.core.BuildConfig
|
import org.kiwix.kiwixmobile.core.BuildConfig
|
||||||
import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent
|
import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent
|
||||||
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
|
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
|
||||||
@ -168,18 +169,19 @@ open class KiwixWebView @SuppressLint("SetJavaScriptEnabled") constructor(
|
|||||||
private val sharedPreferenceUtil: SharedPreferenceUtil
|
private val sharedPreferenceUtil: SharedPreferenceUtil
|
||||||
) :
|
) :
|
||||||
Handler(Looper.getMainLooper()) {
|
Handler(Looper.getMainLooper()) {
|
||||||
@SuppressWarnings("NestedBlockDepth")
|
@SuppressWarnings("InjectDispatcher")
|
||||||
override fun handleMessage(msg: Message) {
|
override fun handleMessage(msg: Message) {
|
||||||
val url = msg.data.getString("url", null)
|
val url = msg.data.getString("url", null)
|
||||||
val src = msg.data.getString("src", null)
|
val src = msg.data.getString("src", null)
|
||||||
if (url != null || src != null) {
|
if (url == null && src == null) return
|
||||||
CoroutineScope(Dispatchers.Main.immediate).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val savedFile =
|
val savedFile =
|
||||||
FileUtils.downloadFileFromUrl(url, src, zimReaderContainer, sharedPreferenceUtil)
|
FileUtils.downloadFileFromUrl(url, src, zimReaderContainer, sharedPreferenceUtil)
|
||||||
|
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
savedFile?.let {
|
savedFile?.let {
|
||||||
instance.toast(instance.getString(R.string.save_media_saved, it.name)).also {
|
instance.toast(instance.getString(R.string.save_media_saved, it.name))
|
||||||
Log.e("savedFile", "handleMessage: ${savedFile.isFile} ${savedFile.path}")
|
Log.e("savedFile", "handleMessage: ${savedFile.isFile} ${savedFile.path}")
|
||||||
}
|
|
||||||
} ?: run {
|
} ?: run {
|
||||||
instance.toast(R.string.save_media_error)
|
instance.toast(R.string.save_media_error)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user