Skip to content

Commit

Permalink
Fix crash while scrolling during call to updateBricks (#256)
Browse files Browse the repository at this point in the history
* Fix crash while scrolling during call to updateBricks
  • Loading branch information
mindwalkr authored Mar 23, 2021
1 parent 5622965 commit 131ebd3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.wayfair.brickkit

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Before
import org.junit.runner.RunWith
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import org.junit.Test

@RunWith(AndroidJUnit4::class)
class SafeAdapterListUpdateCallbackTest {

private val brickRecyclerAdapter: BrickRecyclerAdapter = mock()
private lateinit var adapter: SafeAdapterListUpdateCallback

@Before
fun setup() {
adapter = SafeAdapterListUpdateCallback(brickRecyclerAdapter)
}

@Test
fun testOnInserted() {
val position = 3
val count = 1
adapter.onInserted(position, count)
verify(brickRecyclerAdapter).safeNotifyItemRangeInserted(position, count)
}

@Test
fun testOnRemoved() {
val position = 4
val count = 1
adapter.onRemoved(position, count)
verify(brickRecyclerAdapter).safeNotifyItemRangeRemoved(position, count)
}

@Test
fun testOnMoved() {
val position = 5
val count = 9
adapter.onMoved(position, count)
verify(brickRecyclerAdapter).safeNotifyItemMoved(position, count)
}

@Test
fun testOnChanged() {
val position = 2
val count = 6
val any: Any = mock()
adapter.onChanged(position, count, any)
verify(brickRecyclerAdapter).safeNotifyItemRangeChanged(position, count, any)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ open class BrickDataManager : Serializable {
currentItems.addAll(bricks)

dataHasChanged()
brickRecyclerAdapter?.let { DiffUtil.calculateDiff(diffUtilCallback).dispatchUpdatesTo(it) }
brickRecyclerAdapter?.let {
DiffUtil.calculateDiff(diffUtilCallback).dispatchUpdatesTo(
SafeAdapterListUpdateCallback(it)
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wayfair.brickkit

import androidx.recyclerview.widget.ListUpdateCallback

/**
* A safe implementation of [androidx.recyclerview.widget.ListUpdateCallback],
* which allows updates to the adapter during scrolling by mapping the calls
* to safe versions.
*/
internal class SafeAdapterListUpdateCallback(
private val adapter: BrickRecyclerAdapter
) : ListUpdateCallback {

override fun onInserted(position: Int, count: Int) {
adapter.safeNotifyItemRangeInserted(position, count)
}

override fun onRemoved(position: Int, count: Int) {
adapter.safeNotifyItemRangeRemoved(position, count)
}

override fun onMoved(fromPosition: Int, toPosition: Int) {
adapter.safeNotifyItemMoved(fromPosition, toPosition)
}

override fun onChanged(position: Int, count: Int, payload: Any?) {
adapter.safeNotifyItemRangeChanged(position, count, payload)
}
}

0 comments on commit 131ebd3

Please sign in to comment.