From 847d268db096c1ec62b6dabf2848ec619c88f694 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Tue, 3 Mar 2026 15:58:56 +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 --- .../main/java/io/legado/app/model/ReadBook.kt | 31 +++++++++++++------ .../java/io/legado/app/model/ReadManga.kt | 24 ++++++++------ .../app/ui/book/manga/ReadMangaActivity.kt | 7 +++-- .../app/ui/book/manga/ReadMangaViewModel.kt | 7 ++++- .../app/ui/book/read/ReadBookActivity.kt | 13 ++++---- .../app/ui/book/read/ReadBookViewModel.kt | 9 +++--- 6 files changed, 60 insertions(+), 31 deletions(-) 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 30ec6c6d4..a20ae7ffe 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -96,6 +96,7 @@ object ReadBook : CoroutineScope by MainScope() { val executor = globalExecutor fun resetData(book: Book) { + releaseAndCancel() ReadBook.book = book readRecord.bookName = book.name readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0 @@ -125,6 +126,7 @@ object ReadBook : CoroutineScope by MainScope() { } fun upData(book: Book) { + releaseAndCancel() ReadBook.book = book chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl) simulatedChapterSize = if (book.readSimulating()) { @@ -863,17 +865,22 @@ object ReadBook : CoroutineScope by MainScope() { val bookSource = bookSource ?: return val book = book ?: return if (!book.canUpdate) return + if (chapterSize - durChapterIndex - 1 >= 3) return if (System.currentTimeMillis() - book.lastCheckTime < 600000) return book.lastCheckTime = System.currentTimeMillis() + val oldBook = book.copy() WebBook.getChapterList(this, bookSource, book).onSuccess(IO) { cList -> - if (book.bookUrl == ReadBook.book?.bookUrl - && cList.size > chapterSize - ) { - appDb.bookChapterDao.delByBook(book.bookUrl) + ensureActive() + if (cList.size > chapterSize) { + if (oldBook.bookUrl == book.bookUrl) { + appDb.bookDao.update(book) + } else { + appDb.bookDao.replace(oldBook, book) + BookHelp.updateCacheFolder(oldBook, book) + } + appDb.bookChapterDao.delByBook(oldBook.bookUrl) appDb.bookChapterDao.insert(*cList.toTypedArray()) - saveRead() - chapterSize = cList.size - simulatedChapterSize = book.simulatedTotalChapterNum() + onChapterListUpdated(book, false) nextTextChapter ?: loadContent(durChapterIndex + 1) } } @@ -922,6 +929,7 @@ object ReadBook : CoroutineScope by MainScope() { if (book?.isLocal == true) return executor.execute { if (AppConfig.preDownloadNum < 2) { + upToc() return@execute } preDownloadTask?.cancel() @@ -955,7 +963,7 @@ object ReadBook : CoroutineScope by MainScope() { } } - fun onChapterListUpdated(newBook: Book) { + fun onChapterListUpdated(newBook: Book, loadContent: Boolean = true) { if (newBook.isSameNameAuthor(book)) { book = newBook chapterSize = newBook.totalChapterNum @@ -963,9 +971,10 @@ object ReadBook : CoroutineScope by MainScope() { if (simulatedChapterSize > 0 && durChapterIndex > simulatedChapterSize - 1) { durChapterIndex = simulatedChapterSize - 1 } + callBack?.upMenuView() if (callBack == null) { clearTextChapter() - } else { + } else if (loadContent) { loadContent(true) } } @@ -997,6 +1006,10 @@ object ReadBook : CoroutineScope by MainScope() { if (callBack === cb) { callBack = null } + releaseAndCancel() + } + + private fun releaseAndCancel() { msg = null preDownloadTask?.cancel() downloadScope.coroutineContext.cancelChildren() diff --git a/app/src/main/java/io/legado/app/model/ReadManga.kt b/app/src/main/java/io/legado/app/model/ReadManga.kt index d2be860c3..3fca5f5b7 100644 --- a/app/src/main/java/io/legado/app/model/ReadManga.kt +++ b/app/src/main/java/io/legado/app/model/ReadManga.kt @@ -380,6 +380,7 @@ object ReadManga : CoroutineScope by MainScope() { if (book?.isLocal == true) return executor.execute { if (AppConfig.preDownloadNum < 2) { + upToc() return@execute } preDownloadTask?.cancel() @@ -463,17 +464,22 @@ object ReadManga : CoroutineScope by MainScope() { val bookSource = bookSource ?: return val book = book ?: return if (!book.canUpdate) return + if (chapterSize - durChapterIndex - 1 >= 3) return if (System.currentTimeMillis() - book.lastCheckTime < 600000) return book.lastCheckTime = System.currentTimeMillis() + val oldBook = book.copy() WebBook.getChapterList(this, bookSource, book).onSuccess(IO) { cList -> - if (book.bookUrl == ReadManga.book?.bookUrl - && cList.size > chapterSize - ) { - appDb.bookChapterDao.delByBook(book.bookUrl) + ensureActive() + if (cList.size > chapterSize) { + if (oldBook.bookUrl == book.bookUrl) { + appDb.bookDao.update(book) + } else { + appDb.bookDao.replace(oldBook, book) + BookHelp.updateCacheFolder(oldBook, book) + } + appDb.bookChapterDao.delByBook(oldBook.bookUrl) appDb.bookChapterDao.insert(*cList.toTypedArray()) - saveRead() - chapterSize = cList.size - simulatedChapterSize = book.simulatedTotalChapterNum() + onChapterListUpdated(book, false) nextMangaChapter ?: loadContent(durChapterIndex + 1) } } @@ -553,7 +559,7 @@ object ReadManga : CoroutineScope by MainScope() { mCallback?.loadFail(msg, retry) } - fun onChapterListUpdated(newBook: Book) { + fun onChapterListUpdated(newBook: Book, loadContent: Boolean = true) { if (newBook.isSameNameAuthor(book)) { book = newBook chapterSize = newBook.totalChapterNum @@ -563,7 +569,7 @@ object ReadManga : CoroutineScope by MainScope() { } if (mCallback == null) { clearMangaChapter() - } else { + } else if (loadContent) { loadContent() } } diff --git a/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt b/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt index 06b1983c0..82bc0b778 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/ReadMangaActivity.kt @@ -36,6 +36,7 @@ import io.legado.app.help.book.removeType import io.legado.app.help.config.AppConfig import io.legado.app.help.storage.Backup import io.legado.app.lib.dialogs.alert +import io.legado.app.model.ReadBook import io.legado.app.model.ReadManga import io.legado.app.receiver.NetworkChangedListener import io.legado.app.ui.book.changesource.ChangeBookSourceDialog @@ -348,7 +349,7 @@ class ReadMangaActivity : VMBaseActivity sureNewProgress(progress) }) } } @@ -370,9 +371,11 @@ class ReadMangaActivity : VMBaseActivity ReadManga.mCallback?.sureNewProgress(progress) }) @@ -129,6 +131,7 @@ class ReadMangaViewModel(application: Application) : BaseViewModel(application) ReadManga.onChapterListUpdated(book) return true }.onFailure { + currentCoroutineContext().ensureActive() //加载章节出错 ReadManga.mCallback?.loadFail(appCtx.getString(R.string.error_load_toc)) return false @@ -145,6 +148,7 @@ class ReadMangaViewModel(application: Application) : BaseViewModel(application) WebBook.getBookInfoAwait(source, book, canReName = false) return true } catch (e: Throwable) { + currentCoroutineContext().ensureActive() ReadManga.mCallback?.loadFail("详情页出错: ${e.localizedMessage}") return false } @@ -220,6 +224,7 @@ class ReadMangaViewModel(application: Application) : BaseViewModel(application) } else if (progress.durChapterIndex < book.simulatedTotalChapterNum()) { ReadManga.setProgress(progress) AppLog.put("自动同步阅读进度成功《${book.name}》 ${progress.durChapterTitle}") + context.toastOnUi("已同步最新漫画阅读进度") } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index fb6705500..b1e55e234 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -353,7 +353,7 @@ class ReadBookActivity : BaseReadBookActivity(), networkChangedListener.register() networkChangedListener.onNetworkChanged = { // 当网络是可用状态且无需初始化时同步进度(初始化中已有同步进度逻辑) - if (AppConfig.syncBookProgressPlus && NetworkUtils.isAvailable() && !justInitData) { + if (AppConfig.syncBookProgressPlus && NetworkUtils.isAvailable() && !justInitData && ReadBook.inBookshelf) { ReadBook.syncProgress({ progress -> sureNewProgress(progress) }) } } @@ -367,12 +367,14 @@ class ReadBookActivity : BaseReadBookActivity(), ReadBook.cancelPreDownloadTask() unregisterReceiver(timeBatteryReceiver) upSystemUiVisibility() - if (!BuildConfig.DEBUG) { + if (!BuildConfig.DEBUG && ReadBook.inBookshelf) { if (AppConfig.syncBookProgressPlus) { ReadBook.syncProgress() } else { ReadBook.uploadProgress() } + } + if (!BuildConfig.DEBUG) { Backup.autoBack(this) } justInitData = false @@ -440,12 +442,11 @@ class ReadBookActivity : BaseReadBookActivity(), } } lifecycleScope.launch { - menu.findItem(R.id.menu_get_progress)?.isVisible = withContext(IO) { - AppWebDav.isOk - } - menu.findItem(R.id.menu_cover_progress)?.isVisible = withContext(IO) { + val show = ReadBook.inBookshelf && withContext(IO) { AppWebDav.isOk } + menu.findItem(R.id.menu_get_progress)?.isVisible = show + menu.findItem(R.id.menu_cover_progress)?.isVisible = show } } 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 6871c9627..5c1f0c7c8 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 @@ -39,6 +39,7 @@ import io.legado.app.utils.mapParallelSafe import io.legado.app.utils.postEvent import io.legado.app.utils.toStringArray import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collect @@ -52,7 +53,6 @@ import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException import java.io.FileOutputStream -import kotlin.coroutines.coroutineContext /** * 阅读界面数据处理 @@ -134,7 +134,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (ReadBook.chapterChanged) { // 有章节跳转不同步阅读进度 ReadBook.chapterChanged = false - } else if (!isSameBook || !BaseReadAloudService.isRun) { + } else if (!(isSameBook && BaseReadAloudService.isRun) && ReadBook.inBookshelf) { if (AppConfig.syncBookProgressPlus) { ReadBook.syncProgress({ progress -> ReadBook.callBack?.sureNewProgress(progress) }) } else { @@ -169,7 +169,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { WebBook.getBookInfoAwait(source, book, canReName = false) return true } catch (e: Throwable) { - coroutineContext.ensureActive() + currentCoroutineContext().ensureActive() ReadBook.upMsg("详情页出错: ${e.localizedMessage}") return false } @@ -225,7 +225,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.onChapterListUpdated(book) return true }.onFailure { - coroutineContext.ensureActive() + currentCoroutineContext().ensureActive() ReadBook.upMsg(context.getString(R.string.error_load_toc)) return false } @@ -256,6 +256,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } else if (progress.durChapterIndex < book.simulatedTotalChapterNum()) { ReadBook.setProgress(progress) AppLog.put("自动同步阅读进度成功《${book.name}》 ${progress.durChapterTitle}") + context.toastOnUi("已同步最新阅读进度") } } }