From 8448097590603f5866c3bb775dda17fc79b6ee0a Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Tue, 22 Oct 2024 10:33:11 -0700 Subject: [PATCH] Expose layout factory for Lazy Collections Summary: We didn't expose layout factory for lazy collections which was reported by oncall request(https://fb.workplace.com/groups/litho.support/permalink/3906883812966291/), this diff is to expose it to all collections. Reviewed By: zielinskimz Differential Revision: D64706268 fbshipit-source-id: 2d0d415c43922c67df74c7edbf8d9e80347df96a --- .../sections/widget/GridRecyclerConfiguration.java | 8 +++----- .../sections/widget/ListRecyclerConfiguration.java | 5 +++-- .../widget/StaggeredGridRecyclerConfiguration.java | 9 +++++---- .../litho/widget/collection/CollectionLayout.kt | 11 ++++++++--- .../com/facebook/litho/widget/collection/LazyGrid.kt | 3 +++ .../com/facebook/litho/widget/collection/LazyList.kt | 6 +++++- .../litho/widget/collection/LazyStaggeredGrid.kt | 4 +--- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/GridRecyclerConfiguration.java b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/GridRecyclerConfiguration.java index ef445a6b1f..404a9a18c0 100644 --- a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/GridRecyclerConfiguration.java +++ b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/GridRecyclerConfiguration.java @@ -68,9 +68,7 @@ private GridRecyclerConfiguration( mRecyclerBinderConfiguration = recyclerBinderConfiguration; mAllowMeasureOverride = allowMeasureOverride; mGridLayoutInfoFactory = - gridLayoutInfoFactory == null - ? GridRecyclerConfiguration.Builder.GRID_LAYOUT_INFO_FACTORY - : gridLayoutInfoFactory; + gridLayoutInfoFactory == null ? Builder.GRID_LAYOUT_INFO_FACTORY : gridLayoutInfoFactory; mSnapMode = snapMode; mSnapHelper = snapHelper; } @@ -142,7 +140,7 @@ public static final class Builder implements RecyclerConfiguration.Builder { private boolean mAllowMeasureOverride = false; private RecyclerBinderConfiguration mRecyclerBinderConfiguration = RECYCLER_BINDER_CONFIGURATION; - private GridLayoutInfoFactory mGridLayoutInfoFactory = GRID_LAYOUT_INFO_FACTORY; + private @Nullable GridLayoutInfoFactory mGridLayoutInfoFactory; private int mDeltaJumpThreshold = Integer.MAX_VALUE; private int mSnapToStartFlingOffset = SnapUtil.SNAP_TO_START_DEFAULT_FLING_OFFSET; private @SnapMode int mSnapMode = SNAP_NONE; @@ -227,7 +225,7 @@ public Builder snapHelper(SnapHelper snapHelper) { /** * Provide a customized {@link GridLayoutInfo} through {@link GridLayoutInfoFactory} interface. */ - public Builder gridLayoutInfoFactory(GridLayoutInfoFactory gridLayoutInfoFactory) { + public Builder gridLayoutInfoFactory(@Nullable GridLayoutInfoFactory gridLayoutInfoFactory) { mGridLayoutInfoFactory = gridLayoutInfoFactory; return this; } diff --git a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/ListRecyclerConfiguration.java b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/ListRecyclerConfiguration.java index d7132d0661..884728fec9 100644 --- a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/ListRecyclerConfiguration.java +++ b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/ListRecyclerConfiguration.java @@ -143,7 +143,7 @@ public static final class Builder implements RecyclerConfiguration.Builder { @SnapMode private int mSnapMode = SNAP_NONE; private RecyclerBinderConfiguration mRecyclerBinderConfiguration = RECYCLER_BINDER_CONFIGURATION; - private LinearLayoutInfoFactory mLinearLayoutInfoFactory = LINEAR_LAYOUT_INFO_FACTORY; + private @Nullable LinearLayoutInfoFactory mLinearLayoutInfoFactory; private int mDeltaJumpThreshold = Integer.MAX_VALUE; private int mSnapToStartFlingOffset = SnapUtil.SNAP_TO_START_DEFAULT_FLING_OFFSET; @@ -194,7 +194,8 @@ public Builder recyclerBinderConfiguration( return this; } - public Builder linearLayoutInfoFactory(LinearLayoutInfoFactory linearLayoutInfoFactory) { + public Builder linearLayoutInfoFactory( + @Nullable LinearLayoutInfoFactory linearLayoutInfoFactory) { mLinearLayoutInfoFactory = linearLayoutInfoFactory; return this; } diff --git a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/StaggeredGridRecyclerConfiguration.java b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/StaggeredGridRecyclerConfiguration.java index 1cee196613..a0bc28a0ca 100644 --- a/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/StaggeredGridRecyclerConfiguration.java +++ b/litho-sections-widget/src/main/java/com/facebook/litho/sections/widget/StaggeredGridRecyclerConfiguration.java @@ -52,14 +52,15 @@ private StaggeredGridRecyclerConfiguration( boolean stackFromEnd, int gapStrategy, RecyclerBinderConfiguration recyclerBinderConfiguration, - StaggeredGridLayoutInfoFactory layoutInfoFactory) { + @Nullable StaggeredGridLayoutInfoFactory layoutInfoFactory) { mNumSpans = numSpans; mOrientation = orientation; mReverseLayout = reverseLayout; mStackFromEnd = stackFromEnd; mGapStrategy = gapStrategy; mRecyclerBinderConfiguration = recyclerBinderConfiguration; - mLayoutInfoFactory = layoutInfoFactory; + mLayoutInfoFactory = + layoutInfoFactory == null ? Builder.STAGGERED_GRID_LAYOUT_INFO_FACTORY : layoutInfoFactory; } @Override @@ -125,7 +126,7 @@ public static final class Builder implements RecyclerConfiguration.Builder { private int mGapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE; private RecyclerBinderConfiguration mRecyclerBinderConfiguration = RECYCLER_BINDER_CONFIGURATION; - private StaggeredGridLayoutInfoFactory mLayoutInfoFactory = STAGGERED_GRID_LAYOUT_INFO_FACTORY; + private @Nullable StaggeredGridLayoutInfoFactory mLayoutInfoFactory; Builder() {} @@ -185,7 +186,7 @@ public Builder recyclerBinderConfiguration( * StaggeredGridLayoutInfoFactory} interface. */ public Builder staggeredGridLayoutInfoFactory( - StaggeredGridLayoutInfoFactory staggeredGridLayoutInfoFactory) { + @Nullable StaggeredGridLayoutInfoFactory staggeredGridLayoutInfoFactory) { mLayoutInfoFactory = staggeredGridLayoutInfoFactory; return this; } 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 f9cb66db25..58e1ad3fad 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 @@ -21,7 +21,9 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager import com.facebook.litho.ComponentContext import com.facebook.litho.config.PreAllocationHandler +import com.facebook.litho.sections.widget.GridLayoutInfoFactory import com.facebook.litho.sections.widget.GridRecyclerConfiguration +import com.facebook.litho.sections.widget.LinearLayoutInfoFactory import com.facebook.litho.sections.widget.ListRecyclerConfiguration import com.facebook.litho.sections.widget.RecyclerBinderConfiguration import com.facebook.litho.sections.widget.RecyclerConfiguration @@ -131,7 +133,8 @@ internal object CollectionLayouts { mainAxisWrapContent: Boolean = false, preAllocationHandler: PreAllocationHandler?, isCircular: Boolean, - enableStableIds: Boolean + enableStableIds: Boolean, + linearLayoutInfoFactory: LinearLayoutInfoFactory?, ): CollectionLayout = object : CollectionLayout( @@ -149,6 +152,7 @@ internal object CollectionLayouts { override fun createRecyclerConfigurationBuilder(): RecyclerConfiguration.Builder = ListRecyclerConfiguration.create() .snapMode(snapMode) + .linearLayoutInfoFactory(linearLayoutInfoFactory) .snapToStartOffset(snapToStartOffset) } @@ -175,6 +179,7 @@ internal object CollectionLayouts { preAllocationHandler: PreAllocationHandler?, enableStableIds: Boolean, mainAxisWrapContent: Boolean = false, + gridLayoutInfoFactory: GridLayoutInfoFactory?, ): CollectionLayout = object : CollectionLayout( @@ -192,6 +197,7 @@ internal object CollectionLayouts { GridRecyclerConfiguration.create() .snapMode(snapMode) .snapToStartOffset(snapToStartOffset) + .gridLayoutInfoFactory(gridLayoutInfoFactory) .numColumns(columns) } @@ -216,8 +222,7 @@ internal object CollectionLayouts { isIncrementalMountEnabled: Boolean = true, spans: Int = 2, gapStrategy: Int = StaggeredGridLayoutManager.GAP_HANDLING_NONE, - staggeredGridlayoutInfoFactory: StaggeredGridLayoutInfoFactory = - StaggeredGridRecyclerConfiguration.Builder.STAGGERED_GRID_LAYOUT_INFO_FACTORY, + staggeredGridlayoutInfoFactory: StaggeredGridLayoutInfoFactory?, preAllocationHandler: PreAllocationHandler?, enableStableIds: Boolean ): CollectionLayout = diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyGrid.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyGrid.kt index 74e54a5c45..6246a32a97 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyGrid.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyGrid.kt @@ -23,6 +23,7 @@ import com.facebook.litho.LithoStartupLogger import com.facebook.litho.ResourcesScope import com.facebook.litho.Style import com.facebook.litho.config.PreAllocationHandler +import com.facebook.litho.sections.widget.GridLayoutInfoFactory import com.facebook.litho.widget.LithoRecyclerView import com.facebook.litho.widget.SnapUtil import com.facebook.rendercore.Dimen @@ -78,6 +79,7 @@ inline fun ResourcesScope.LazyGrid( shouldExcludeFromIncrementalMount: Boolean = false, enableStableIds: Boolean = context.lithoConfiguration.componentsConfig.useStableIdsInRecyclerBinder, + gridLayoutInfoFactory: GridLayoutInfoFactory? = null, crossinline init: LazyGridScope.() -> Unit ): Component { val lazyGridScope = LazyGridScope(context).apply { init() } @@ -95,6 +97,7 @@ inline fun ResourcesScope.LazyGrid( columns = columns, enableStableIds = enableStableIds, mainAxisWrapContent = mainAxisWrapContent, + gridLayoutInfoFactory = gridLayoutInfoFactory, ), itemAnimator, itemDecoration, 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 df997592d4..d3fe8ff76f 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 @@ -23,6 +23,7 @@ import com.facebook.litho.LithoStartupLogger import com.facebook.litho.ResourcesScope import com.facebook.litho.Style import com.facebook.litho.config.PreAllocationHandler +import com.facebook.litho.sections.widget.LinearLayoutInfoFactory import com.facebook.litho.widget.LithoRecyclerView import com.facebook.litho.widget.SnapUtil import com.facebook.rendercore.Dimen @@ -79,6 +80,7 @@ inline fun ResourcesScope.LazyList( isCircular: Boolean = false, enableStableIds: Boolean = context.lithoConfiguration.componentsConfig.useStableIdsInRecyclerBinder, + linearLayoutInfoFactory: LinearLayoutInfoFactory? = null, crossinline init: LazyListScope.() -> Unit ): Component { val lazyListScope = LazyListScope(context).apply { init() } @@ -97,7 +99,9 @@ inline fun ResourcesScope.LazyList( crossAxisWrapMode = crossAxisWrapMode, mainAxisWrapContent = mainAxisWrapContent, isCircular = isCircular, - enableStableIds = enableStableIds), + enableStableIds = enableStableIds, + linearLayoutInfoFactory = linearLayoutInfoFactory, + ), itemAnimator, itemDecoration, clipToPadding, diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyStaggeredGrid.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyStaggeredGrid.kt index 6a8b9b4184..f39611b973 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyStaggeredGrid.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyStaggeredGrid.kt @@ -25,7 +25,6 @@ import com.facebook.litho.ResourcesScope import com.facebook.litho.Style import com.facebook.litho.config.PreAllocationHandler import com.facebook.litho.sections.widget.StaggeredGridLayoutInfoFactory -import com.facebook.litho.sections.widget.StaggeredGridRecyclerConfiguration import com.facebook.litho.widget.LithoRecyclerView import com.facebook.litho.widget.SnapUtil import com.facebook.rendercore.Dimen @@ -78,8 +77,7 @@ inline fun ResourcesScope.LazyStaggeredGrid( preAllocationHandler: PreAllocationHandler? = context.lithoConfiguration.componentsConfig.preAllocationHandler, shouldExcludeFromIncrementalMount: Boolean = false, - staggeredGridlayoutInfoFactory: StaggeredGridLayoutInfoFactory = - StaggeredGridRecyclerConfiguration.Builder.STAGGERED_GRID_LAYOUT_INFO_FACTORY, + staggeredGridlayoutInfoFactory: StaggeredGridLayoutInfoFactory? = null, enableStableIds: Boolean = context.lithoConfiguration.componentsConfig.useStableIdsInRecyclerBinder, crossinline init: LazyGridScope.() -> Unit