From 64cb64dc6eaeb3d4e003e100334cad568f77e5e6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 6 Feb 2020 23:20:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/bookshelf/books/BaseBooksAdapter.kt | 3 +- .../main/bookshelf/books/BooksAdapterList.kt | 15 ++++-- .../main/bookshelf/books/BooksDiffCallBack.kt | 47 ++++++++++++++++--- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BaseBooksAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BaseBooksAdapter.kt index 9103d3c3c..1e8fda88b 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BaseBooksAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BaseBooksAdapter.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.main.bookshelf.books import android.content.Context +import androidx.core.os.bundleOf import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.Book @@ -11,7 +12,7 @@ abstract class BaseBooksAdapter(context: Context, layoutId: Int) : for (i in 0 until itemCount) { getItem(i)?.let { if (it.bookUrl == bookUrl) { - notifyItemChanged(i, 5) + notifyItemChanged(i, bundleOf(Pair("refresh", null))) return } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt index b35d6fd21..1c500a786 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.main.bookshelf.books import android.content.Context +import android.os.Bundle import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.constant.BookType @@ -15,8 +16,9 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : BaseBooksAdapter(context, R.layout.item_bookshelf_list) { override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList) { + val bundle = payloads.getOrNull(0) as? Bundle with(holder.itemView) { - if (payloads.isEmpty()) { + if (bundle == null) { ATH.applyBackgroundTint(this) tv_name.text = item.name tv_author.text = item.author @@ -37,9 +39,14 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : bv_unread.setHighlight(item.lastCheckCount > 0) } } else { - when (payloads[0]) { - 5 -> { - if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) { + bundle.keySet().map { + when (it) { + "name" -> tv_name.text = item.name + "author" -> tv_author.text = item.author + "durTitle" -> tv_read.text = item.durChapterTitle + "latestTitle" -> tv_last.text = item.latestChapterTitle + "cover" -> iv_cover.load(item.getDisplayCover(), item.name, item.author) + "refresh" -> if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) { bv_unread.invisible() rl_loading.show() } else { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt index fc4358f5e..481411bba 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.main.bookshelf.books +import androidx.core.os.bundleOf import androidx.recyclerview.widget.DiffUtil import io.legado.app.data.entities.Book @@ -22,13 +23,45 @@ class BooksDiffCallBack(private val oldItems: List, private val newItems: override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldItem = oldItems[oldItemPosition] val newItem = newItems[newItemPosition] - return oldItem.name == newItem.name - && oldItem.author == newItem.author - && oldItem.durChapterTitle == newItem.durChapterTitle - && oldItem.latestChapterTitle == newItem.latestChapterTitle - && oldItem.getDisplayCover() == newItem.getDisplayCover() - && oldItem.getUnreadChapterNum() == newItem.getUnreadChapterNum() - && oldItem.lastCheckCount == newItem.lastCheckCount + if (oldItem.name != newItem.name) + return false + if (oldItem.author != newItem.author) + return false + if (oldItem.durChapterTitle != newItem.durChapterTitle) + return false + if (oldItem.latestChapterTitle != newItem.latestChapterTitle) + return false + if (oldItem.lastCheckCount != newItem.lastCheckCount) + return false + if (oldItem.getDisplayCover() != newItem.getDisplayCover()) + return false + if (oldItem.getUnreadChapterNum() != newItem.getUnreadChapterNum()) + return false + return true + } + + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + val bundle = bundleOf() + if (oldItem.name != newItem.name) + bundle.putString("name", null) + if (oldItem.author != newItem.author) + bundle.putString("author", null) + if (oldItem.durChapterTitle != newItem.durChapterTitle) + bundle.putString("durTitle", null) + if (oldItem.latestChapterTitle != newItem.latestChapterTitle) + bundle.putString("latestTitle", null) + if (oldItem.getDisplayCover() != newItem.getDisplayCover()) + bundle.putString("cover", null) + if (oldItem.getUnreadChapterNum() != newItem.getUnreadChapterNum() + || oldItem.lastCheckCount != newItem.lastCheckCount + ) + bundle.putString("refresh", null) + if (bundle.isEmpty) { + return null + } + return bundle } } \ No newline at end of file