feat: 优化代码

pull/178/head
kunfei 5 years ago
parent 036bb3be53
commit 5a179e1659
  1. 20
      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.AppConfig
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -25,7 +26,7 @@ import java.util.concurrent.Executors
class DownloadService : BaseService() { class DownloadService : BaseService() {
private var searchPool = private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
private var tasks: ArrayList<Coroutine<*>> = arrayListOf() private var tasks = CompositeCoroutine()
private val handler = Handler() private val handler = Handler()
private var runnable: Runnable = Runnable { upDownload() } private var runnable: Runnable = Runnable { upDownload() }
private val downloadMap = hashMapOf<String, LinkedHashSet<BookChapter>>() private val downloadMap = hashMapOf<String, LinkedHashSet<BookChapter>>()
@ -125,11 +126,7 @@ class DownloadService : BaseService() {
chapter, chapter,
scope = this, scope = this,
context = searchPool context = searchPool
) ).onSuccess(IO) { content ->
//.onStart {
// notificationContent = "启动:" + chapter.title
//}
.onSuccess(IO) { content ->
downloadCount[entry.key]?.increaseSuccess() downloadCount[entry.key]?.increaseSuccess()
BookHelp.saveContent(book, chapter, content) BookHelp.saveContent(book, chapter, content)
} }
@ -165,7 +162,7 @@ class DownloadService : BaseService() {
tasks.add(task) tasks.add(task)
task.invokeOnCompletion { task.invokeOnCompletion {
tasks.remove(task) tasks.remove(task)
if (tasks.isEmpty()) { if (tasks.isEmpty) {
stopSelf() stopSelf()
} }
} }
@ -192,20 +189,21 @@ class DownloadService : BaseService() {
val notification = builder.build() val notification = builder.build()
startForeground(AppConst.notificationIdDownload, notification) startForeground(AppConst.notificationIdDownload, notification)
} }
}
class DownloadCount{
class DownloadCount {
@Volatile @Volatile
var downloadFinishedCount = 0 // 下载完成的条目数量 var downloadFinishedCount = 0 // 下载完成的条目数量
@Volatile @Volatile
var successCount = 0 //下载成功的条目数量 var successCount = 0 //下载成功的条目数量
fun increaseSuccess(){ fun increaseSuccess() {
++successCount; ++successCount;
} }
fun increaseFinished(){ fun increaseFinished() {
++downloadFinishedCount; ++downloadFinishedCount;
} }
}
} }
Loading…
Cancel
Save