Merge pull request #246 from TouchInstinct/submittable_paging_adapter
SubmittablePagingAdapter
This commit is contained in:
commit
e0b83d80b8
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue