Skip to content

Commit

Permalink
PPJG-2914 Remove last callback (#24)
Browse files Browse the repository at this point in the history
* Remove last callback if we set new items.
We have to remove the previous callback if we set new items, since the callback could break the logic with the predictive animations(namely supportsPredictiveItemAnimations) which are calculated via a few onLayout callbacks

* Set callback as null after we already removed the callback and remove redundant volatile annotation
  • Loading branch information
Manwtein committed Apr 4, 2024
1 parent e94f968 commit 083577a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion delegates/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=delegates
VERSION_NAME=1.1.2
VERSION_NAME=1.1.3
POM_NAME=delegates
POM_PACKAGING=jar
GROUP=com.revolut.recyclerkit
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ open class DiffAdapter(
private set
override val items = mutableListOf<ListItem>()

private var lastDispatchDiffCallback: Runnable? = null

override fun attachRecyclerView(recyclerView: RecyclerView) {
this.recyclerView = WeakReference(recyclerView)
}
Expand All @@ -133,6 +135,7 @@ open class DiffAdapter(
val firstVisiblePosition = recyclerView.layoutManager.findFirstCompletelyVisibleItemPosition(autoScrollToTop)

val dispatchDiff: () -> Unit = {
lastDispatchDiffCallback = null
items.clear()
items.addAll(newList)

Expand All @@ -143,8 +146,13 @@ open class DiffAdapter(
}
}

lastDispatchDiffCallback?.let {
recyclerView.removeCallbacks(it)
}
if (recyclerView.isComputingLayout) {
recyclerView.post { dispatchDiff() }
val newDispatchDiffCallback = Runnable { dispatchDiff() }
lastDispatchDiffCallback = newDispatchDiffCallback
recyclerView.post(newDispatchDiffCallback)
} else {
dispatchDiff()
}
Expand Down

0 comments on commit 083577a

Please sign in to comment.