From d267874fa8cf929e130e10485b99d76cb14cb2b7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 12 Feb 2020 20:51:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/download/DownloadActivity.kt | 22 +++++++++++++++++++ .../legado/app/ui/download/DownloadAdapter.kt | 6 +++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt index dfa5f5b3d..f499287f2 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt @@ -11,12 +11,15 @@ import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.constant.EventBus import io.legado.app.data.entities.Book +import io.legado.app.help.BookHelp import io.legado.app.service.help.Download import io.legado.app.utils.applyTint import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_download.* +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class DownloadActivity : BaseActivity(R.layout.activity_download) { @@ -63,9 +66,28 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) { bookshelfLiveData = App.db.bookDao().observeDownload() bookshelfLiveData?.observe(this, Observer { adapter.setItems(it) + initCacheSize(it) }) } + private fun initCacheSize(books: List) { + launch(IO) { + books.forEach { book -> + val chapterCaches = hashSetOf() + 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() { observeEvent(EventBus.UP_DOWNLOAD) { if (it) { diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt index 28c119f8f..5b3a4cd50 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt @@ -11,6 +11,8 @@ import kotlinx.android.synthetic.main.item_download.view.* class DownloadAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_download) { + val cacheChapters = hashMapOf>() + override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList) { with(holder.itemView) { if (payloads.isEmpty()) { @@ -18,9 +20,9 @@ class DownloadAdapter(context: Context) : tv_author.text = context.getString(R.string.author_show, item.getRealAuthor()) tv_download.setText(R.string.loading) } else { - val count = payloads[0] as Int + val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0 tv_download.text = - context.getString(R.string.download_count, count, item.totalChapterNum) + context.getString(R.string.download_count, cacheSize, item.totalChapterNum) } } }