Skip to content

Commit

Permalink
Performance improvements for scroll (#45)
Browse files Browse the repository at this point in the history
* Add Stable annotation to inheritors of ReorderableLazyCollectionState

* Mark ReorderableCollectionItemScope as Stable and cache impl per item

---------

Co-authored-by: Dzmitry Strekha <dstrekha@alfa-bank.by>
  • Loading branch information
strekha and Dzmitry Strekha committed Aug 22, 2024
1 parent cdc9af2 commit a3ab624
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
}
}

@Stable
interface ReorderableCollectionItemScope {
/**
* Make the UI element the draggable handle for the reorderable item.
Expand Down Expand Up @@ -830,11 +831,14 @@ internal fun ReorderableCollectionItem(
itemPosition = it.positionInRoot()
}
) {
ReorderableCollectionItemScopeImpl(
reorderableLazyCollectionState = state,
key = key,
itemPositionProvider = { itemPosition },
).content(dragging)
val itemScope = remember(state, key) {
ReorderableCollectionItemScopeImpl(
reorderableLazyCollectionState = state,
key = key,
itemPositionProvider = { itemPosition },
)
}
itemScope.content(dragging)
}

LaunchedEffect(state.reorderableKeys, enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.lazy.grid.LazyGridItemScope
import androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -153,6 +154,7 @@ private fun LazyGridState.toLazyCollectionState() =
this@toLazyCollectionState.scrollToItem(scrollToIndex, firstVisibleItemScrollOffset)
}

@Stable
class ReorderableLazyGridState internal constructor(
state: LazyGridState,
scope: CoroutineScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListLayoutInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -242,6 +243,7 @@ private fun LazyListState.toLazyCollectionState() =
this@toLazyCollectionState.scrollToItem(scrollToIndex, firstVisibleItemScrollOffset)
}

@Stable
class ReorderableLazyListState internal constructor(
state: LazyListState,
scope: CoroutineScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridItemScope
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridLayoutInfo
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -152,6 +153,7 @@ private fun LazyStaggeredGridState.toLazyCollectionState() =
this@toLazyCollectionState.scrollToItem(scrollToIndex, firstVisibleItemScrollOffset)
}

@Stable
class ReorderableLazyStaggeredGridState internal constructor(
state: LazyStaggeredGridState,
scope: CoroutineScope,
Expand Down

0 comments on commit a3ab624

Please sign in to comment.