Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ishail-dev opened this issue Jan 6, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@ishail-dev
Copy link

ishail-dev commented Jan 6, 2024

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:

WhatsApp.Video.2024-01-06.at.23.47.37.mp4
@jordond
Copy link
Owner

jordond commented Jan 7, 2024

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

@jordond jordond added the bug Something isn't working label Jan 7, 2024
@jordond jordond self-assigned this Jan 7, 2024
@ishail-dev
Copy link
Author

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
Copy link
Owner

jordond commented Jan 23, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants