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

Fix excessive recompositions in SelectableLazyColumn #723

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
Expand Down Expand Up @@ -59,19 +57,20 @@ public fun SelectableLazyColumn(
content: SelectableLazyListScope.() -> Unit,
) {
val scope = rememberCoroutineScope()
val container = SelectableLazyListScopeContainer().apply(content)
val container = remember(content) { SelectableLazyListScopeContainer().apply(content) }

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

val latestOnSelectedIndexesChanged = rememberUpdatedState(onSelectedIndexesChange)
LaunchedEffect(state, container) {
snapshotFlow { state.selectedKeys }
.collect { selectedKeys ->
val indices = selectedKeys.mapNotNull { key -> container.getKeyIndex(key) }
latestOnSelectedIndexesChanged.value.invoke(indices)
}
var lastSelectedKeys by remember { mutableStateOf(state.selectedKeys) }
LaunchedEffect(state.selectedKeys, onSelectedIndexesChange, container) {
if (lastSelectedKeys == state.selectedKeys) return@LaunchedEffect

val indices = state.selectedKeys.mapNotNull { key -> container.getKeyIndex(key) }
lastSelectedKeys = state.selectedKeys
onSelectedIndexesChange(indices)
}

val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
LazyColumn(
Expand Down
Loading