|
|
@@ -42,6 +42,7 @@ import kotlinx.coroutines.ensureActive
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
import kotlinx.coroutines.flow.receiveAsFlow
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.sync.Semaphore
|
|
|
import kotlinx.coroutines.withContext
|
|
|
import splitties.init.appCtx
|
|
|
import kotlin.math.max
|
|
|
@@ -92,6 +93,7 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
val downloadFailChapters = hashMapOf<Int, Int>()
|
|
|
var contentProcessor: ContentProcessor? = null
|
|
|
val downloadScope = CoroutineScope(SupervisorJob() + IO)
|
|
|
+ val preDownloadSemaphore = Semaphore(2)
|
|
|
val executor = globalExecutor
|
|
|
|
|
|
//暂时保存跳转前进度
|
|
|
@@ -492,6 +494,8 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
|
|
|
val isScroll inline get() = pageAnim() == scrollPageAnim
|
|
|
|
|
|
+ val contentLoadFinish get() = curTextChapter != null || msg != null
|
|
|
+
|
|
|
/**
|
|
|
* chapterOnDur: 0为当前页,1为下一页,-1为上一页
|
|
|
*/
|
|
|
@@ -612,10 +616,10 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
downloadedChapters.add(chapter.index)
|
|
|
} else {
|
|
|
delay(1000)
|
|
|
- download(downloadScope, chapter, false)
|
|
|
+ download(downloadScope, chapter, false, preDownloadSemaphore)
|
|
|
}
|
|
|
} ?: removeLoading(index)
|
|
|
- } catch (e: Exception) {
|
|
|
+ } catch (_: Exception) {
|
|
|
removeLoading(index)
|
|
|
}
|
|
|
}
|
|
|
@@ -628,12 +632,13 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
scope: CoroutineScope,
|
|
|
chapter: BookChapter,
|
|
|
resetPageOffset: Boolean,
|
|
|
+ semaphore: Semaphore? = null,
|
|
|
success: (() -> Unit)? = null
|
|
|
) {
|
|
|
val book = book ?: return removeLoading(chapter.index)
|
|
|
val bookSource = bookSource
|
|
|
if (bookSource != null) {
|
|
|
- CacheBook.getOrCreate(bookSource, book).download(scope, chapter)
|
|
|
+ CacheBook.getOrCreate(bookSource, book).download(scope, chapter, semaphore)
|
|
|
} else {
|
|
|
val msg = if (book.isLocal) "无内容" else "没有书源"
|
|
|
contentLoadFinish(
|
|
|
@@ -905,6 +910,13 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun cancelPreDownloadTask() {
|
|
|
+ if (contentLoadFinish) {
|
|
|
+ preDownloadTask?.cancel()
|
|
|
+ downloadScope.coroutineContext.cancelChildren()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 注册回调
|
|
|
*/
|