From 6937184ec4802198285a3d0c7a92fec0ec863f1a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 21 Aug 2021 01:34:26 +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/assets/updateLog.md | 2 +- .../main/java/io/legado/app/model/ReadBook.kt | 47 ++++++++++--------- .../app/model/webBook/BookChapterList.kt | 2 +- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 6e70517ad..e86de2c23 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -14,7 +14,7 @@ **2021/08/18** -1. 翻到最后一章时自动更新最新章节 +1. 阅读时自动更新最新章节 2. 朗读添加媒体按键配置 3. 修复rss列表界面分类往回切换时没有数据的bug 4. 修复订阅分类往回切换时不显示内容的bug diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index bd7250a70..5c0f66c01 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -19,14 +19,13 @@ import io.legado.app.utils.msg import io.legado.app.utils.toastOnUi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.MainScope import kotlinx.coroutines.delay import splitties.init.appCtx -import kotlin.math.max -import kotlin.math.min @Suppress("MemberVisibilityCanBePrivate") -object ReadBook { +object ReadBook : CoroutineScope by MainScope() { var titleDate = MutableLiveData() var book: Book? = null var inBookshelf = false @@ -145,27 +144,11 @@ object ReadBook { curPageChanged() Coroutine.async { //预下载 - val maxChapterIndex = - min(chapterSize - 1, durChapterIndex + AppConfig.preDownloadNum) + val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum for (i in durChapterIndex.plus(2)..maxChapterIndex) { delay(1000) download(i) } - book?.let { book -> - //最后一章时检查更新 - if (durChapterPos == 0 && durChapterIndex == chapterSize - 1) { - webBook?.getChapterList(this, book) - ?.onSuccess(IO) { cList -> - if (book.bookUrl == ReadBook.book?.bookUrl - && cList.size > chapterSize - ) { - appDb.bookChapterDao.insert(*cList.toTypedArray()) - chapterSize = cList.size - nextTextChapter ?: loadContent(1) - } - } - } - } } return true } else { @@ -194,7 +177,7 @@ object ReadBook { curPageChanged() Coroutine.async { //预下载 - val minChapterIndex = max(0, durChapterIndex - 5) + val minChapterIndex = durChapterIndex - 5 for (i in durChapterIndex.minus(2) downTo minChapterIndex) { delay(1000) download(i) @@ -301,6 +284,11 @@ object ReadBook { } private fun download(index: Int) { + if (index < 0) return + if (index > chapterSize - 1) { + upToc() + return + } book?.let { book -> if (book.isLocalBook()) return if (addLoading(index)) { @@ -404,6 +392,23 @@ object ReadBook { } } + @Synchronized + fun upToc() { + val webBook = webBook ?: return + val book = book ?: return + if (System.currentTimeMillis() - book.lastCheckTime < 600000) return + book.lastCheckTime = System.currentTimeMillis() + webBook.getChapterList(this, book).onSuccess(IO) { cList -> + if (book.bookUrl == ReadBook.book?.bookUrl + && cList.size > chapterSize + ) { + appDb.bookChapterDao.insert(*cList.toTypedArray()) + chapterSize = cList.size + nextTextChapter ?: loadContent(1) + } + } + } + fun pageAnim(): Int { book?.let { return if (it.getPageAnim() < 0) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index 8b1a56803..817038732 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -120,8 +120,8 @@ object BookChapterList { if (book.totalChapterNum < list.size) { book.lastCheckCount = list.size - book.totalChapterNum book.latestChapterTime = System.currentTimeMillis() - book.lastCheckTime = System.currentTimeMillis() } + book.lastCheckTime = System.currentTimeMillis() book.totalChapterNum = list.size return list }