jordond / drag-select-compose

⚡ A Compose multiplatform library for adding Google Photos style drag-to-select multi-selection to a LazyGrid
https://demo.dragselectcompose.com
MIT License
176 stars 0 forks source link

dragSelectState is selecting incorrect items if data source isn't static list #113

Open ishail-dev opened 5 months ago

ishail-dev commented 5 months ago

Hello 👋🏽, DragSelectState behaves very weird when item list isn't static.

For an example, I'm fetching data from on-device database(Room) using the following:

val deletedPhotos by localPhotoViewModel.repository.allCameraTrashedPhotos.collectAsState(emptyList())
val dragSelectState = rememberDragSelectState<PhotoEntity>(compareSelector = { it.uid!! })

Now drag-select works fine when data loads for the first time. But when some more rows are added/deleted from database, then sometimes drag-select selects incorrect item, or sometimes can't select/de-select items.

I'm not sure whether I'm doing something wrong, or whether there is an issue in library. Here is how I'm rendering images:

            items(photos.size) { index ->
                val isSelected by remember {
                    derivedStateOf { dragSelectState.isSelected(photos[index]) }
                }

                PhotoItemContent(
                    item = photos[index],
                    inSelectionMode = dragSelectState.inSelectionMode,
                    isSelected = isSelected,
                    modifier = Modifier
                        // Add semantics for accessibility
                        .semantics {
                            if (!dragSelectState.inSelectionMode) {
                                onLongClick("Select") {
                                    dragSelectState.addSelected(photos[index])
                                    true
                                }
                            }
                        }
                        // If we are in selection mode allow the item to be toggleable
                        .then(
                            if (dragSelectState.inSelectionMode) {
                                Modifier.toggleable(
                                    value = isSelected,
                                    interactionSource = remember { MutableInteractionSource() },
                                    indication = null, // do not show a ripple
                                    onValueChange = { toggled ->
                                        if (toggled) dragSelectState.addSelected(photos[index])
                                        else dragSelectState.removeSelected(photos[index])
                                    }
                                )
                            } else {
                                Modifier.clickable {
                                    onClickPhotoItem(navController, photos[index])
                                }
                            },
                        )
                )
            }

Please let me know if you can help here or need more info.

I have also attached a recording of the user experience here:

https://github.com/jordond/drag-select-compose/assets/58887170/853307cb-3f47-4e19-81b5-da5df10c0062

jordond commented 5 months ago

It's a bug for sure, I'll look into it 👍. Thanks for reporting.

ishail-dev commented 5 months ago

Hi @jordond, Do you have any estimation on when it will be fixed? Just trying to understand the timeline so that I can prioritise things at my end.

jordond commented 5 months ago

I have no timeline or estimate on my end. I can only work on this in my spare time, but I am open to contributions if you wanted to take a crack at it.

ishail-dev commented 4 months ago

I'll attempt to fix it. However I'm still a hobby Android programmer, and learning it since last ~6 months. So can't be sure about it. I'll raise a PR if I'm able to fix it.