feat: 优化代码

pull/105/head
kunfei 5 years ago
parent 7b62b64178
commit 9d869ce50e
  1. 4
      app/src/main/java/io/legado/app/service/DownloadService.kt
  2. 11
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt

@ -68,7 +68,7 @@ class DownloadService : BaseService() {
searchPool.close() searchPool.close()
handler.removeCallbacks(runnable) handler.removeCallbacks(runnable)
super.onDestroy() super.onDestroy()
postEvent(EventBus.UP_DOWNLOAD, false) postEvent(EventBus.UP_DOWNLOAD, downloadMap)
} }
private fun addDownloadData(bookUrl: String?, start: Int, end: Int) { private fun addDownloadData(bookUrl: String?, start: Int, end: Int) {
@ -144,7 +144,7 @@ class DownloadService : BaseService() {
private fun upDownload() { private fun upDownload() {
updateNotification(notificationContent) updateNotification(notificationContent)
postEvent(EventBus.UP_DOWNLOAD, true) postEvent(EventBus.UP_DOWNLOAD, downloadMap)
handler.removeCallbacks(runnable) handler.removeCallbacks(runnable)
handler.postDelayed(runnable, 1000) handler.postDelayed(runnable, 1000)
} }

@ -105,15 +105,16 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
} }
override fun observeLiveBus() { override fun observeLiveBus() {
observeEvent<Boolean>(EventBus.UP_DOWNLOAD) { observeEvent<HashMap<String, LinkedHashSet<BookChapter>>>(EventBus.UP_DOWNLOAD) {
if (it) { if (it.isEmpty()) {
menu?.findItem(R.id.menu_download)?.setIcon(R.drawable.ic_stop_black_24dp) menu?.findItem(R.id.menu_download)?.setIcon(R.drawable.ic_play_24dp)
menu?.applyTint(this) menu?.applyTint(this)
adapter.notifyItemRangeChanged(0, adapter.getActualItemCount(), true)
} else { } else {
menu?.findItem(R.id.menu_download)?.setIcon(R.drawable.ic_play_24dp) menu?.findItem(R.id.menu_download)?.setIcon(R.drawable.ic_stop_black_24dp)
menu?.applyTint(this) menu?.applyTint(this)
} }
adapter.downloadMap = it
adapter.notifyItemRangeChanged(0, adapter.getActualItemCount(), true)
} }
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) { observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
adapter.cacheChapters[it.bookUrl]?.add(it.url) adapter.cacheChapters[it.bookUrl]?.add(it.url)

@ -5,6 +5,7 @@ import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import kotlinx.android.synthetic.main.item_download.view.* import kotlinx.android.synthetic.main.item_download.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -14,6 +15,7 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_download) { SimpleRecyclerAdapter<Book>(context, R.layout.item_download) {
val cacheChapters = hashMapOf<String, HashSet<String>>() val cacheChapters = hashMapOf<String, HashSet<String>>()
var downloadMap: HashMap<String, LinkedHashSet<BookChapter>>? = null
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {

Loading…
Cancel
Save