From 16b3efcdb7b35ece0eefc27c41ced31e5268a1fb Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Fri, 23 Feb 2024 15:22:31 +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 --- .../java/io/legado/app/data/entities/Book.kt | 6 +++ .../main/java/io/legado/app/model/ReadBook.kt | 38 +++++++++++++++---- .../app/ui/book/read/ReadBookViewModel.kt | 13 +------ .../ui/book/read/page/entities/TextChapter.kt | 2 - 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 49f35f985..46d7d5075 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -16,10 +16,12 @@ import io.legado.app.help.book.BookHelp import io.legado.app.help.book.ContentProcessor import io.legado.app.help.book.isEpub import io.legado.app.help.book.isImage +import io.legado.app.help.book.isLocal import io.legado.app.help.book.isPdf import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ReadBookConfig import io.legado.app.model.ReadBook +import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.GSON import io.legado.app.utils.MD5Utils import io.legado.app.utils.fromJsonObject @@ -286,6 +288,10 @@ data class Book( return appDb.bookSourceDao.getBookSource(origin) } + fun isLocalModified(): Boolean { + return isLocal && LocalBook.getLastModified(this).getOrDefault(0L) > latestChapterTime + } + fun toSearchBook() = SearchBook( name = name, author = author, 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 796cb9b1a..68b01eb34 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -88,7 +88,7 @@ object ReadBook : CoroutineScope by MainScope() { readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0 chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl) contentProcessor = ContentProcessor.get(book) - durChapterIndex = book.durChapterIndex + durChapterIndex = min(book.durChapterIndex, chapterSize - 1) durChapterPos = book.durChapterPos isLocalBook = book.isLocal clearTextChapter() @@ -111,6 +111,15 @@ object ReadBook : CoroutineScope by MainScope() { durChapterPos = book.durChapterPos clearTextChapter() } + if (curTextChapter?.isCompleted == false) { + curTextChapter = null + } + if (nextTextChapter?.isCompleted == false) { + nextTextChapter = null + } + if (prevTextChapter?.isCompleted == false) { + prevTextChapter = null + } callBack?.upMenuView() upWebBook(book) synchronized(this) { @@ -369,6 +378,20 @@ object ReadBook : CoroutineScope by MainScope() { loadContent(durChapterIndex - 1, resetPageOffset = resetPageOffset) } + fun loadOrUpContent() { + if (curTextChapter == null) { + loadContent(durChapterIndex) + } else { + callBack?.upContent() + } + if (nextTextChapter == null) { + loadContent(durChapterIndex + 1) + } + if (prevTextChapter == null) { + loadContent(durChapterIndex - 1) + } + } + /** * 加载章节内容 * @param index 章节序号 @@ -392,10 +415,9 @@ object ReadBook : CoroutineScope by MainScope() { chapter, it, upContent, - resetPageOffset - ) { - success?.invoke() - } + resetPageOffset, + success + ) } ?: download( downloadScope, chapter, @@ -456,9 +478,8 @@ object ReadBook : CoroutineScope by MainScope() { chapter, "加载正文失败\n$msg", resetPageOffset = resetPageOffset, - ) { - success?.invoke() - } + success = success + ) } } @@ -655,6 +676,7 @@ object ReadBook : CoroutineScope by MainScope() { downloadedChapters.clear() downloadFailChapters.clear() ImageProvider.clear() + curTextChapter?.cancelLayout() } interface CallBack { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 8e73e5e84..d6d78c7cd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -109,21 +109,12 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } else { loadChapterList(book) } - } else if (book.isLocal - && LocalBook.getLastModified(book).getOrDefault(0L) > book.latestChapterTime - ) { + } else if (book.isLocalModified()) { loadChapterList(book) } else if (isSameBook) { - if (ReadBook.curTextChapter != null) { - ReadBook.callBack?.upContent(resetPageOffset = false) - } else { - ReadBook.loadContent(resetPageOffset = true) - } + ReadBook.loadOrUpContent() checkLocalBookFileExist(book) } else { - if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) { - ReadBook.durChapterIndex = ReadBook.chapterSize - 1 - } ReadBook.loadContent(resetPageOffset = false) checkLocalBookFileExist(book) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index 57dc774ef..e309c33dd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -288,14 +288,12 @@ data class TextChapter( } override fun onLayoutException(e: Throwable) { - isCompleted = true listener?.onLayoutException(e) listener = null } fun cancelLayout() { layout?.cancel() - isCompleted = true listener = null }