Fix crash on page loading
This commit is contained in:
@@ -7,14 +7,16 @@ import android.net.Uri
|
|||||||
import androidx.collection.LongSparseArray
|
import androidx.collection.LongSparseArray
|
||||||
import androidx.collection.set
|
import androidx.collection.set
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import java.io.File
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import java.util.*
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import java.util.zip.ZipFile
|
import kotlinx.coroutines.Dispatchers
|
||||||
import javax.inject.Inject
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.runInterruptible
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@@ -30,7 +32,15 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
|
|||||||
import org.koitharu.kotatsu.parsers.util.await
|
import org.koitharu.kotatsu.parsers.util.await
|
||||||
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
|
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
|
||||||
import org.koitharu.kotatsu.utils.ext.connectivityManager
|
import org.koitharu.kotatsu.utils.ext.connectivityManager
|
||||||
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
||||||
import org.koitharu.kotatsu.utils.progress.ProgressDeferred
|
import org.koitharu.kotatsu.utils.progress.ProgressDeferred
|
||||||
|
import java.io.File
|
||||||
|
import java.util.LinkedList
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
import java.util.zip.ZipFile
|
||||||
|
import javax.inject.Inject
|
||||||
|
import kotlin.coroutines.AbstractCoroutineContextElement
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
private const val PROGRESS_UNDEFINED = -1f
|
private const val PROGRESS_UNDEFINED = -1f
|
||||||
private const val PREFETCH_LIMIT_DEFAULT = 10
|
private const val PREFETCH_LIMIT_DEFAULT = 10
|
||||||
@@ -43,7 +53,7 @@ class PageLoader @Inject constructor(
|
|||||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
) : Closeable {
|
) : Closeable {
|
||||||
|
|
||||||
val loaderScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
val loaderScope = CoroutineScope(SupervisorJob() + InternalErrorHandler() + Dispatchers.Default)
|
||||||
|
|
||||||
private val connectivityManager = context.connectivityManager
|
private val connectivityManager = context.connectivityManager
|
||||||
private val tasks = LongSparseArray<ProgressDeferred<File, Float>>()
|
private val tasks = LongSparseArray<ProgressDeferred<File, Float>>()
|
||||||
@@ -197,4 +207,13 @@ class PageLoader @Inject constructor(
|
|||||||
val deferred = CompletableDeferred(file)
|
val deferred = CompletableDeferred(file)
|
||||||
return ProgressDeferred(deferred, emptyProgressFlow)
|
return ProgressDeferred(deferred, emptyProgressFlow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class InternalErrorHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
|
||||||
|
CoroutineExceptionHandler {
|
||||||
|
|
||||||
|
override fun handleException(context: CoroutineContext, exception: Throwable) {
|
||||||
|
exception.printStackTraceDebug()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ class PageHolderDelegate(
|
|||||||
callback.onImageReady(file.toUri())
|
callback.onImageReady(file.toUri())
|
||||||
} catch (e: CancellationException) {
|
} catch (e: CancellationException) {
|
||||||
throw e
|
throw e
|
||||||
} catch (e: Exception) {
|
} catch (e: Throwable) {
|
||||||
state = State.ERROR
|
state = State.ERROR
|
||||||
error = e
|
error = e
|
||||||
callback.onError(e)
|
callback.onError(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user