From 91a1608298edb18d4f806b632d7d579ed7638e91 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 12 Oct 2020 09:15:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A2=84=E4=B8=8B=E8=BD=BD,?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E5=90=8C=E6=97=B6=E4=B8=8B=E8=BD=BD=E5=A4=AA?= =?UTF-8?q?=E5=A4=9A=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/help/CacheBook.kt | 11 +++++++++ .../io/legado/app/ui/main/MainViewModel.kt | 24 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/CacheBook.kt b/app/src/main/java/io/legado/app/service/help/CacheBook.kt index 149e6258c..18b68b332 100644 --- a/app/src/main/java/io/legado/app/service/help/CacheBook.kt +++ b/app/src/main/java/io/legado/app/service/help/CacheBook.kt @@ -54,6 +54,14 @@ object CacheBook { } } + fun downloadCount(): Int { + var count = 0 + downloadMap.forEach { + count += it.value.size + } + return count + } + fun download( webBook: WebBook, book: Book, @@ -98,6 +106,9 @@ object CacheBook { } }.onFinally { downloadMap[book.bookUrl]?.remove(chapter.index) + if (downloadMap[book.bookUrl].isNullOrEmpty()) { + downloadMap.remove(book.bookUrl) + } ReadBook.removeLoading(chapter.index) } } 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 9a10d0060..8f45c4a9c 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 @@ -8,6 +8,7 @@ import io.legado.app.constant.EventBus import io.legado.app.data.entities.Book import io.legado.app.data.entities.RssSource import io.legado.app.help.AppConfig +import io.legado.app.help.BookHelp import io.legado.app.help.DefaultValueHelp import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Restore @@ -19,6 +20,7 @@ import io.legado.app.utils.fromJsonObject import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.delay import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors @@ -109,11 +111,23 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } private fun cacheBook(webBook: WebBook, book: Book) { - if (book.totalChapterNum > book.durChapterIndex) { - val downloadToIndex = min(book.totalChapterNum, book.durChapterIndex.plus(10)) - for (i in book.durChapterIndex until downloadToIndex) { - App.db.bookChapterDao().getChapter(book.bookUrl, i)?.let { chapter -> - CacheBook.download(webBook, book, chapter) + execute { + if (book.totalChapterNum > book.durChapterIndex) { + val downloadToIndex = min(book.totalChapterNum, book.durChapterIndex.plus(10)) + for (i in book.durChapterIndex until downloadToIndex) { + App.db.bookChapterDao().getChapter(book.bookUrl, i)?.let { chapter -> + if (!BookHelp.hasContent(book, chapter)) { + var addToCache = false + while (!addToCache) { + if (CacheBook.downloadCount() < 5) { + CacheBook.download(webBook, book, chapter) + addToCache = true + } else { + delay(1000) + } + } + } + } } } }