Skip to content

Commit

Permalink
Fix selection failed to work when data source isn't a static list (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
R9DCHDC authored Jul 16, 2024
1 parent 28bf5c5 commit ed53cfc
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
Expand Down Expand Up @@ -71,11 +73,13 @@ public fun <Item> Modifier.gridDragSelect(
if (!enableHaptics) null
else hapticFeedback ?: GridDragSelectDefaults.hapticsFeedback

val currentItems by rememberUpdatedState(items)

pointerInput(Unit) {
detectDragGesturesAfterLongPress(
onDragStart = { offset ->
state.gridState.itemIndexAtPosition(offset)?.let { startIndex ->
val item = items.getOrNull(startIndex)
val item = currentItems.getOrNull(startIndex)
if (item != null && state.selected.contains(item).not()) {
haptics?.performHapticFeedback(HapticFeedbackType.LongPress)
state.startDrag(item, startIndex)
Expand All @@ -92,7 +96,7 @@ public fun <Item> Modifier.gridDragSelect(
?: return@whenDragging

val newSelection =
items.getSelectedItems(itemPosition, dragState, state::isSelected)
currentItems.getSelectedItems(itemPosition, dragState, state::isSelected)

updateDrag(current = itemPosition)
updateSelected(newSelection)
Expand Down

0 comments on commit ed53cfc

Please sign in to comment.