pull/52/head
kunfei 5 years ago
parent 03fe6ef2ee
commit 5c2f394304
  1. 25
      app/src/main/java/io/legado/app/service/DownloadService.kt

@ -2,12 +2,17 @@ package io.legado.app.service
import android.content.Intent import android.content.Intent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.Action 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.IntentHelp import io.legado.app.help.IntentHelp
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() {
@ -39,7 +44,25 @@ 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 book = App.db.bookDao().getBook(bookUrl) ?: return@launch
val bookSource = App.db.bookSourceDao().getBookSource(book.origin) ?: return@launch
val webBook = WebBook(bookSource)
for (index in start..end) {
App.db.bookChapterDao().getChapter(bookUrl, index)?.let { chapter ->
webBook.getContent(book, chapter, scope = this, context = searchPool)
.onStart {
updateNotification("${chapter.title}开始下载")
}
.onSuccess { content ->
content?.let {
BookHelp.saveContent(book, chapter, content)
}
updateNotification("${chapter.title}下载完成")
}
}
}
}
} }
/** /**

Loading…
Cancel
Save