pull/783/head
gedoor 4 years ago
parent 14fb5dc1ac
commit c7ac4e74c4
  1. 33
      app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  3. 46
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  4. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  5. 32
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -98,8 +98,39 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
@Synchronized @Synchronized
fun setItems(items: List<ITEM>?, diffResult: DiffUtil.DiffResult) { fun setItems(items: List<ITEM>?, itemCallback: DiffUtil.ItemCallback<ITEM>) {
kotlin.runCatching { 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()) { if (this.items.isNotEmpty()) {
this.items.clear() this.items.clear()
} }

@ -204,7 +204,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
Sort.Update -> data.sortedBy { it.lastUpdateTime } Sort.Update -> data.sortedBy { it.lastUpdateTime }
else -> data.reversed() else -> data.reversed()
} }
adapter.setItems(sourceList) adapter.setItems(sourceList, adapter.diffItemCallback)
}) })
} }

@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
@ -27,6 +28,51 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
private val selected = linkedSetOf<BookSource>() private val selected = linkedSetOf<BookSource>()
val diffItemCallback: DiffUtil.ItemCallback<BookSource>
get() = object : DiffUtil.ItemCallback<BookSource>() {
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 { override fun getViewBinding(parent: ViewGroup): ItemBookSourceBinding {
return ItemBookSourceBinding.inflate(inflater, parent, false) return ItemBookSourceBinding.inflate(inflater, parent, false)
} }

@ -206,7 +206,7 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
App.db.rssSourceDao.liveSearch("%$key%") App.db.rssSourceDao.liveSearch("%$key%")
} }
sourceLiveData?.observe(this, { sourceLiveData?.observe(this, {
adapter.setItems(it) adapter.setItems(it, adapter.diffItemCallback)
}) })
} }

@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
@ -23,6 +24,37 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
private val selected = linkedSetOf<RssSource>() private val selected = linkedSetOf<RssSource>()
val diffItemCallback: DiffUtil.ItemCallback<RssSource>
get() = object : DiffUtil.ItemCallback<RssSource>() {
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 { override fun getViewBinding(parent: ViewGroup): ItemRssSourceBinding {
return ItemRssSourceBinding.inflate(inflater, parent, false) return ItemRssSourceBinding.inflate(inflater, parent, false)
} }

Loading…
Cancel
Save