|
|
|
|
@ -6,9 +6,22 @@ import android.graphics.BitmapFactory
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import androidx.collection.LongSparseArray
|
|
|
|
|
import androidx.collection.set
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.util.LinkedList
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import java.util.zip.ZipFile
|
|
|
|
|
import kotlin.coroutines.AbstractCoroutineContextElement
|
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
import kotlinx.coroutines.CompletableDeferred
|
|
|
|
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
|
import kotlinx.coroutines.cancel
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.runInterruptible
|
|
|
|
|
import kotlinx.coroutines.sync.Mutex
|
|
|
|
|
import kotlinx.coroutines.sync.withLock
|
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
|
@ -26,18 +39,15 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
|
|
|
import org.koitharu.kotatsu.parsers.util.await
|
|
|
|
|
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.connectivityManager
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
|
|
|
|
|
import org.koitharu.kotatsu.utils.progress.ProgressDeferred
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.util.*
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import java.util.zip.ZipFile
|
|
|
|
|
|
|
|
|
|
private const val PROGRESS_UNDEFINED = -1f
|
|
|
|
|
private const val PREFETCH_LIMIT_DEFAULT = 10
|
|
|
|
|
|
|
|
|
|
class PageLoader : KoinComponent, Closeable {
|
|
|
|
|
|
|
|
|
|
val loaderScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
|
|
|
val loaderScope = CoroutineScope(SupervisorJob() + InternalErrorHandler() + Dispatchers.Default)
|
|
|
|
|
|
|
|
|
|
private val okHttp = get<OkHttpClient>()
|
|
|
|
|
private val cache = get<PagesCache>()
|
|
|
|
|
@ -194,4 +204,13 @@ class PageLoader : KoinComponent, Closeable {
|
|
|
|
|
val deferred = CompletableDeferred(file)
|
|
|
|
|
return ProgressDeferred(deferred, emptyProgressFlow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class InternalErrorHandler :
|
|
|
|
|
AbstractCoroutineContextElement(CoroutineExceptionHandler),
|
|
|
|
|
CoroutineExceptionHandler {
|
|
|
|
|
|
|
|
|
|
override fun handleException(context: CoroutineContext, exception: Throwable) {
|
|
|
|
|
exception.printStackTraceDebug()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|