feat: 优化代码

pull/105/head
kunfei 5 years ago
parent c2f314cf99
commit 916d651ffa
  1. 1
      app/src/main/java/io/legado/app/constant/IntentAction.kt
  2. 55
      app/src/main/java/io/legado/app/service/DownloadService.kt
  3. 8
      app/src/main/java/io/legado/app/service/help/Download.kt
  4. 6
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt

@ -17,4 +17,5 @@ object IntentAction {
const val next = "next" const val next = "next"
const val moveTo = "moveTo" const val moveTo = "moveTo"
const val init = "init" const val init = "init"
const val remove = "remove"
} }

@ -58,6 +58,7 @@ class DownloadService : BaseService() {
intent.getIntExtra("start", 0), intent.getIntExtra("start", 0),
intent.getIntExtra("end", 0) intent.getIntExtra("end", 0)
) )
IntentAction.remove -> removeDownload(intent.getStringExtra("bookUrl"))
IntentAction.stop -> stopDownload() IntentAction.stop -> stopDownload()
} }
} }
@ -91,6 +92,11 @@ class DownloadService : BaseService() {
} }
} }
private fun removeDownload(bookUrl: String?) {
downloadMap.remove(bookUrl)
finalMap.remove(bookUrl)
}
private fun download() { private fun download() {
val task = Coroutine.async(this, context = searchPool) { val task = Coroutine.async(this, context = searchPool) {
downloadMap.forEach { entry -> downloadMap.forEach { entry ->
@ -102,30 +108,37 @@ class DownloadService : BaseService() {
val webBook = WebBook(bookSource) val webBook = WebBook(bookSource)
entry.value.forEach { chapter -> entry.value.forEach { chapter ->
if (!isActive) return@async if (!isActive) return@async
if (!BookHelp.hasContent(book, chapter)) { if (downloadMap.containsKey(book.bookUrl)) {
webBook.getContent(book, chapter, scope = this, context = searchPool) if (!BookHelp.hasContent(book, chapter)) {
.onStart { webBook.getContent(
notificationContent = chapter.title book,
} chapter,
.onSuccess(IO) { content -> scope = this,
content?.let { context = searchPool
BookHelp.saveContent(book, chapter, content) )
.onStart {
notificationContent = chapter.title
}
.onSuccess(IO) { content ->
content?.let {
BookHelp.saveContent(book, chapter, content)
}
} }
} .onFinally(IO) {
.onFinally(IO) { synchronized(this@DownloadService) {
synchronized(this@DownloadService) { val chapterMap =
val chapterMap = finalMap[book.bookUrl]
finalMap[book.bookUrl] ?: linkedSetOf<BookChapter>().apply {
?: linkedSetOf<BookChapter>().apply { finalMap[book.bookUrl] = this
finalMap[book.bookUrl] = this }
} chapterMap.add(chapter)
chapterMap.add(chapter) if (chapterMap.size == entry.value.size) {
if (chapterMap.size == entry.value.size) { downloadMap.remove(book.bookUrl)
downloadMap.remove(book.bookUrl) finalMap.remove(book.bookUrl)
finalMap.remove(book.bookUrl) }
} }
} }
} }
} }
} }
} }

@ -17,6 +17,14 @@ object Download {
} }
} }
fun remove(context: Context, bookUrl: String) {
Intent(context, DownloadService::class.java).let {
it.action = IntentAction.remove
it.putExtra("bookUrl", bookUrl)
context.startService(it)
}
}
fun stop(context: Context) { fun stop(context: Context) {
Intent(context, DownloadService::class.java).let { Intent(context, DownloadService::class.java).let {
it.action = IntentAction.stop it.action = IntentAction.stop

@ -45,7 +45,11 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
holder.itemView.apply { holder.itemView.apply {
iv_download.onClick { iv_download.onClick {
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
Download.start(context, it.bookUrl, 0, it.totalChapterNum) if (downloadMap?.containsKey(it.bookUrl) == true) {
Download.remove(context, it.bookUrl)
} else {
Download.start(context, it.bookUrl, 0, it.totalChapterNum)
}
} }
} }
tv_export.onClick { tv_export.onClick {

Loading…
Cancel
Save