From a9e88cac0481e1857a5a48fa0569738650a23be2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 14 Sep 2021 09:50:49 +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 --- .../io/legado/app/ui/main/MainViewModel.kt | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) 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 e9240002c..e230c53c9 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 @@ -9,6 +9,7 @@ import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book 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 @@ -85,11 +86,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { appDb.bookDao.update(book) appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.insert(*toc.toTypedArray()) - val endIndex = min( - book.totalChapterNum, - book.durChapterIndex.plus(AppConfig.preDownloadNum) - ) - CacheBook.start(context, book.bookUrl, book.durChapterIndex, endIndex) + cacheBook(book) }.onError(upTocPool) { AppLog.addLog("${book.name} 更新目录失败\n${it.localizedMessage}", it) it.printOnDebug() @@ -115,6 +112,22 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } } + private fun cacheBook(book: Book) { + val endIndex = min( + book.totalChapterNum - 1, + book.durChapterIndex.plus(AppConfig.preDownloadNum) + ) + for (i in book.durChapterIndex..endIndex) { + appDb.bookChapterDao.getChapter(book.bookUrl, i)?.let { chapter -> + if (!BookHelp.hasContent(book, chapter)) { + CacheBook.start(context, book.bookUrl, i, endIndex) + return + } + } + } + + } + private fun upNext() { if (bookMap.size > updateList.size) { updateToc()