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 moveTo = "moveTo"
const val init = "init"
const val remove = "remove"
}

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

@ -45,7 +45,11 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
holder.itemView.apply {
iv_download.onClick {
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 {

Loading…
Cancel
Save