[优化] 依旧下载优化

This commit is contained in:
HapeLee
2026-04-29 13:23:57 +08:00
parent 00dc733ec8
commit 914efbb658
4 changed files with 22 additions and 19 deletions
@@ -592,7 +592,7 @@ object CacheBook {
return
}
val task = repository.downloadContentTask(
val task = repository.cacheContentTask(
scope = scope,
bookSource = bookSource,
book = book,
@@ -600,14 +600,12 @@ object CacheBook {
context = context,
start = CoroutineStart.LAZY,
executeContext = context,
).onSuccess { content ->
).onSuccess {
onSuccess(chapter)
downloadFinish(chapter, content)
}.onError {
onPreError(chapter, it)
delay(1000)
onPostError(chapter, it)
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}")
}.onCancel {
onCancel(chapterIndex)
}.onFinally {
@@ -62,6 +62,25 @@ class CacheDownloadRepository {
)
}
fun cacheContentTask(
scope: CoroutineScope,
bookSource: BookSource,
book: Book,
chapter: BookChapter,
context: CoroutineContext,
start: CoroutineStart = CoroutineStart.LAZY,
executeContext: CoroutineContext = context,
): Coroutine<Unit> {
return Coroutine.async(
scope = scope,
context = context,
start = start,
executeContext = executeContext,
) {
WebBook.getContentAwait(bookSource, book, chapter)
}
}
suspend fun downloadContentAwait(
bookSource: BookSource,
book: Book,
@@ -679,15 +679,9 @@ private fun BookshelfManageScreen(
val cacheCount = remember(renderVersion, book.bookUrl) {
viewModel.getCacheCount(book.bookUrl) ?: 0
}
val downloadState = viewModel.getBookDownloadState(book.bookUrl)
val isPreparingDownload = viewModel.isBookPreparingDownload(book.bookUrl)
val waitingDownloadCount = downloadState?.waitingCount ?: 0
val runningDownloadCount = downloadState?.runningIndices?.size ?: 0
val isDownloadingInCacheModel = viewModel.isBookDownloading(book.bookUrl)
val isDownloading = isPreparingDownload ||
waitingDownloadCount > 0 ||
runningDownloadCount > 0 ||
isDownloadingInCacheModel
val isDownloading = isPreparingDownload || isDownloadingInCacheModel
val downloadFailureText = viewModel.getDownloadFailureMessage(book.bookUrl)?.let {
stringResource(R.string.cache_download_failed, it)
}
@@ -27,7 +27,6 @@ import io.legado.app.help.book.isAudio
import io.legado.app.help.book.isLocal
import io.legado.app.help.book.removeType
import io.legado.app.model.CacheBook
import io.legado.app.model.cache.CacheBookDownloadState
import io.legado.app.help.config.LocalConfig
import io.legado.app.service.ExportBookService
import io.legado.app.ui.config.bookshelfConfig.BookshelfConfig
@@ -169,7 +168,6 @@ class BookshelfManageScreenViewModel(
val effects = _effects.asSharedFlow()
private val cacheCounts = ConcurrentHashMap<String, Int>()
private val downloadStates = ConcurrentHashMap<String, CacheBookDownloadState>()
private val pendingDownloadBookUrls = ConcurrentHashMap.newKeySet<String>()
private val downloadFailureMessages = ConcurrentHashMap<String, String>()
private var booksJob: Job? = null
@@ -309,10 +307,6 @@ class BookshelfManageScreenViewModel(
return CacheBook.cacheBookMap[bookUrl]?.isStop() == false
}
fun getBookDownloadState(bookUrl: String): CacheBookDownloadState? {
return downloadStates[bookUrl]
}
fun getDownloadFailureMessage(bookUrl: String): String? {
return downloadFailureMessages[bookUrl]
}
@@ -413,8 +407,6 @@ class BookshelfManageScreenViewModel(
message?.let { bookUrl to it }
}.toMap()
pendingDownloadBookUrls.removeAll(downloadState.books.keys)
downloadStates.clear()
downloadStates.putAll(downloadState.books)
successfulBookUrls.forEach { downloadFailureMessages.remove(it) }
downloadFailureMessages.putAll(failureMsgs)
_uiState.update { it.copy(isDownloadRunning = downloadState.isRunning) }