[优化] 依旧下载优化
This commit is contained in:
@@ -592,7 +592,7 @@ object CacheBook {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val task = repository.downloadContentTask(
|
val task = repository.cacheContentTask(
|
||||||
scope = scope,
|
scope = scope,
|
||||||
bookSource = bookSource,
|
bookSource = bookSource,
|
||||||
book = book,
|
book = book,
|
||||||
@@ -600,14 +600,12 @@ object CacheBook {
|
|||||||
context = context,
|
context = context,
|
||||||
start = CoroutineStart.LAZY,
|
start = CoroutineStart.LAZY,
|
||||||
executeContext = context,
|
executeContext = context,
|
||||||
).onSuccess { content ->
|
).onSuccess {
|
||||||
onSuccess(chapter)
|
onSuccess(chapter)
|
||||||
downloadFinish(chapter, content)
|
|
||||||
}.onError {
|
}.onError {
|
||||||
onPreError(chapter, it)
|
onPreError(chapter, it)
|
||||||
delay(1000)
|
delay(1000)
|
||||||
onPostError(chapter, it)
|
onPostError(chapter, it)
|
||||||
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}")
|
|
||||||
}.onCancel {
|
}.onCancel {
|
||||||
onCancel(chapterIndex)
|
onCancel(chapterIndex)
|
||||||
}.onFinally {
|
}.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(
|
suspend fun downloadContentAwait(
|
||||||
bookSource: BookSource,
|
bookSource: BookSource,
|
||||||
book: Book,
|
book: Book,
|
||||||
|
|||||||
@@ -679,15 +679,9 @@ private fun BookshelfManageScreen(
|
|||||||
val cacheCount = remember(renderVersion, book.bookUrl) {
|
val cacheCount = remember(renderVersion, book.bookUrl) {
|
||||||
viewModel.getCacheCount(book.bookUrl) ?: 0
|
viewModel.getCacheCount(book.bookUrl) ?: 0
|
||||||
}
|
}
|
||||||
val downloadState = viewModel.getBookDownloadState(book.bookUrl)
|
|
||||||
val isPreparingDownload = viewModel.isBookPreparingDownload(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 isDownloadingInCacheModel = viewModel.isBookDownloading(book.bookUrl)
|
||||||
val isDownloading = isPreparingDownload ||
|
val isDownloading = isPreparingDownload || isDownloadingInCacheModel
|
||||||
waitingDownloadCount > 0 ||
|
|
||||||
runningDownloadCount > 0 ||
|
|
||||||
isDownloadingInCacheModel
|
|
||||||
val downloadFailureText = viewModel.getDownloadFailureMessage(book.bookUrl)?.let {
|
val downloadFailureText = viewModel.getDownloadFailureMessage(book.bookUrl)?.let {
|
||||||
stringResource(R.string.cache_download_failed, it)
|
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.isLocal
|
||||||
import io.legado.app.help.book.removeType
|
import io.legado.app.help.book.removeType
|
||||||
import io.legado.app.model.CacheBook
|
import io.legado.app.model.CacheBook
|
||||||
import io.legado.app.model.cache.CacheBookDownloadState
|
|
||||||
import io.legado.app.help.config.LocalConfig
|
import io.legado.app.help.config.LocalConfig
|
||||||
import io.legado.app.service.ExportBookService
|
import io.legado.app.service.ExportBookService
|
||||||
import io.legado.app.ui.config.bookshelfConfig.BookshelfConfig
|
import io.legado.app.ui.config.bookshelfConfig.BookshelfConfig
|
||||||
@@ -169,7 +168,6 @@ class BookshelfManageScreenViewModel(
|
|||||||
val effects = _effects.asSharedFlow()
|
val effects = _effects.asSharedFlow()
|
||||||
|
|
||||||
private val cacheCounts = ConcurrentHashMap<String, Int>()
|
private val cacheCounts = ConcurrentHashMap<String, Int>()
|
||||||
private val downloadStates = ConcurrentHashMap<String, CacheBookDownloadState>()
|
|
||||||
private val pendingDownloadBookUrls = ConcurrentHashMap.newKeySet<String>()
|
private val pendingDownloadBookUrls = ConcurrentHashMap.newKeySet<String>()
|
||||||
private val downloadFailureMessages = ConcurrentHashMap<String, String>()
|
private val downloadFailureMessages = ConcurrentHashMap<String, String>()
|
||||||
private var booksJob: Job? = null
|
private var booksJob: Job? = null
|
||||||
@@ -309,10 +307,6 @@ class BookshelfManageScreenViewModel(
|
|||||||
return CacheBook.cacheBookMap[bookUrl]?.isStop() == false
|
return CacheBook.cacheBookMap[bookUrl]?.isStop() == false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getBookDownloadState(bookUrl: String): CacheBookDownloadState? {
|
|
||||||
return downloadStates[bookUrl]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getDownloadFailureMessage(bookUrl: String): String? {
|
fun getDownloadFailureMessage(bookUrl: String): String? {
|
||||||
return downloadFailureMessages[bookUrl]
|
return downloadFailureMessages[bookUrl]
|
||||||
}
|
}
|
||||||
@@ -413,8 +407,6 @@ class BookshelfManageScreenViewModel(
|
|||||||
message?.let { bookUrl to it }
|
message?.let { bookUrl to it }
|
||||||
}.toMap()
|
}.toMap()
|
||||||
pendingDownloadBookUrls.removeAll(downloadState.books.keys)
|
pendingDownloadBookUrls.removeAll(downloadState.books.keys)
|
||||||
downloadStates.clear()
|
|
||||||
downloadStates.putAll(downloadState.books)
|
|
||||||
successfulBookUrls.forEach { downloadFailureMessages.remove(it) }
|
successfulBookUrls.forEach { downloadFailureMessages.remove(it) }
|
||||||
downloadFailureMessages.putAll(failureMsgs)
|
downloadFailureMessages.putAll(failureMsgs)
|
||||||
_uiState.update { it.copy(isDownloadRunning = downloadState.isRunning) }
|
_uiState.update { it.copy(isDownloadRunning = downloadState.isRunning) }
|
||||||
|
|||||||
Reference in New Issue
Block a user