优化离线下载

pull/275/head
gedoor 4 years ago
parent 235289e3c3
commit 0a31be0817
  1. 10
      app/src/main/java/io/legado/app/service/DownloadService.kt

@ -126,6 +126,7 @@ class DownloadService : BaseService() {
private fun addDownloadData(bookUrl: String?, start: Int, end: Int) { private fun addDownloadData(bookUrl: String?, start: Int, end: Int) {
bookUrl ?: return bookUrl ?: return
if (downloadMap.containsKey(bookUrl)) { if (downloadMap.containsKey(bookUrl)) {
updateNotification(getString(R.string.already_in_download))
toast(R.string.already_in_download) toast(R.string.already_in_download)
return return
} }
@ -136,6 +137,8 @@ class DownloadService : BaseService() {
val chapters = CopyOnWriteArraySet<BookChapter>() val chapters = CopyOnWriteArraySet<BookChapter>()
chapters.addAll(it) chapters.addAll(it)
downloadMap[bookUrl] = chapters downloadMap[bookUrl] = chapters
} else {
Download.addLog("${getBook(bookUrl)?.name} is empty")
} }
} }
for (i in 0 until threadCount) { for (i in 0 until threadCount) {
@ -153,7 +156,7 @@ class DownloadService : BaseService() {
private fun download() { private fun download() {
downloadingCount += 1 downloadingCount += 1
tasks.add(Coroutine.async(this, context = searchPool) { val task = Coroutine.async(this, context = searchPool) {
if (!isActive) return@async if (!isActive) return@async
val bookChapter: BookChapter? = synchronized(this@DownloadService) { val bookChapter: BookChapter? = synchronized(this@DownloadService) {
downloadMap.forEach { downloadMap.forEach {
@ -224,7 +227,10 @@ class DownloadService : BaseService() {
postDownloading(true) postDownloading(true)
} }
} }
}) }.onError {
Download.addLog("ERROR:${it.localizedMessage}")
}
tasks.add(task)
} }
private fun postDownloading(hasChapter: Boolean) { private fun postDownloading(hasChapter: Boolean) {

Loading…
Cancel
Save