Skip to content

Commit

Permalink
Merge pull request #77 from vsukharew/dev
Browse files Browse the repository at this point in the history
dev -> master 1.0.11
  • Loading branch information
vsukharew committed Apr 25, 2021
2 parents 02c427b + 3eca95e commit bcda5c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ext {
library : [
publishGroupId: 'io.github.vsukharew',
publishArtifactId: 'anytypeadapter',
versionName: "1.0.10",
versionName: "1.0.11",
versionCode: 1,
],
sample : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.consumeEach
import vsukharev.anytypeadapter.holder.AnyTypeViewHolder
import vsukharev.anytypeadapter.item.AdapterItem

Expand All @@ -14,9 +17,13 @@ import vsukharev.anytypeadapter.item.AdapterItem
*/
open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBinding>>(),
CoroutineScope by MainScope() {
protected var anyTypeCollection: AnyTypeCollection = AnyTypeCollection.EMPTY
var diffStrategy: DiffStrategy = DiffStrategy.DiscardLatest
private val diffActor =
actor<() -> Unit?>(capacity = Channel.BUFFERED) { consumeEach { it.invoke() } }
private var diffJob: Job? = null

protected var anyTypeCollection: AnyTypeCollection = AnyTypeCollection.EMPTY

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
Expand Down Expand Up @@ -54,21 +61,31 @@ open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBind
collection: AnyTypeCollection,
onUpdatesDispatch: ((AnyTypeCollection) -> Unit)? = null
) {
val adapter = this
diffJob?.cancel()
diffJob = launch {
val diffResult = withContext(Dispatchers.Default) {
DiffUtil.calculateDiff(
DiffUtilCallback(
anyTypeCollection.items,
collection.items
)
val diffBlock = {
val adapter = this
val diffResult = DiffUtil.calculateDiff(
DiffUtilCallback(
anyTypeCollection.items,
collection.items
)
}
)
anyTypeCollection = collection
diffResult.dispatchUpdatesTo(adapter)
onUpdatesDispatch?.invoke(collection)
}
diffJob = when (diffStrategy) {
DiffStrategy.Queue -> {
launch {
diffActor.send(diffBlock)
}
}
DiffStrategy.DiscardLatest -> {
diffJob?.cancel()
launch {
diffActor.send(diffBlock)
}
}
}
}

/**
Expand Down Expand Up @@ -106,4 +123,9 @@ open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBind
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
oldList[oldItemPosition].areContentsTheSame(newList[newItemPosition])
}

sealed class DiffStrategy {
object Queue : DiffStrategy()
object DiscardLatest : DiffStrategy()
}
}

0 comments on commit bcda5c8

Please sign in to comment.