From 9552bd30db9c692e8850667686b9bb8867726cdb Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 6 Sep 2021 16:41:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/CacheBook.kt | 6 ++- .../io/legado/app/ui/main/MainViewModel.kt | 39 ++++--------------- 2 files changed, 13 insertions(+), 32 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 f32e33250..ddaf3aaa2 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.kt @@ -160,7 +160,11 @@ class CacheBook(var bookSource: BookSource, var book: Book) { */ fun download(scope: CoroutineScope, context: CoroutineContext): Boolean { synchronized(this) { - val chapterIndex = waitDownloadSet.firstOrNull() ?: return false + val chapterIndex = waitDownloadSet.firstOrNull() + if (chapterIndex == null) { + cacheBookMap.remove(book.bookUrl) + return false + } if (onDownloadSet.contains(chapterIndex)) { waitDownloadSet.remove(chapterIndex) return download(scope, context) 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 99adf6b0a..d37f7ced8 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 @@ -6,16 +6,13 @@ import io.legado.app.constant.BookType import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookSource import io.legado.app.help.AppConfig -import io.legado.app.help.BookHelp import io.legado.app.help.DefaultData import io.legado.app.help.LocalConfig import io.legado.app.model.CacheBook import io.legado.app.model.webBook.WebBook import io.legado.app.utils.postEvent import kotlinx.coroutines.asCoroutineDispatcher -import kotlinx.coroutines.delay import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.Executors @@ -23,7 +20,8 @@ import kotlin.math.min class MainViewModel(application: Application) : BaseViewModel(application) { private var threadCount = AppConfig.threadCount - private var upTocPool = Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() + private var upTocPool = + Executors.newFixedThreadPool(min(threadCount, 8)).asCoroutineDispatcher() val updateList = CopyOnWriteArraySet() private val bookMap = ConcurrentHashMap() @@ -38,7 +36,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { fun upPool() { threadCount = AppConfig.threadCount upTocPool.close() - upTocPool = Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() + upTocPool = Executors.newFixedThreadPool(min(threadCount, 8)).asCoroutineDispatcher() } fun upAllBookToc() { @@ -83,7 +81,11 @@ class MainViewModel(application: Application) : BaseViewModel(application) { appDb.bookDao.update(book) appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.insert(*toc.toTypedArray()) - cacheBook(bookSource, book) + val endIndex = min( + book.totalChapterNum, + book.durChapterIndex.plus(AppConfig.preDownloadNum) + ) + CacheBook.start(context, book.bookUrl, book.durChapterIndex, endIndex) }.onError(upTocPool) { it.printStackTrace() }.onFinally(upTocPool) { @@ -108,31 +110,6 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } } - private fun cacheBook(bookSource: BookSource, book: Book) { - execute { - if (book.totalChapterNum > book.durChapterIndex) { - val downloadToIndex = - min(book.totalChapterNum, book.durChapterIndex.plus(AppConfig.preDownloadNum)) - for (i in book.durChapterIndex until downloadToIndex) { - appDb.bookChapterDao.getChapter(book.bookUrl, i)?.let { chapter -> - if (!BookHelp.hasContent(book, chapter)) { - var addToCache = false - while (!addToCache) { - val cacheBook = CacheBook.getOrCreate(bookSource, book) - if (CacheBook.onDownloadCount < 10) { - cacheBook.download(this, chapter) - addToCache = true - } else { - delay(100) - } - } - } - } - } - } - } - } - private fun upNext() { if (bookMap.size > updateList.size) { updateToc()