pull/52/head
kunfei 5 years ago
parent fe01078781
commit c01468f529
  1. 17
      app/src/main/java/io/legado/app/service/DownloadService.kt

@ -9,14 +9,14 @@ import io.legado.app.constant.Action
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
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.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import java.util.concurrent.Executors import java.util.concurrent.Executors
class DownloadService : BaseService() { class DownloadService : BaseService() {
private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher() private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher()
private var tasks: ArrayList<Coroutine<*>> = arrayListOf()
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -44,9 +44,9 @@ class DownloadService : BaseService() {
private fun download(bookUrl: String?, start: Int, end: Int) { private fun download(bookUrl: String?, start: Int, end: Int) {
if (bookUrl == null) return if (bookUrl == null) return
launch(IO) { val task = Coroutine.async {
val book = App.db.bookDao().getBook(bookUrl) ?: return@launch val book = App.db.bookDao().getBook(bookUrl) ?: return@async
val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@launch val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@async
val webBook = WebBook(bookSource) val webBook = WebBook(bookSource)
for (index in start..end) { for (index in start..end) {
App.db.bookChapterDao().getChapter(bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(bookUrl, index)?.let { chapter ->
@ -64,6 +64,13 @@ class DownloadService : BaseService() {
} }
} }
} }
tasks.add(task)
task.invokeOnCompletion {
tasks.remove(task)
if (tasks.isEmpty()) {
stopSelf()
}
}
} }
/** /**

Loading…
Cancel
Save