philipplackner / SwipeToDeleteCompose

23 stars 5 forks source link

Undo behaviour #1

Open PrimozPernat opened 6 months ago

PrimozPernat commented 6 months ago

Thank you for the great video and example of how to SwipeToDelete. I was playing around with this code and have a question: how would undo work in this scenario? The problem is that only the delete background is visible once we attach the item back to the list.

I played around with the code and first thought that the isRemoved mutable state gets persistent for the item, but that is not the case. The value is set to false. So it seems that the dismissContent = { content(item) } that removes the compose visibility is persisting that value.

I could only make the item visible again if I made a copy of the data with a different ID, but to me, it doesn't seem like a good approach to changing ids in this part of the code.

What would your suggestion be in tackling this scenario?

Snipit of the simple code of adding the item back:

scope.launch {
    when (snackbarHostState.showSnackbar("Snackbar", actionLabel = "Undo")) {
        SnackbarResult.ActionPerformed -> {
            recipeList.add(position, deletedRecipe.copy(id = UUID.randomUUID().toString()))
            listState.animateScrollToItem(position)
        }

        SnackbarResult.Dismissed -> {
            /* Handle snackbar dismissed */
        }
    }
ctechdev commented 5 months ago

I would need a confirmation dialog before deleting the element but I can't implement it, have you done something similar?