From 96b3846313137ed0c9302be0805bb8d0789c8f12 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 7 Feb 2020 09:24:23 +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 --- .../legado/app/ui/book/search/DiffCallBack.kt | 49 ++++-- .../app/ui/book/search/SearchAdapter.kt | 156 ++++++++---------- 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt index 37158ea5b..a4467c1f7 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt @@ -3,21 +3,48 @@ package io.legado.app.ui.book.search import androidx.recyclerview.widget.DiffUtil import io.legado.app.data.entities.SearchBook -class DiffCallBack : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean { - return oldItem.name == newItem.name - && oldItem.author == newItem.author +class DiffCallBack(private val oldItems: List, private val newItems: List) : + DiffUtil.Callback() { + + override fun getNewListSize(): Int { + return newItems.size + } + + override fun getOldListSize(): Int { + return oldItems.size } - override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean { - return oldItem.origins?.size == newItem.origins?.size - && (oldItem.coverUrl == newItem.coverUrl || !oldItem.coverUrl.isNullOrEmpty()) - && (oldItem.kind == newItem.kind || !oldItem.kind.isNullOrEmpty()) - && (oldItem.latestChapterTitle == newItem.latestChapterTitle || !oldItem.kind.isNullOrEmpty()) - && oldItem.intro?.length ?: 0 > newItem.intro?.length ?: 0 + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + return true + } + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + if (oldItem.name != newItem.name) { + return false + } + if (oldItem.author != newItem.author) { + return false + } + if (oldItem.kind != newItem.kind) { + return false + } + if (oldItem.latestChapterTitle != newItem.latestChapterTitle) { + return false + } + if (oldItem.intro != newItem.intro) { + return false + } + if (oldItem.coverUrl != newItem.coverUrl) { + return false + } + return true } - override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? { + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] return when { oldItem.origins?.size != newItem.origins?.size -> 1 oldItem.coverUrl != newItem.coverUrl -> 2 diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 4a725e854..c95f5eb7e 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -1,12 +1,12 @@ package io.legado.app.ui.book.search import android.content.Context +import android.os.Bundle import android.view.View import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.SearchBook -import io.legado.app.help.ImageLoader import io.legado.app.utils.gone import io.legado.app.utils.visible import kotlinx.android.synthetic.main.item_bookshelf_list.view.iv_cover @@ -18,10 +18,11 @@ class SearchAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_search) { override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) { - if (payloads.isEmpty()) { + val bundle = payloads.getOrNull(0) as? Bundle + if (bundle == null) { bind(holder.itemView, item) } else { - bindChange(holder.itemView, item, payloads) + bindChange(holder.itemView, item, bundle) } } @@ -30,43 +31,9 @@ class SearchAdapter(context: Context, val callBack: CallBack) : tv_name.text = searchBook.name tv_author.text = context.getString(R.string.author_show, searchBook.author) bv_originCount.setBadgeCount(searchBook.origins?.size ?: 1) - if (searchBook.latestChapterTitle.isNullOrEmpty()) { - tv_lasted.gone() - } else { - tv_lasted.text = context.getString(R.string.lasted_show, searchBook.latestChapterTitle) - tv_lasted.visible() - } + upLasted(itemView, searchBook.latestChapterTitle) tv_introduce.text = context.getString(R.string.intro_show, searchBook.intro) - val kinds = searchBook.getKindList() - if (kinds.isEmpty()) { - ll_kind.gone() - } else { - ll_kind.visible() - for (index in 0..2) { - if (kinds.size > index) { - when (index) { - 0 -> { - tv_kind.text = kinds[index] - tv_kind.visible() - } - 1 -> { - tv_kind_1.text = kinds[index] - tv_kind_1.visible() - } - 2 -> { - tv_kind_2.text = kinds[index] - tv_kind_2.visible() - } - } - } else { - when (index) { - 0 -> tv_kind.gone() - 1 -> tv_kind_1.gone() - 2 -> tv_kind_2.gone() - } - } - } - } + upKind(itemView, searchBook.getKindList()) iv_cover.load(searchBook.coverUrl, searchBook.name, searchBook.author) onClick { callBack.showBookInfo(searchBook.name, searchBook.author) @@ -74,64 +41,71 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } - private fun bindChange(itemView: View, searchBook: SearchBook, payloads: MutableList) { + private fun bindChange(itemView: View, searchBook: SearchBook, bundle: Bundle) { with(itemView) { - when (payloads[0]) { - 1 -> bv_originCount.setBadgeCount(searchBook.origins?.size ?: 1) - 2 -> searchBook.coverUrl.let { - ImageLoader.load(context, it)//Glide自动识别http://和file:// - .placeholder(R.drawable.image_cover_default) - .error(R.drawable.image_cover_default) - .centerCrop() - .into(iv_cover) + bundle.keySet().map { + when (it) { + "name" -> tv_name.text = searchBook.name + "author" -> tv_author.text = + context.getString(R.string.author_show, searchBook.author) + "originCount" -> bv_originCount.setBadgeCount(searchBook.origins?.size ?: 1) + "lasted" -> upLasted(itemView, searchBook.latestChapterTitle) + "introduce" -> tv_introduce.text = + context.getString(R.string.intro_show, searchBook.intro) + "kind" -> upKind(itemView, searchBook.getKindList()) + "cover" -> iv_cover.load( + searchBook.coverUrl, + searchBook.name, + searchBook.author + ) } - 3 -> { - val kinds = searchBook.getKindList() - if (kinds.isEmpty()) { - ll_kind.gone() - } else { - ll_kind.visible() - for (index in 0..2) { - if (kinds.size > index) { - when (index) { - 0 -> { - tv_kind.text = kinds[index] - tv_kind.visible() - } - 1 -> { - tv_kind_1.text = kinds[index] - tv_kind_1.visible() - } - 2 -> { - tv_kind_2.text = kinds[index] - tv_kind_2.visible() - } - } - } else { - when (index) { - 0 -> tv_kind.gone() - 1 -> tv_kind_1.gone() - 2 -> tv_kind_2.gone() - } - } + } + } + } + + private fun upLasted(itemView: View, latestChapterTitle: String?) { + with(itemView) { + if (latestChapterTitle.isNullOrEmpty()) { + tv_lasted.gone() + } else { + tv_lasted.text = + context.getString( + R.string.lasted_show, + latestChapterTitle + ) + tv_lasted.visible() + } + } + } + + private fun upKind(itemView: View, kinds: List) = with(itemView) { + if (kinds.isEmpty()) { + ll_kind.gone() + } else { + ll_kind.visible() + for (index in 0..2) { + if (kinds.size > index) { + when (index) { + 0 -> { + tv_kind.text = kinds[index] + tv_kind.visible() + } + 1 -> { + tv_kind_1.text = kinds[index] + tv_kind_1.visible() + } + 2 -> { + tv_kind_2.text = kinds[index] + tv_kind_2.visible() } } - } - 4 -> { - if (searchBook.latestChapterTitle.isNullOrEmpty()) { - tv_lasted.gone() - } else { - tv_lasted.text = context.getString( - R.string.lasted_show, - searchBook.latestChapterTitle - ) - tv_lasted.visible() + } else { + when (index) { + 0 -> tv_kind.gone() + 1 -> tv_kind_1.gone() + 2 -> tv_kind_2.gone() } } - 5 -> tv_introduce.text = - context.getString(R.string.intro_show, searchBook.intro) - else -> { - } } } }