From c7ac4e74c4d35f37e5ba22eaece42023a4522fa8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 30 Dec 2020 10:06:19 +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 --- .../app/base/adapter/RecyclerAdapter.kt | 33 ++++++++++++- .../book/source/manage/BookSourceActivity.kt | 2 +- .../book/source/manage/BookSourceAdapter.kt | 46 +++++++++++++++++++ .../ui/rss/source/manage/RssSourceActivity.kt | 2 +- .../ui/rss/source/manage/RssSourceAdapter.kt | 32 +++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt index 1f374acdf..c2882c048 100644 --- a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt @@ -98,8 +98,39 @@ abstract class RecyclerAdapter(protected val context: Co } @Synchronized - fun setItems(items: List?, diffResult: DiffUtil.DiffResult) { + fun setItems(items: List?, itemCallback: DiffUtil.ItemCallback) { kotlin.runCatching { + val callback = object : DiffUtil.Callback() { + override fun getOldListSize(): Int { + return itemCount + } + + override fun getNewListSize(): Int { + return items?.size ?: 0 + } + + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = getItem(oldItemPosition) ?: return false + val newItem = items?.getOrNull(newItemPosition) ?: return false + return itemCallback.areItemsTheSame(oldItem, newItem) + } + + override fun areContentsTheSame( + oldItemPosition: Int, + newItemPosition: Int + ): Boolean { + val oldItem = getItem(oldItemPosition) ?: return false + val newItem = items?.getOrNull(newItemPosition) ?: return false + return itemCallback.areContentsTheSame(oldItem, newItem) + } + + override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { + val oldItem = getItem(oldItemPosition) ?: return null + val newItem = items?.getOrNull(newItemPosition) ?: return null + return itemCallback.getChangePayload(oldItem, newItem) + } + } + val diffResult = DiffUtil.calculateDiff(callback) if (this.items.isNotEmpty()) { this.items.clear() } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 86e99b361..38c138482 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -204,7 +204,7 @@ class BookSourceActivity : VMBaseActivity data.sortedBy { it.lastUpdateTime } else -> data.reversed() } - adapter.setItems(sourceList) + adapter.setItems(sourceList, adapter.diffItemCallback) }) } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 55f179023..1545a3108 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -8,6 +8,7 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.PopupMenu import androidx.core.os.bundleOf +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder @@ -27,6 +28,51 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : private val selected = linkedSetOf() + val diffItemCallback: DiffUtil.ItemCallback + get() = object : DiffUtil.ItemCallback() { + + override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean { + return oldItem.bookSourceUrl == newItem.bookSourceUrl + } + + override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean { + if (oldItem.bookSourceName != newItem.bookSourceName) + return false + if (oldItem.bookSourceGroup != newItem.bookSourceGroup) + return false + if (oldItem.enabled != newItem.enabled) + return false + if (oldItem.enabledExplore != newItem.enabledExplore + || oldItem.exploreUrl != newItem.exploreUrl + ) { + return false + } + return true + } + + override fun getChangePayload(oldItem: BookSource, newItem: BookSource): Any? { + val payload = Bundle() + if (oldItem.bookSourceName != newItem.bookSourceName) { + payload.putString("name", newItem.bookSourceName) + } + if (oldItem.bookSourceGroup != newItem.bookSourceGroup) { + payload.putString("group", newItem.bookSourceGroup) + } + if (oldItem.enabled != newItem.enabled) { + payload.putBoolean("enabled", newItem.enabled) + } + if (oldItem.enabledExplore != newItem.enabledExplore + || oldItem.exploreUrl != newItem.exploreUrl + ) { + payload.putBoolean("showExplore", true) + } + if (payload.isEmpty) { + return null + } + return payload + } + } + override fun getViewBinding(parent: ViewGroup): ItemBookSourceBinding { return ItemBookSourceBinding.inflate(inflater, parent, false) } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 0edd9304a..e6db8c73e 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -206,7 +206,7 @@ class RssSourceActivity : VMBaseActivity() + val diffItemCallback: DiffUtil.ItemCallback + get() = object : DiffUtil.ItemCallback() { + + override fun areItemsTheSame(oldItem: RssSource, newItem: RssSource): Boolean { + return oldItem.sourceUrl == newItem.sourceUrl + } + + override fun areContentsTheSame(oldItem: RssSource, newItem: RssSource): Boolean { + return oldItem.sourceName == newItem.sourceName + && oldItem.sourceGroup == newItem.sourceGroup + && oldItem.enabled == newItem.enabled + } + + override fun getChangePayload(oldItem: RssSource, newItem: RssSource): Any? { + val payload = Bundle() + if (oldItem.sourceName != newItem.sourceName) { + payload.putString("name", newItem.sourceName) + } + if (oldItem.sourceGroup != newItem.sourceGroup) { + payload.putString("group", newItem.sourceGroup) + } + if (oldItem.enabled != newItem.enabled) { + payload.putBoolean("enabled", newItem.enabled) + } + if (payload.isEmpty) { + return null + } + return payload + } + } + override fun getViewBinding(parent: ViewGroup): ItemRssSourceBinding { return ItemRssSourceBinding.inflate(inflater, parent, false) }