Skip to content

Commit

Permalink
[Carousel] Changed CarouselConfiguration to have an empty constructor…
Browse files Browse the repository at this point in the history
… and pass Carousel in through onFirstChildMeasuredWithMargins

PiperOrigin-RevId: 508389752
  • Loading branch information
hunterstich authored and dsn5ft committed Feb 9, 2023
1 parent 697d93a commit 8014267
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
forceCompactSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) ->
multiBrowseStartCarouselLayoutManager.setCarouselConfiguration(
new MultiBrowseCarouselConfiguration(
multiBrowseStartCarouselLayoutManager, isChecked)));
new MultiBrowseCarouselConfiguration(isChecked)));

drawDividers.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
*/
abstract class CarouselConfiguration {

private final Carousel carousel;

CarouselConfiguration(@NonNull Carousel carousel) {
this.carousel = carousel;
}

/**
* Calculates a keyline arrangement and returns a constructed {@link KeylineState}.
*
Expand All @@ -56,14 +50,10 @@ abstract class CarouselConfiguration {
*
* @param child The first measured view from the carousel, use this view to determine the max size
* that all items in the carousel will be given.
* @param carousel The carousel to create a {@link KeylineState} for
* @return A {@link KeylineState} to be used by the layout manager to offset and mask children
* along the scrolling axis.
*/
protected abstract KeylineState onFirstChildMeasuredWithMargins(@NonNull View child);

/** Gets the {@link Carousel} associated with this configuration. */
@NonNull
protected final Carousel getCarousel() {
return carousel;
}
protected abstract KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child);
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static final class ChildCalculations {
}

public CarouselLayoutManager() {
setCarouselConfiguration(new MultiBrowseCarouselConfiguration(this));
setCarouselConfiguration(new MultiBrowseCarouselConfiguration());
}

public CarouselLayoutManager(
Expand Down Expand Up @@ -144,7 +144,7 @@ public void onLayoutChildren(Recycler recycler, State state) {
if (isInitialLoad) {
View firstChild = recycler.getViewForPosition(0);
measureChildWithMargins(firstChild, 0, 0);
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(firstChild);
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(this, firstChild);
keylineStateList =
KeylineStateList.from(this, isRtl ? KeylineState.reverse(keylineState) : keylineState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,18 @@ public final class MultiBrowseCarouselConfiguration extends CarouselConfiguratio
// unmasked items visible at once.
private final boolean forceCompactArrangement;

public MultiBrowseCarouselConfiguration(@NonNull Carousel carousel) {
this(carousel, false);
public MultiBrowseCarouselConfiguration() {
this(false);
}

/**
* Create a new instance of {@link MultiBrowseCarouselConfiguration}.
*
* @param carousel The carousel which items will be fit to
* @param forceCompactArrangement true if items should be fit in a way that maximizes the number
* of large, unmasked items. false if this configuration is free to determine an opinionated
* balance between item sizes.
*/
public MultiBrowseCarouselConfiguration(
@NonNull Carousel carousel, boolean forceCompactArrangement) {
super(carousel);
public MultiBrowseCarouselConfiguration(boolean forceCompactArrangement) {
this.forceCompactArrangement = forceCompactArrangement;
}

Expand All @@ -91,14 +88,15 @@ private static float getChildMaskPercentage(

@Override
@NonNull
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
LayoutParams childLayoutParams = (LayoutParams) child.getLayoutParams();
float childHorizontalMargins = childLayoutParams.leftMargin + childLayoutParams.rightMargin;

float smallChildWidth = getSmallSize(child.getContext()) + childHorizontalMargins;
float extraSmallChildWidth = getExtraSmallSize(child.getContext()) + childHorizontalMargins;

float availableSpace = getCarousel().getContainerWidth();
float availableSpace = carousel.getContainerWidth();

// The minimum viable arrangement is 1 large and 1 small child. A single large item size
// cannot be greater than the available space minus a small child width.
Expand Down Expand Up @@ -193,7 +191,7 @@ protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {

float smallStartCenterX = smallCount > 0 ? start + (smallChildWidth / 2F) : mediumCenterX;

float extraSmallTailCenterX = getCarousel().getContainerWidth() + (extraSmallChildWidth / 2F);
float extraSmallTailCenterX = carousel.getContainerWidth() + (extraSmallChildWidth / 2F);

float extraSmallMask =
getChildMaskPercentage(extraSmallChildWidth, largeChildWidth, childHorizontalMargins);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.material.carousel.CarouselHelper.CarouselTestAdapter;
Expand Down Expand Up @@ -70,9 +71,10 @@ public void setUp() {
@Test
public void testFirstAdapterItem_isDrawnAtRightOfContainer() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -88,9 +90,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() throws Throwable {
KeylineState keylineState = getTestCenteredKeylineState();
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return keylineState;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public void setUp() {
@Test
public void testAddAdapterItem_isAddedByLayoutManager() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -71,9 +72,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
@Test
public void testMeasureChild_usesStateItemSize() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -84,9 +86,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
@Test
public void testMaskedChild_isStillGivenFullWidthBounds() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return new KeylineState.Builder(DEFAULT_ITEM_WIDTH)
.addKeyline(225F, .5F, 225F, true)
.build();
Expand All @@ -102,9 +105,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
@Test
public void testMaskedChild_isMaskedToCorrectSize() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return new KeylineState.Builder(DEFAULT_ITEM_WIDTH)
.addKeyline(225F, .8F, 90F, true)
.build();
Expand All @@ -120,9 +124,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
public void testKnownArrangement_initialScrollPositionHasAllItemsWithinCarouselContainer()
throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -142,9 +147,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
public void testScrollToPosition_movesChildToFocalStartKeyline() throws Throwable {
KeylineState keylineState = getTestCenteredKeylineState();
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return keylineState;
}
});
Expand All @@ -162,9 +168,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() throws Throwable {
KeylineState keylineState = getTestCenteredKeylineState();
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return keylineState;
}
});
Expand All @@ -182,9 +189,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
@Test
public void testInitialFill_shouldFillMinimumItemCountForContainer() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -197,9 +205,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
public void testScrollAndFill_shouldRecycleAndFillMinimumItemCountForContainer()
throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand All @@ -212,9 +221,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
@Test
public void testEmptyAdapter_shouldClearAllViewsFromRecyclerView() throws Throwable {
layoutManager.setCarouselConfiguration(
new CarouselConfiguration(layoutManager) {
new CarouselConfiguration() {
@Override
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
protected KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ public class MultiBrowseCarouselConfigurationTest {

@Test
public void testOnFirstItemMeasuredWithMargins_createsKeylineStateWithCorrectItemSize() {
MultiBrowseCarouselConfiguration config =
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(2470));
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
View view = createViewWithSize(450, 450);

KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
KeylineState keylineState =
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(2470), view);
assertThat(keylineState.getItemSize()).isEqualTo(450F);
}

@Test
public void testItemLargerThanContainer_resizesToFit() {
MultiBrowseCarouselConfiguration config =
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(100));
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
View view = createViewWithSize(400, 400);

KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
KeylineState keylineState =
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(100), view);
assertThat(keylineState.getItemSize()).isAtMost(100F);
}

@Test
public void testItemLargerThanContainerSize_defaultsToFullscreen() {
Carousel carousel = createCarouselWithWidth(100);
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration(carousel);
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
View view = createViewWithSize(400, 400);

KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view);

// A fullscreen layout should be [collapsed-expanded-collapsed] where the collapsed items are
// outside the bounds of the carousel container and the expanded center item takes up the
Expand All @@ -76,11 +76,11 @@ public void testItemLargerThanContainerSize_defaultsToFullscreen() {
public void testKnownArrangement_correctlyCalculatesKeylineLocations() {
float[] locOffsets = new float[] {-.5F, 225F, 675F, 942F, 1012F, 1040.5F};

MultiBrowseCarouselConfiguration config =
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(1040));
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
View view = createViewWithSize(450, 450);

List<Keyline> keylines = config.onFirstChildMeasuredWithMargins(view).getKeylines();
List<Keyline> keylines =
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(1040), view).getKeylines();
for (int i = 0; i < keylines.size(); i++) {
assertThat(keylines.get(i).locOffset).isEqualTo(locOffsets[i]);
}
Expand Down

0 comments on commit 8014267

Please sign in to comment.