Skip to content

Commit

Permalink
Rename 'uiKey' to 'itemKey'
Browse files Browse the repository at this point in the history
  • Loading branch information
lamba92 committed Oct 31, 2023
1 parent 3064c60 commit c7a2821
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ interface SelectableLazyListScope {

/**
* Represents an item in a selectable lazy list.
* By default `[selectionKey] = [uiKey]`, but you can assign [uiKey] if you need to
* By default [selectionKey] = [itemKey], but you can assign [itemKey] if you need to
* recompose the item while keeping the selection unchanged.
*
* @param selectionKey The unique identifier for the item used for selection.
* @param uiKey The unique identifier for the item used for triggering recompositions.
* @param itemKey The unique identifier for the item used for triggering recompositions.
* @param contentType The type of content displayed in the item.
* @param selectable Determines if the item is selectable. Default is
* `true`.
* @param content The content of the item as a composable function.
*/
fun item(
selectionKey: Any,
uiKey: Any = selectionKey,
itemKey: Any = selectionKey,
contentType: Any? = null,
selectable: Boolean = true,
content: @Composable SelectableLazyItemScope.() -> Unit,
)

/**
* Represents a list of items based on the provided parameters.
* By default `[selectionKey] = [uiKey]`, but you can assign [uiKey] if you need to
* By default [selectionKey] = [itemKey], but you can assign [itemKey] if you need to
* recompose the item while keeping the selection unchanged.
*
* @param count The number of items in the list.
* @param selectionKey A function that generates a unique key for each item based on
* its index. It is used for selection.
* @param uiKey A function that generates a unique key for each item based on
* @param itemKey A function that generates a unique key for each item based on
* its index. It is used for triggering recompositions.
* @param contentType A function that returns the content type of an item
* based on its index. Defaults to `null`.
Expand All @@ -49,27 +49,27 @@ interface SelectableLazyListScope {
fun items(
count: Int,
selectionKey: (index: Int) -> Any,
uiKey: (index: Int) -> Any = selectionKey,
itemKey: (index: Int) -> Any = selectionKey,
contentType: (index: Int) -> Any? = { null },
selectable: (index: Int) -> Boolean = { true },
itemContent: @Composable SelectableLazyItemScope.(index: Int) -> Unit,
)

/**
* A method that enables sticky header behavior in a list or grid view.
* By default `[selectionKey] = [uiKey]`, but you can assign [uiKey] if you need to
* By default [selectionKey] = [itemKey], but you can assign [itemKey] if you need to
* recompose the item while keeping the selection unchanged.
*
* @param selectionKey The unique identifier for the sticky header used for selection.
* @param uiKey The unique identifier for the sticky header used for triggering recompositions.
* @param itemKey The unique identifier for the sticky header used for triggering recompositions.
* @param contentType The type of content in the sticky header.
* @param selectable Specifies whether the sticky header is selectable.
* @param content The content to be displayed in the sticky header,
* provided as a composable function
*/
fun stickyHeader(
selectionKey: Any,
uiKey: Any = selectionKey,
itemKey: Any = selectionKey,
contentType: Any? = null,
selectable: Boolean = false,
content: @Composable SelectableLazyItemScope.() -> Unit,
Expand Down Expand Up @@ -115,20 +115,20 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {

override fun item(
selectionKey: Any,
uiKey: Any,
itemKey: Any,
contentType: Any?,
selectable: Boolean,
content: @Composable (SelectableLazyItemScope.() -> Unit),
) {
selectionKeys.add(if (selectable) Selectable(selectionKey) else NotSelectable(selectionKey))
entries.add(Entry.Item(selectionKey, uiKey, contentType, content, entriesCount))
entries.add(Entry.Item(selectionKey, itemKey, contentType, content, entriesCount))
entriesCount++
}

override fun items(
count: Int,
selectionKey: (index: Int) -> Any,
uiKey: (index: Int) -> Any,
itemKey: (index: Int) -> Any,
contentType: (index: Int) -> Any?,
selectable: (index: Int) -> Boolean,
itemContent: @Composable (SelectableLazyItemScope.(index: Int) -> Unit),
Expand All @@ -141,36 +141,36 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
}
}
selectionKeys.addAll(selectableKeys)
entries.add(Entry.Items(count, selectionKey, uiKey, contentType, itemContent, entriesCount))
entries.add(Entry.Items(count, selectionKey, itemKey, contentType, itemContent, entriesCount))
entriesCount += count
}

@ExperimentalFoundationApi
override fun stickyHeader(
selectionKey: Any,
uiKey: Any,
itemKey: Any,
contentType: Any?,
selectable: Boolean,
content: @Composable (SelectableLazyItemScope.() -> Unit),
) {
selectionKeys.add(if (selectable) Selectable(selectionKey) else NotSelectable(selectionKey))
entries.add(Entry.StickyHeader(selectionKey, uiKey, contentType, content, entriesCount))
entries.add(Entry.StickyHeader(selectionKey, itemKey, contentType, content, entriesCount))
entriesCount++
}
}

fun <T : Any> SelectableLazyListScope.items(
items: List<T>,
selectionKey: (item: T) -> Any = { it },
uiKey: (item: T) -> Any = { it },
itemKey: (item: T) -> Any = selectionKey,
contentType: (item: T) -> Any? = { it },
selectable: (item: T) -> Boolean = { true },
itemContent: @Composable SelectableLazyItemScope.(item: T) -> Unit,
) {
items(
count = items.size,
selectionKey = { selectionKey(items[it]) },
uiKey = { uiKey(items[it]) },
itemKey = { itemKey(items[it]) },
contentType = { contentType(items[it]) },
selectable = { selectable(items[it]) },
itemContent = { itemContent(items[it]) },
Expand All @@ -180,15 +180,15 @@ fun <T : Any> SelectableLazyListScope.items(
fun <T : Any> SelectableLazyListScope.itemsIndexed(
items: List<T>,
selectionKey: (index: Int, item: T) -> Any = { _, item -> item },
uiKey: (index: Int, item: T) -> Any = { _, item -> item },
itemKey: (index: Int, item: T) -> Any = selectionKey,
contentType: (index: Int, item: T) -> Any? = { _, item -> item },
selectable: (index: Int, item: T) -> Boolean = { _, _ -> true },
itemContent: @Composable SelectableLazyItemScope.(index: Int, item: T) -> Unit,
) {
items(
count = items.size,
selectionKey = { selectionKey(it, items[it]) },
uiKey = { uiKey(it, items[it]) },
itemKey = { itemKey(it, items[it]) },
contentType = { contentType(it, items[it]) },
selectable = { selectable(it, items[it]) },
itemContent = { itemContent(it, items[it]) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fun <T> BasicLazyTree(
itemsIndexed(
items = flattenedTree,
selectionKey = { _, item -> item.selectionId },
uiKey = { _, item -> item.uiId },
itemKey = { _, item -> item.uiId },
contentType = { _, item -> item.data },
) { index, element ->
val elementState = TreeElementState.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fun SelectableLazyColumnSample() {
items(
count = listOfItems.size,
selectionKey = { index -> index },
uiKey = { index -> listOfItems[index] },
itemKey = { index -> listOfItems[index] },
) { index ->
Text(
text = listOfItems[index],
Expand Down

0 comments on commit c7a2821

Please sign in to comment.