From 8285acacbdb513799b89cb6aabd63a05d2654ed1 Mon Sep 17 00:00:00 2001 From: Fabio Carballo Date: Tue, 18 Jun 2024 04:42:10 -0700 Subject: [PATCH] Add temporary support to enable stable ids on LazyLists Summary: We need to add the ability to enable stable ids for a `LazyList` surface in GenAI. Ideally, we would set this via `ComponentsConfiguration` (but it needs a bigger refactor fist). Therefore, I'm adding this ability to the `LazyList` signature temporarily while I work on the refactor. Reviewed By: kingsleyadio Differential Revision: D58684561 fbshipit-source-id: b67fcf90b5cb3487021086a2c7a7fc2492836097 --- .../widget/collection/CollectionLayout.kt | 19 +++++++++++++------ .../litho/widget/collection/LazyList.kt | 5 ++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt index 958e2a2b03..530864b615 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt @@ -20,6 +20,7 @@ import androidx.annotation.Px import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager import com.facebook.litho.ComponentContext +import com.facebook.litho.config.ComponentsConfiguration import com.facebook.litho.config.PreAllocationHandler import com.facebook.litho.sections.widget.GridRecyclerConfiguration import com.facebook.litho.sections.widget.ListRecyclerConfiguration @@ -51,7 +52,8 @@ abstract class CollectionLayout( val canMeasureRecycler: Boolean = false, mainAxisWrapContent: Boolean = false, preAllocationHandler: PreAllocationHandler?, - isCircular: Boolean + isCircular: Boolean, + enableStableIds: Boolean ) { internal abstract fun createRecyclerConfigurationBuilder(): RecyclerConfiguration.Builder @@ -71,7 +73,8 @@ abstract class CollectionLayout( incrementalMountEnabled = isIncrementalMountEnabled), rangeRatio = rangeRatio ?: RecyclerBinderConfig.DEFAULT_RANGE_RATIO, wrapContent = mainAxisWrapContent, - isCircular = isCircular)) + isCircular = isCircular, + enableStableIds = enableStableIds)) .useBackgroundChangeSets(useBackgroundChangeSets) .build()) .build() @@ -132,7 +135,8 @@ internal object CollectionLayouts { crossAxisWrapMode: CrossAxisWrapMode = CrossAxisWrapMode.NoWrap, mainAxisWrapContent: Boolean = false, preAllocationHandler: PreAllocationHandler?, - isCircular: Boolean + isCircular: Boolean, + enableStableIds: Boolean ): CollectionLayout = object : CollectionLayout( @@ -146,7 +150,8 @@ internal object CollectionLayouts { canMeasureRecycler = crossAxisWrapMode.canMeasureRecycler, mainAxisWrapContent = mainAxisWrapContent, preAllocationHandler = preAllocationHandler, - isCircular = isCircular) { + isCircular = isCircular, + enableStableIds = enableStableIds) { override fun createRecyclerConfigurationBuilder(): RecyclerConfiguration.Builder = ListRecyclerConfiguration.create() .snapMode(snapMode) @@ -186,7 +191,8 @@ internal object CollectionLayouts { useBackgroundChangeSets = useBackgroundChangeSets, isReconciliationEnabled = isReconciliationEnabled, preAllocationHandler = preAllocationHandler, - isCircular = false) { + isCircular = false, + enableStableIds = ComponentsConfiguration.defaultRecyclerBinderUseStableId) { override fun createRecyclerConfigurationBuilder(): RecyclerConfiguration.Builder = GridRecyclerConfiguration.create() .snapMode(snapMode) @@ -229,7 +235,8 @@ internal object CollectionLayouts { isReconciliationEnabled = isReconciliationEnabled, isIncrementalMountEnabled = isIncrementalMountEnabled, preAllocationHandler = preAllocationHandler, - isCircular = false) { + isCircular = false, + enableStableIds = ComponentsConfiguration.defaultRecyclerBinderUseStableId) { override fun createRecyclerConfigurationBuilder(): RecyclerConfiguration.Builder = StaggeredGridRecyclerConfiguration.create().numSpans(spans).gapStrategy(gapStrategy) } diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt index 3ffb6dbea3..50233c080d 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt @@ -22,6 +22,7 @@ import com.facebook.litho.Handle import com.facebook.litho.LithoStartupLogger import com.facebook.litho.ResourcesScope import com.facebook.litho.Style +import com.facebook.litho.config.ComponentsConfiguration import com.facebook.litho.config.PreAllocationHandler import com.facebook.litho.widget.LithoRecyclerView import com.facebook.litho.widget.SnapUtil @@ -75,6 +76,7 @@ inline fun ResourcesScope.LazyList( fadingEdgeLength: Dimen? = null, shouldExcludeFromIncrementalMount: Boolean = false, isCircular: Boolean = false, + enableStableIds: Boolean = ComponentsConfiguration.defaultRecyclerBinderUseStableId, init: LazyListScope.() -> Unit ): Component { val lazyListScope = LazyListScope(context).apply { init() } @@ -93,7 +95,8 @@ inline fun ResourcesScope.LazyList( preAllocationHandler = preAllocationHandler, crossAxisWrapMode = crossAxisWrapMode, mainAxisWrapContent = mainAxisWrapContent, - isCircular = isCircular), + isCircular = isCircular, + enableStableIds = enableStableIds), itemAnimator, itemDecoration, clipToPadding,