Let's say that there is a simple scenario:
user
table for storing user detailsuser_images
to store in aimage_path
field the paths of images in the phone's storage.
There is a list of users shown in a RecyclerView
and the user can Swipe to delete a row. The adapter is a ListAdapter
and the data comes from Room as being LiveData
Workflow I was thinking is like this:
- remove the swiped item from the adapter and update the recyclerview
- show a Snack with Undo option
- on Snack if user presses Undo, re-add the item in the
ListAdapter
and update the recyclerview. If the user does not press Undo and the Snack gets dismissed after timeout, delete the rows fromuser
,user_images
and also delete all the images from the storage related touser_images
inimage_path
...
override fun onSwiped(...){
val deletedItem = listAdapter.currentList.get(index)
//REMOVE FROM THE ADAPTER HERE
val snack = Snackbar.make(...)
snack.setAction(...){
//RE-ADD TO ADAPTER HERE
}
snack.addCallback(...){
override fun onDismissed(...){
//do the actual deletes
}
}
}
...
The issue is using the ListAdapter. As you may know it uses DiffCallBack to manage the view updates and calling adapter.submitList(list)
does not trigger any updates as it receives the same list. (ListAdapter not updating item in reyclerview)
So in order to be able to remove item, I would need to:
- get the adapter's
currentList
asMutable
updatebleList - remove the item from the updatebleList
- re-submit the list to the adapter
adapter.submitList(updatebleList.toList())
- in case of Undo, re-add the item to the updatebleListand submit it again as a new list
adapter.submitList(updatebleList.toList())
As you see, there are a lot of list re-creation in order to properly submit. Is there a better/simpler way to achieve this?