feat: 优化代码

pull/98/head
kunfei 5 years ago
parent 56fa86ee0b
commit d267874fa8
  1. 22
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt

@ -11,12 +11,15 @@ import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.help.BookHelp
import io.legado.app.service.help.Download import io.legado.app.service.help.Download
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_download.* import kotlinx.android.synthetic.main.activity_download.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class DownloadActivity : BaseActivity(R.layout.activity_download) { class DownloadActivity : BaseActivity(R.layout.activity_download) {
@ -63,9 +66,28 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) {
bookshelfLiveData = App.db.bookDao().observeDownload() bookshelfLiveData = App.db.bookDao().observeDownload()
bookshelfLiveData?.observe(this, Observer { bookshelfLiveData?.observe(this, Observer {
adapter.setItems(it) adapter.setItems(it)
initCacheSize(it)
}) })
} }
private fun initCacheSize(books: List<Book>) {
launch(IO) {
books.forEach { book ->
val chapterCaches = hashSetOf<String>()
val cacheNames = BookHelp.getChapterFiles(book)
App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter ->
if (cacheNames.contains(BookHelp.formatChapterName(chapter))) {
chapterCaches.add(chapter.url)
}
}
adapter.cacheChapters[book.bookUrl] = chapterCaches
withContext(Dispatchers.Main) {
adapter.notifyItemRangeChanged(0, adapter.getActualItemCount(), true)
}
}
}
}
override fun observeLiveBus() { override fun observeLiveBus() {
observeEvent<Boolean>(EventBus.UP_DOWNLOAD) { observeEvent<Boolean>(EventBus.UP_DOWNLOAD) {
if (it) { if (it) {

@ -11,6 +11,8 @@ import kotlinx.android.synthetic.main.item_download.view.*
class DownloadAdapter(context: Context) : class DownloadAdapter(context: Context) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_download) { SimpleRecyclerAdapter<Book>(context, R.layout.item_download) {
val cacheChapters = hashMapOf<String, HashSet<String>>()
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) {
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
@ -18,9 +20,9 @@ class DownloadAdapter(context: Context) :
tv_author.text = context.getString(R.string.author_show, item.getRealAuthor()) tv_author.text = context.getString(R.string.author_show, item.getRealAuthor())
tv_download.setText(R.string.loading) tv_download.setText(R.string.loading)
} else { } else {
val count = payloads[0] as Int val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0
tv_download.text = tv_download.text =
context.getString(R.string.download_count, count, item.totalChapterNum) context.getString(R.string.download_count, cacheSize, item.totalChapterNum)
} }
} }
} }

Loading…
Cancel
Save