mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-20 07:37:15 +00:00
wrap with trycatch StateFlowBasedRecyclerViewAdapter listener
This commit is contained in:
parent
6d999be590
commit
1c86f3f4bf
@ -15,31 +15,35 @@ abstract class StateFlowBasedRecyclerViewAdapter<T>(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
dataState.onEach {
|
dataState.onEach {
|
||||||
val diffForRemoves = Diff(data, it)
|
try {
|
||||||
val removedIndexes = diffForRemoves.removed.map { it.index }
|
val diffForRemoves = Diff(data, it)
|
||||||
val leftRemove = removedIndexes.toMutableList()
|
val removedIndexes = diffForRemoves.removed.map { it.index }
|
||||||
data = data.filterIndexed { i, _ ->
|
val leftRemove = removedIndexes.toMutableList()
|
||||||
if (i in leftRemove) {
|
data = data.filterIndexed { i, _ ->
|
||||||
leftRemove.remove(i)
|
if (i in leftRemove) {
|
||||||
true
|
leftRemove.remove(i)
|
||||||
} else {
|
true
|
||||||
false
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
withContext(Dispatchers.Main) {
|
||||||
withContext(Dispatchers.Main) {
|
removedIndexes.sortedDescending().forEach {
|
||||||
removedIndexes.sortedDescending().forEach {
|
notifyItemRemoved(it)
|
||||||
notifyItemRemoved(it)
|
}
|
||||||
}
|
}
|
||||||
}
|
val diffAddsAndReplaces = Diff(data, it)
|
||||||
val diffAddsAndReplaces = Diff(data, it)
|
data = it
|
||||||
data = it
|
withContext(Dispatchers.Main) {
|
||||||
withContext(Dispatchers.Main) {
|
diffAddsAndReplaces.replaced.forEach { (from, to) ->
|
||||||
diffAddsAndReplaces.replaced.forEach { (from, to) ->
|
notifyItemMoved(from.index, to.index)
|
||||||
notifyItemMoved(from.index, to.index)
|
}
|
||||||
}
|
diffAddsAndReplaces.added.forEach {
|
||||||
diffAddsAndReplaces.added.forEach {
|
notifyItemInserted(it.index)
|
||||||
notifyItemInserted(it.index)
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
// currently do nothing
|
||||||
}
|
}
|
||||||
}.launchIn(listeningScope)
|
}.launchIn(listeningScope)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user