Skip to content

Commit

Permalink
Support disabling gestures.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuhDoy committed Jun 1, 2024
1 parent 097587c commit e118c08
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ is the most important reason I created this library: to modularize selection for
- Clean and Stable Implementation: Does not rely on any internal APIs or experimental APIs of
LazyList and LazyGrid, so you don't have to worry about API changes or incompatibilities due to
updates of Compose.
- Highly Extensible: Provides multi-level interfaces and supports various complex use cases.

## Integration

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
val androidGradlePluginVersion = "8.4.0"
val androidGradlePluginVersion = "8.4.1"
id("com.android.application") version androidGradlePluginVersion apply false
id("com.android.library") version androidGradlePluginVersion apply false
kotlin("android") version "1.9.23" apply false
Expand Down
17 changes: 13 additions & 4 deletions library/src/main/java/me/gm/selection/grid/Gestures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ import me.gm.selection.detectTapGestures
abstract class LazyGridDetailsLookup<V> : DetailsLookup<LazyGridItemInfo, V>

private class FullyInteractiveLazyGridDetailsLookup<V>(
private val enabled: () -> Boolean,
private val map: () -> KeyIndexItemMap<Any, V>,
private val key: (itemInfo: LazyGridItemInfo) -> Any = when (map()) {
is KeyItemMap -> { itemInfo -> itemInfo.key }
is IndexItemMap -> { itemInfo -> itemInfo.index }
}
) : LazyGridDetailsLookup<V>() {

override fun getItem(itemInfo: LazyGridItemInfo): V? = map().getItem(key(itemInfo))
override fun getItem(itemInfo: LazyGridItemInfo): V? =
if (enabled()) {
map().getItem(key(itemInfo))
} else {
null
}
}

private fun touchInfo(
Expand Down Expand Up @@ -116,10 +122,11 @@ fun <V> Modifier.longPressToToggleGesture(
gridState: LazyGridState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = longPressToToggleGesture(
gridState,
selectionState,
FullyInteractiveLazyGridDetailsLookup(map)
FullyInteractiveLazyGridDetailsLookup(enabled, map)
)

fun <V> Modifier.tapInActionModeToToggleGesture(
Expand Down Expand Up @@ -147,10 +154,11 @@ fun <V> Modifier.tapInActionModeToToggleGesture(
gridState: LazyGridState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = tapInActionModeToToggleGesture(
gridState,
selectionState,
FullyInteractiveLazyGridDetailsLookup(map)
FullyInteractiveLazyGridDetailsLookup(enabled, map)
)

private class RangeSupport<V>(
Expand Down Expand Up @@ -255,8 +263,9 @@ fun <V> Modifier.dragAfterLongPressToSelectGesture(
gridState: LazyGridState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = dragAfterLongPressToSelectGesture(
gridState,
selectionState,
FullyInteractiveLazyGridDetailsLookup(map)
FullyInteractiveLazyGridDetailsLookup(enabled, map)
)
17 changes: 13 additions & 4 deletions library/src/main/java/me/gm/selection/list/Gestures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ import me.gm.selection.detectTapGestures
abstract class LazyListDetailsLookup<V> : DetailsLookup<LazyListItemInfo, V>

private class FullyInteractiveLazyListDetailsLookup<V>(
private val enabled: () -> Boolean,
private val map: () -> KeyIndexItemMap<Any, V>,
private val key: (itemInfo: LazyListItemInfo) -> Any = when (map()) {
is KeyItemMap -> { itemInfo -> itemInfo.key }
is IndexItemMap -> { itemInfo -> itemInfo.index }
}
) : LazyListDetailsLookup<V>() {

override fun getItem(itemInfo: LazyListItemInfo): V? = map().getItem(key(itemInfo))
override fun getItem(itemInfo: LazyListItemInfo): V? =
if (enabled()) {
map().getItem(key(itemInfo))
} else {
null
}
}

private fun touchInfo(
Expand Down Expand Up @@ -119,10 +125,11 @@ fun <V> Modifier.longPressToToggleGesture(
listState: LazyListState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = longPressToToggleGesture(
listState,
selectionState,
FullyInteractiveLazyListDetailsLookup(map)
FullyInteractiveLazyListDetailsLookup(enabled, map)
)

fun <V> Modifier.tapInActionModeToToggleGesture(
Expand Down Expand Up @@ -150,10 +157,11 @@ fun <V> Modifier.tapInActionModeToToggleGesture(
listState: LazyListState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = tapInActionModeToToggleGesture(
listState,
selectionState,
FullyInteractiveLazyListDetailsLookup(map)
FullyInteractiveLazyListDetailsLookup(enabled, map)
)

private class RangeSupport<V>(
Expand Down Expand Up @@ -258,8 +266,9 @@ fun <V> Modifier.dragAfterLongPressToSelectGesture(
listState: LazyListState,
selectionState: SelectionState<Any, V>,
map: () -> KeyIndexItemMap<Any, V>,
enabled: () -> Boolean = { true },
): Modifier = dragAfterLongPressToSelectGesture(
listState,
selectionState,
FullyInteractiveLazyListDetailsLookup(map)
FullyInteractiveLazyListDetailsLookup(enabled, map)
)

0 comments on commit e118c08

Please sign in to comment.