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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SelectableLazyColumn for key separation and update Gradle wrapper #216

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 63 additions & 44 deletions foundation/api/foundation.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface SelectableColumnOnKeyEvent {
fun onSelectFirstItem(allKeys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val firstSelectable = allKeys.withIndex().firstOrNull { it.value is Selectable }
if (firstSelectable != null) {
state.selectedKeys = listOf(firstSelectable.value.key)
state.selectedKeys = listOf(firstSelectable.value.value)
state.lastActiveItemIndex = firstSelectable.index
}
}
Expand All @@ -30,7 +30,7 @@ interface SelectableColumnOnKeyEvent {
while (iterator.hasPrevious()) {
val previous = iterator.previous()
if (previous is Selectable) {
add(previous.key)
add(previous.value)
state.lastActiveItemIndex = (iterator.previousIndex() + 1).coerceAtMost(keys.size)
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ interface SelectableColumnOnKeyEvent {
val list = mutableListOf<Any>(state.selectedKeys)
keys.subList(it, keys.lastIndex).forEachIndexed { index, selectableLazyListKey ->
if (selectableLazyListKey is Selectable) {
list.add(selectableLazyListKey.key)
list.add(selectableLazyListKey.value)
}
state.lastActiveItemIndex = index
}
Expand All @@ -85,7 +85,7 @@ interface SelectableColumnOnKeyEvent {
.reversed()
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = listOf(selectableKey.key)
state.selectedKeys = listOf(selectableKey.value)
state.lastActiveItemIndex = index
}
}
Expand All @@ -104,7 +104,7 @@ interface SelectableColumnOnKeyEvent {
.reversed()
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = state.selectedKeys + selectableKey.key
state.selectedKeys = state.selectedKeys + selectableKey.value
state.lastActiveItemIndex = index
}
}
Expand All @@ -121,7 +121,7 @@ interface SelectableColumnOnKeyEvent {
.dropWhile { it.index <= lastActiveIndex }
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = listOf(selectableKey.key)
state.selectedKeys = listOf(selectableKey.value)
state.lastActiveItemIndex = index
}
}
Expand All @@ -139,7 +139,7 @@ interface SelectableColumnOnKeyEvent {
.dropWhile { it.index <= lastActiveIndex }
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = state.selectedKeys + selectableKey.key
state.selectedKeys = state.selectedKeys + selectableKey.value
state.lastActiveItemIndex = index
}
}
Expand All @@ -151,7 +151,7 @@ interface SelectableColumnOnKeyEvent {
fun onScrollPageUpAndSelectItem(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = max((state.lastActiveItemIndex ?: 0) - visibleSize, 0)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = listOf(keys[targetIndex].value)
state.lastActiveItemIndex = targetIndex
}

Expand All @@ -166,7 +166,7 @@ interface SelectableColumnOnKeyEvent {
.withIndex()
.filter { it.value is Selectable }
.let {
state.selectedKeys + it.map { selectableKey -> selectableKey.value.key }
state.selectedKeys + it.map { selectableKey -> selectableKey.value.value }
}
state.selectedKeys = newSelectionList
state.lastActiveItemIndex = targetIndex
Expand All @@ -178,7 +178,7 @@ interface SelectableColumnOnKeyEvent {
fun onScrollPageDownAndSelectItem(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = min((state.lastActiveItemIndex ?: 0) + visibleSize, keys.lastIndex)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = listOf(keys[targetIndex].value)
state.lastActiveItemIndex = targetIndex
}

Expand All @@ -192,7 +192,7 @@ interface SelectableColumnOnKeyEvent {
keys.subList(state.lastActiveItemIndex ?: 0, targetIndex)
.filterIsInstance<Selectable>()
.let {
state.selectedKeys + it.map { selectableKey -> selectableKey.key }
state.selectedKeys + it.map { selectableKey -> selectableKey.value }
}
state.selectedKeys = newSelectionList
state.lastActiveItemIndex = targetIndex
Expand All @@ -210,7 +210,7 @@ interface SelectableColumnOnKeyEvent {
* Select All
*/
fun onSelectAll(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.key }
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.value }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ fun SelectableLazyColumn(
val container = SelectableLazyListScopeContainer()
.apply(content)

val keys = remember(container) {
container.getKeys()
val selectionKeys = remember(container) {
container.getSelectionKeys()
}
var isFocused by remember { mutableStateOf(false) }

fun evaluateIndexes(): List<Int> {
val keyToIndexMap = keys.withIndex().associateBy({ it.value.key }, { it.index })
val keyToIndexMap = selectionKeys.withIndex().associateBy({ it.value.value }, { it.index })
return state.selectedKeys
.mapNotNull { selected -> keyToIndexMap[selected] }
.sorted()
Expand All @@ -78,7 +78,7 @@ fun SelectableLazyColumn(
.onPreviewKeyEvent { event ->
if (state.lastActiveItemIndex != null) {
val actionHandled = keyActions
.handleOnKeyEvent(event, keys, state, selectionMode)
.handleOnKeyEvent(event, selectionKeys, state, selectionMode)
.invoke(event)
if (actionHandled) {
scope.launch {
Expand All @@ -99,21 +99,21 @@ fun SelectableLazyColumn(
) {
container.getEntries().forEach { entry ->
when (entry) {
is Entry.Item -> item(entry.key, entry.contentType) {
is Entry.Item -> item(entry.uiKey, entry.contentType) {
val itemScope = SelectableLazyItemScope(
isSelected = entry.key in state.selectedKeys,
isSelected = entry.selectionKey in state.selectedKeys,
isActive = isFocused,
)
if (keys.any { it.key == entry.key && it is SelectableLazyListKey.Selectable }) {
if (selectionKeys.any { it.value == entry.selectionKey && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
requester = focusRequester,
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key,
allKeys = selectionKeys,
itemKey = entry.selectionKey,
),
) {
entry.content.invoke(itemScope)
Expand All @@ -125,20 +125,21 @@ fun SelectableLazyColumn(

is Entry.Items -> items(
count = entry.count,
key = { entry.key(entry.startIndex + it) },
key = { entry.uiKey(entry.startIndex + it) },
contentType = { entry.contentType(it) },
) { index ->
val itemScope = SelectableLazyItemScope(entry.key(index) in state.selectedKeys, isFocused)
if (keys.any { it.key == entry.key(index) && it is SelectableLazyListKey.Selectable }) {
val itemScope =
SelectableLazyItemScope(entry.selectionKey(index) in state.selectedKeys, isFocused)
if (selectionKeys.any { it.value == entry.selectionKey(index) && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
requester = focusRequester,
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key(index),
allKeys = selectionKeys,
itemKey = entry.selectionKey(index),
),
) {
entry.itemContent.invoke(itemScope, index)
Expand All @@ -148,23 +149,23 @@ fun SelectableLazyColumn(
}
}

is Entry.StickyHeader -> stickyHeader(entry.key, entry.contentType) {
val itemScope = SelectableLazyItemScope(entry.key in state.selectedKeys, isFocused)
if (keys.any { it.key == entry.key && it is SelectableLazyListKey.Selectable }) {
is Entry.StickyHeader -> stickyHeader(entry.uiKey, entry.contentType) {
val itemScope = SelectableLazyItemScope(entry.selectionKey in state.selectedKeys, isFocused)
if (selectionKeys.any { it.value == entry.selectionKey && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key,
allKeys = selectionKeys,
itemKey = entry.selectionKey,
),
) {
entry.content.invoke(itemScope)
}
} else {
SelectableLazyItemScope(entry.key in state.selectedKeys, isFocused).apply {
SelectableLazyItemScope(entry.selectionKey in state.selectedKeys, isFocused).apply {
entry.content.invoke(itemScope)
}
}
Expand All @@ -182,7 +183,7 @@ private fun Modifier.selectable(
selectableState: SelectableLazyListState,
allKeys: List<SelectableLazyListKey>,
itemKey: Any,
) = this.pointerInput(allKeys, itemKey) {
) = pointerInput(allKeys, itemKey) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent()
Expand Down
Loading