From 5a179e1659f02baf210dca195527fac307baff3a Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 29 Mar 2020 22:22:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/DownloadService.kt | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index adae1ae10..1cbfe44f4 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -13,6 +13,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp +import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import io.legado.app.utils.postEvent @@ -25,7 +26,7 @@ import java.util.concurrent.Executors class DownloadService : BaseService() { private var searchPool = Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() - private var tasks: ArrayList> = arrayListOf() + private var tasks = CompositeCoroutine() private val handler = Handler() private var runnable: Runnable = Runnable { upDownload() } private val downloadMap = hashMapOf>() @@ -125,11 +126,7 @@ class DownloadService : BaseService() { chapter, scope = this, context = searchPool - ) - //.onStart { - // notificationContent = "启动:" + chapter.title - //} - .onSuccess(IO) { content -> + ).onSuccess(IO) { content -> downloadCount[entry.key]?.increaseSuccess() BookHelp.saveContent(book, chapter, content) } @@ -165,7 +162,7 @@ class DownloadService : BaseService() { tasks.add(task) task.invokeOnCompletion { tasks.remove(task) - if (tasks.isEmpty()) { + if (tasks.isEmpty) { stopSelf() } } @@ -192,20 +189,21 @@ class DownloadService : BaseService() { val notification = builder.build() startForeground(AppConst.notificationIdDownload, notification) } -} -class DownloadCount{ - @Volatile - var downloadFinishedCount = 0 // 下载完成的条目数量 - @Volatile - var successCount = 0 //下载成功的条目数量 + class DownloadCount { + @Volatile + var downloadFinishedCount = 0 // 下载完成的条目数量 - fun increaseSuccess(){ - ++successCount; - } + @Volatile + var successCount = 0 //下载成功的条目数量 + + fun increaseSuccess() { + ++successCount; + } - fun increaseFinished(){ - ++downloadFinishedCount; + fun increaseFinished() { + ++downloadFinishedCount; + } } } \ No newline at end of file