Skip to content

Commit

Permalink
Allow user to create its GridLayoutInfo through GridLayoutInfoFactory
Browse files Browse the repository at this point in the history
Summary:
This is a feature request from [this post](https://fb.workplace.com/groups/1430200360634661/permalink/2386733414981346/) that allow user to provide its own `GridLayoutManager` when creating a GridLayoutInfo.

In `LinearLayoutInfo` we already have `LinearLayoutInfoFactory` for user to use its own LinearLayoutManager, this diff is providing similar APIs to `GridLayoutInfo`.

Reviewed By: astreet

Differential Revision: D16560816

fbshipit-source-id: 6cfa71c5d4099f92aa26d3c4521bc6e795adbd31
  • Loading branch information
Jing-Wei Wu authored and facebook-github-bot committed Jul 30, 2019
1 parent e61ac4f commit 4568d58
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.litho.sections.widget;

import android.content.Context;
import com.facebook.litho.widget.GridLayoutInfo;

/** A Factory used to create {@link GridLayoutInfo}s in {@link GridRecyclerConfiguration}. */
public interface GridLayoutInfoFactory {
/**
* @return a new {@link GridLayoutInfo} that will be used to compute the layouts of the children
* of the {@link GridRecyclerConfiguration}.
*/
GridLayoutInfo createGridLayoutInfo(
Context c,
int spanCount,
int orientation,
boolean reverseLayout,
boolean allowGridMeasuresOverride);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.facebook.litho.widget.SnapUtil.SNAP_NONE;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
Expand All @@ -39,6 +40,7 @@ public class GridRecyclerConfiguration<T extends SectionTree.Target & Binder<Rec
private final int mNumColumns;
private final boolean mReverseLayout;
private final RecyclerBinderConfiguration mRecyclerBinderConfiguration;
private final GridLayoutInfoFactory mGridLayoutInfoFactory;
private final boolean mAllowMeasureOverride;

public static GridRecyclerConfiguration.Builder create() {
Expand Down Expand Up @@ -81,14 +83,31 @@ public GridRecyclerConfiguration(
this(orientation, numColumns, reverseLayout, recyclerBinderConfiguration, false);
}

/** Use {@link #create()} instead. */
@Deprecated
public GridRecyclerConfiguration(
int orientation,
int numColumns,
boolean reverseLayout,
RecyclerBinderConfiguration recyclerBinderConfiguration,
boolean allowMeasureOverride) {
this(
orientation,
numColumns,
reverseLayout,
recyclerBinderConfiguration,
allowMeasureOverride,
Builder.GRID_LAYOUT_INFO_FACTORY);
}

/** Use {@link #create()} instead. */
@Deprecated
public GridRecyclerConfiguration(
int orientation,
int numColumns,
boolean reverseLayout,
RecyclerBinderConfiguration recyclerBinderConfiguration,
boolean allowMeasureOverride,
@Nullable GridLayoutInfoFactory gridLayoutInfoFactory) {
mOrientation = orientation;
mNumColumns = numColumns;
mReverseLayout = reverseLayout;
Expand All @@ -97,6 +116,10 @@ public GridRecyclerConfiguration(
? Builder.RECYCLER_BINDER_CONFIGURATION
: recyclerBinderConfiguration;
mAllowMeasureOverride = allowMeasureOverride;
mGridLayoutInfoFactory =
gridLayoutInfoFactory == null
? GridRecyclerConfiguration.Builder.GRID_LAYOUT_INFO_FACTORY
: gridLayoutInfoFactory;
}

@Override
Expand All @@ -121,7 +144,7 @@ public int getOrientation() {

@Override
public LayoutInfo getLayoutInfo(ComponentContext c) {
return new GridLayoutInfo(
return mGridLayoutInfoFactory.createGridLayoutInfo(
c.getAndroidContext(), mNumColumns, mOrientation, mReverseLayout, mAllowMeasureOverride);
}

Expand All @@ -130,16 +153,32 @@ public RecyclerBinderConfiguration getRecyclerBinderConfiguration() {
return mRecyclerBinderConfiguration;
}

private static class DefaultGridLayoutInfoFactory implements GridLayoutInfoFactory {
@Override
public GridLayoutInfo createGridLayoutInfo(
Context c,
int spanCount,
int orientation,
boolean reverseLayout,
boolean allowGridMeasuresOverride) {
return new GridLayoutInfo(
c, spanCount, orientation, reverseLayout, allowGridMeasuresOverride);
}
}

public static class Builder implements RecyclerConfiguration.Builder {
static final RecyclerBinderConfiguration RECYCLER_BINDER_CONFIGURATION =
RecyclerBinderConfiguration.create().build();
static final GridLayoutInfoFactory GRID_LAYOUT_INFO_FACTORY =
new DefaultGridLayoutInfoFactory();

private int mOrientation = LinearLayoutManager.VERTICAL;
private int mNumColumns = 2;
private boolean mReverseLayout = false;
private boolean mAllowMeasureOverride = false;
private RecyclerBinderConfiguration mRecyclerBinderConfiguration =
RECYCLER_BINDER_CONFIGURATION;
private GridLayoutInfoFactory mGridLayoutInfoFactory = GRID_LAYOUT_INFO_FACTORY;

Builder() {}

Expand All @@ -149,6 +188,7 @@ public static class Builder implements RecyclerConfiguration.Builder {
this.mReverseLayout = gridRecyclerConfiguration.mReverseLayout;
this.mAllowMeasureOverride = gridRecyclerConfiguration.mAllowMeasureOverride;
this.mRecyclerBinderConfiguration = gridRecyclerConfiguration.mRecyclerBinderConfiguration;
this.mGridLayoutInfoFactory = gridRecyclerConfiguration.mGridLayoutInfoFactory;
}

@Override
Expand Down Expand Up @@ -185,6 +225,14 @@ public Builder allowMeasureOverride(boolean allowMeasureOverride) {
return this;
}

/**
* Provide a customized {@link GridLayoutInfo} through {@link GridLayoutInfoFactory} interface.
*/
public Builder gridLayoutInfoFactory(GridLayoutInfoFactory gridLayoutInfoFactory) {
mGridLayoutInfoFactory = gridLayoutInfoFactory;
return this;
}

/**
* Builds a {@link GridRecyclerConfiguration} using the parameters specified in this builder.
*/
Expand All @@ -195,7 +243,8 @@ public GridRecyclerConfiguration build() {
mNumColumns,
mReverseLayout,
mRecyclerBinderConfiguration,
mAllowMeasureOverride);
mAllowMeasureOverride,
mGridLayoutInfoFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ public class GridLayoutInfo implements LayoutInfo {

private final GridLayoutManager mGridLayoutManager;
private final GridSpanSizeLookup mGridSpanSizeLookup;
private final boolean mAllowGridMeasureOverride;

private RenderInfoCollection mRenderInfoCollection;

public GridLayoutInfo(GridLayoutManager gridLayoutManager) {
mGridLayoutManager = gridLayoutManager;
mGridSpanSizeLookup = new GridSpanSizeLookup();
mGridLayoutManager.setSpanSizeLookup(mGridSpanSizeLookup);
}

public GridLayoutInfo(
Context context,
int spanCount,
int orientation,
boolean reverseLayout,
boolean allowGridMeasuresOverride) {
mAllowGridMeasureOverride = allowGridMeasuresOverride;
mGridLayoutManager =
mAllowGridMeasureOverride
this(
allowGridMeasuresOverride
? new GridLayoutManager(context, spanCount, orientation, reverseLayout)
: new LithoGridLayoutManager(context, spanCount, orientation, reverseLayout);
mGridSpanSizeLookup = new GridSpanSizeLookup();
mGridLayoutManager.setSpanSizeLookup(mGridSpanSizeLookup);
: new LithoGridLayoutManager(context, spanCount, orientation, reverseLayout));
}

public GridLayoutInfo(Context context, int spanCount, int orientation, boolean reverseLayout) {
Expand Down

0 comments on commit 4568d58

Please sign in to comment.