From ad5f454362403a2b67b9f425247ce3284c3bd7a9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 6 Jul 2022 21:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/model/CacheBook.kt | 11 +++++------ .../java/io/legado/app/service/CacheBookService.kt | 10 +++++++--- .../main/java/io/legado/app/ui/main/MainViewModel.kt | 10 +++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index 79394d5c4..d132a6f18 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.kt @@ -202,25 +202,25 @@ object CacheBook { * 从待下载列表内取第一条下载 */ @Synchronized - fun download(scope: CoroutineScope, context: CoroutineContext): Boolean { + fun download(scope: CoroutineScope, context: CoroutineContext) { val chapterIndex = waitDownloadSet.firstOrNull() if (chapterIndex == null) { if (onDownloadSet.isEmpty()) { cacheBookMap.remove(book.bookUrl) } - return false + return } if (onDownloadSet.contains(chapterIndex)) { waitDownloadSet.remove(chapterIndex) - return download(scope, context) + return } val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, chapterIndex) ?: let { waitDownloadSet.remove(chapterIndex) - return download(scope, context) + return } if (BookHelp.hasContent(book, chapter)) { waitDownloadSet.remove(chapterIndex) - return download(scope, context) + return } waitDownloadSet.remove(chapterIndex) onDownloadSet.add(chapterIndex) @@ -243,7 +243,6 @@ object CacheBook { }.onFinally { onFinally() } - return true } @Synchronized diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index cbba71814..3407c7034 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -107,10 +107,14 @@ class CacheBookService : BaseService() { return@launch } CacheBook.cacheBookMap.forEach { - while (CacheBook.onDownloadCount > threadCount) { - delay(100) + val cacheBookModel = it.value + while (cacheBookModel.waitCount > 0) { + if (CacheBook.onDownloadCount < threadCount) { + cacheBookModel.download(this, cachePool) + } else { + delay(100) + } } - it.value.download(this, cachePool) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index b1559f2cb..2bef7753b 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -195,10 +195,14 @@ class MainViewModel(application: Application) : BaseViewModel(application) { return@launch } CacheBook.cacheBookMap.forEach { - while (CacheBook.onDownloadCount > threadCount) { - delay(100) + val cacheBookModel = it.value + while (cacheBookModel.waitCount > 0) { + if (CacheBook.onDownloadCount < threadCount) { + cacheBookModel.download(this, upTocPool) + } else { + delay(100) + } } - it.value.download(this, upTocPool) } } }