How to handle swipe to delete and undo correctly?

196 views Asked by At

I am implementing undo to delete when swipe item from recycler view.

All the logic working correctly, the problem is that I am deleting the item only if the user didn't press undo on the snackbar.

Which mean there is a short time between the the time that the user swiped the item from the list, to the time that the item was actual deleted from the database.

In that time, I still having the red background and the icon from the onChildDraw method, it's gone only when the item was actual deleted.

I'm adding a screenshot that clarify the problem:

enter image description here

How can I fix this problem?

 private fun showUndoSnackbar(
        position: Int,
        itemToDelete: Article
    ) {
        val snackbar =
            Snackbar.make(binding.root, "Item was removed from the list", Snackbar.LENGTH_LONG)
        snackbar.setAction("UNDO") {
            uiCommunicationListener.onResponseReceived("Undo action", UIComponentType.Toast)
            recyclerViewAdapter.notifyItemChanged(position)
        }
        snackbar.addCallback(object : Snackbar.Callback() {
            override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
                if (event == DISMISS_EVENT_TIMEOUT) {
                    viewModel.delete(itemToDelete)
                }
            }
        })
        snackbar.show()
    }
0

There are 0 answers