This commit is contained in:
Horis
2026-03-03 15:58:56 +08:00
parent 0de75c4ef7
commit 847d268db0
6 changed files with 60 additions and 31 deletions
@@ -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()
@@ -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()
}
}
@@ -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<ActivityMangaBinding, ReadMangaViewMode
networkChangedListener.register()
networkChangedListener.onNetworkChanged = {
// 当网络是可用状态且无需初始化时同步进度(初始化中已有同步进度逻辑)
if (AppConfig.syncBookProgressPlus && NetworkUtils.isAvailable() && !justInitData) {
if (AppConfig.syncBookProgressPlus && NetworkUtils.isAvailable() && !justInitData && ReadBook.inBookshelf) {
ReadManga.syncProgress({ progress -> sureNewProgress(progress) })
}
}
@@ -370,9 +371,11 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
} else {
ReadManga.uploadProgress()
}
Backup.autoBack(this)
}
}
if (!BuildConfig.DEBUG) {
Backup.autoBack(this)
}
ReadManga.cancelPreDownloadTask()
networkChangedListener.unRegister()
mScrollTimer.isEnabledPage = false
@@ -26,6 +26,8 @@ import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.mapParallelSafe
import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
@@ -98,7 +100,7 @@ class ReadMangaViewModel(application: Application) : BaseViewModel(application)
if (ReadManga.chapterChanged) {
// 有章节跳转不同步阅读进度
ReadManga.chapterChanged = false
} else {
} else if (ReadManga.inBookshelf) {
if (AppConfig.syncBookProgressPlus) {
ReadManga.syncProgress(
{ progress -> 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("已同步最新漫画阅读进度")
}
}
}
@@ -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
}
}
@@ -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("已同步最新阅读进度")
}
}
}