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. 15
      app/src/main/java/io/legado/app/service/DownloadService.kt
  3. 8
      app/src/main/java/io/legado/app/service/help/Download.kt
  4. 4
      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,8 +108,14 @@ 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 (downloadMap.containsKey(book.bookUrl)) {
if (!BookHelp.hasContent(book, chapter)) { if (!BookHelp.hasContent(book, chapter)) {
webBook.getContent(book, chapter, scope = this, context = searchPool) webBook.getContent(
book,
chapter,
scope = this,
context = searchPool
)
.onStart { .onStart {
notificationContent = chapter.title notificationContent = chapter.title
} }
@ -129,6 +141,7 @@ class DownloadService : BaseService() {
} }
} }
} }
}
} }
} }

@ -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,9 +45,13 @@ 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 {
if (downloadMap?.containsKey(it.bookUrl) == true) {
Download.remove(context, it.bookUrl)
} else {
Download.start(context, it.bookUrl, 0, it.totalChapterNum) Download.start(context, it.bookUrl, 0, it.totalChapterNum)
} }
} }
}
tv_export.onClick { tv_export.onClick {
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
val cacheSize = cacheChapters[it.bookUrl]?.size ?: 0 val cacheSize = cacheChapters[it.bookUrl]?.size ?: 0

Loading…
Cancel
Save