-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash while scrolling during call to updateBricks (#256)
* Fix crash while scrolling during call to updateBricks
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...Kit/bricks/src/androidTest/java/com/wayfair/brickkit/SafeAdapterListUpdateCallbackTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
BrickKit/bricks/src/main/java/com/wayfair/brickkit/SafeAdapterListUpdateCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |