Merge pull request #246 from TouchInstinct/submittable_paging_adapter

SubmittablePagingAdapter
This commit is contained in:
Kirill Nayduik 2022-02-28 20:22:46 +03:00 committed by GitHub
commit e0b83d80b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,8 @@ dependencies {
implementation "androidx.core:core-ktx"
implementation "androidx.paging:paging-runtime"
constraints {
implementation("androidx.recyclerview:recyclerview") {
version {
@ -18,5 +20,10 @@ dependencies {
require '1.0.0'
}
}
implementation("androidx.paging:paging-runtime") {
version {
require '3.0.0-alpha06'
}
}
}
}

View File

@ -0,0 +1,27 @@
package ru.touchin.roboswag.recyclerview_adapters.adapters
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
abstract class SubmittablePagingAdapter<T : Any, VH : RecyclerView.ViewHolder>(private val diffCallback: DiffUtil.ItemCallback<T>):
PagingDataAdapter<T, VH>(diffCallback) {
/**
* [items] list of updated elements
* [transform] callback that keeps logic of transformation item based on newItem.
* Should return updated element
*/
fun submitList(items: List<T>, transform: T.(newItem: T, payload: Any?) -> T) {
items.forEach { newItem ->
snapshot().forEachIndexed { index, oldItem ->
if (oldItem != null && diffCallback.areItemsTheSame(oldItem, newItem) && !diffCallback.areContentsTheSame(oldItem, newItem)) {
val payload = diffCallback.getChangePayload(oldItem, newItem)
oldItem.transform(newItem, payload)
notifyItemChanged(index, payload)
}
}
}
}
}