Skip to content

Commit 8014267

Browse files
hunterstichdsn5ft
authored andcommitted
[Carousel] Changed CarouselConfiguration to have an empty constructor and pass Carousel in through onFirstChildMeasuredWithMargins
PiperOrigin-RevId: 508389752
1 parent 697d93a commit 8014267

File tree

7 files changed

+61
-61
lines changed

7 files changed

+61
-61
lines changed

catalog/java/io/material/catalog/carousel/MultiBrowseDemoFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
7979
forceCompactSwitch.setOnCheckedChangeListener(
8080
(buttonView, isChecked) ->
8181
multiBrowseStartCarouselLayoutManager.setCarouselConfiguration(
82-
new MultiBrowseCarouselConfiguration(
83-
multiBrowseStartCarouselLayoutManager, isChecked)));
82+
new MultiBrowseCarouselConfiguration(isChecked)));
8483

8584
drawDividers.setOnCheckedChangeListener(
8685
(buttonView, isChecked) -> {

lib/java/com/google/android/material/carousel/CarouselConfiguration.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
*/
3030
abstract class CarouselConfiguration {
3131

32-
private final Carousel carousel;
33-
34-
CarouselConfiguration(@NonNull Carousel carousel) {
35-
this.carousel = carousel;
36-
}
37-
3832
/**
3933
* Calculates a keyline arrangement and returns a constructed {@link KeylineState}.
4034
*
@@ -56,14 +50,10 @@ abstract class CarouselConfiguration {
5650
*
5751
* @param child The first measured view from the carousel, use this view to determine the max size
5852
* that all items in the carousel will be given.
53+
* @param carousel The carousel to create a {@link KeylineState} for
5954
* @return A {@link KeylineState} to be used by the layout manager to offset and mask children
6055
* along the scrolling axis.
6156
*/
62-
protected abstract KeylineState onFirstChildMeasuredWithMargins(@NonNull View child);
63-
64-
/** Gets the {@link Carousel} associated with this configuration. */
65-
@NonNull
66-
protected final Carousel getCarousel() {
67-
return carousel;
68-
}
57+
protected abstract KeylineState onFirstChildMeasuredWithMargins(
58+
@NonNull Carousel carousel, @NonNull View child);
6959
}

lib/java/com/google/android/material/carousel/CarouselLayoutManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private static final class ChildCalculations {
109109
}
110110

111111
public CarouselLayoutManager() {
112-
setCarouselConfiguration(new MultiBrowseCarouselConfiguration(this));
112+
setCarouselConfiguration(new MultiBrowseCarouselConfiguration());
113113
}
114114

115115
public CarouselLayoutManager(
@@ -144,7 +144,7 @@ public void onLayoutChildren(Recycler recycler, State state) {
144144
if (isInitialLoad) {
145145
View firstChild = recycler.getViewForPosition(0);
146146
measureChildWithMargins(firstChild, 0, 0);
147-
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(firstChild);
147+
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(this, firstChild);
148148
keylineStateList =
149149
KeylineStateList.from(this, isRtl ? KeylineState.reverse(keylineState) : keylineState);
150150
}

lib/java/com/google/android/material/carousel/MultiBrowseCarouselConfiguration.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,18 @@ public final class MultiBrowseCarouselConfiguration extends CarouselConfiguratio
5757
// unmasked items visible at once.
5858
private final boolean forceCompactArrangement;
5959

60-
public MultiBrowseCarouselConfiguration(@NonNull Carousel carousel) {
61-
this(carousel, false);
60+
public MultiBrowseCarouselConfiguration() {
61+
this(false);
6262
}
6363

6464
/**
6565
* Create a new instance of {@link MultiBrowseCarouselConfiguration}.
6666
*
67-
* @param carousel The carousel which items will be fit to
6867
* @param forceCompactArrangement true if items should be fit in a way that maximizes the number
6968
* of large, unmasked items. false if this configuration is free to determine an opinionated
7069
* balance between item sizes.
7170
*/
72-
public MultiBrowseCarouselConfiguration(
73-
@NonNull Carousel carousel, boolean forceCompactArrangement) {
74-
super(carousel);
71+
public MultiBrowseCarouselConfiguration(boolean forceCompactArrangement) {
7572
this.forceCompactArrangement = forceCompactArrangement;
7673
}
7774

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

9289
@Override
9390
@NonNull
94-
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
91+
protected KeylineState onFirstChildMeasuredWithMargins(
92+
@NonNull Carousel carousel, @NonNull View child) {
9593
LayoutParams childLayoutParams = (LayoutParams) child.getLayoutParams();
9694
float childHorizontalMargins = childLayoutParams.leftMargin + childLayoutParams.rightMargin;
9795

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

101-
float availableSpace = getCarousel().getContainerWidth();
99+
float availableSpace = carousel.getContainerWidth();
102100

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

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

196-
float extraSmallTailCenterX = getCarousel().getContainerWidth() + (extraSmallChildWidth / 2F);
194+
float extraSmallTailCenterX = carousel.getContainerWidth() + (extraSmallChildWidth / 2F);
197195

198196
float extraSmallMask =
199197
getChildMaskPercentage(extraSmallChildWidth, largeChildWidth, childHorizontalMargins);

lib/javatests/com/google/android/material/carousel/CarouselLayoutManagerRtlTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import androidx.recyclerview.widget.RecyclerView;
3333
import android.view.View;
3434
import android.view.ViewGroup.LayoutParams;
35+
import androidx.annotation.NonNull;
3536
import androidx.annotation.RequiresApi;
3637
import androidx.test.core.app.ApplicationProvider;
3738
import com.google.android.material.carousel.CarouselHelper.CarouselTestAdapter;
@@ -70,9 +71,10 @@ public void setUp() {
7071
@Test
7172
public void testFirstAdapterItem_isDrawnAtRightOfContainer() throws Throwable {
7273
layoutManager.setCarouselConfiguration(
73-
new CarouselConfiguration(layoutManager) {
74+
new CarouselConfiguration() {
7475
@Override
75-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
76+
protected KeylineState onFirstChildMeasuredWithMargins(
77+
@NonNull Carousel carousel, @NonNull View child) {
7678
return getTestCenteredKeylineState();
7779
}
7880
});
@@ -88,9 +90,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
8890
public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() throws Throwable {
8991
KeylineState keylineState = getTestCenteredKeylineState();
9092
layoutManager.setCarouselConfiguration(
91-
new CarouselConfiguration(layoutManager) {
93+
new CarouselConfiguration() {
9294
@Override
93-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
95+
protected KeylineState onFirstChildMeasuredWithMargins(
96+
@NonNull Carousel carousel, @NonNull View child) {
9497
return keylineState;
9598
}
9699
});

lib/javatests/com/google/android/material/carousel/CarouselLayoutManagerTest.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public void setUp() {
5858
@Test
5959
public void testAddAdapterItem_isAddedByLayoutManager() throws Throwable {
6060
layoutManager.setCarouselConfiguration(
61-
new CarouselConfiguration(layoutManager) {
61+
new CarouselConfiguration() {
6262
@Override
63-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
63+
protected KeylineState onFirstChildMeasuredWithMargins(
64+
@NonNull Carousel carousel, @NonNull View child) {
6465
return getTestCenteredKeylineState();
6566
}
6667
});
@@ -71,9 +72,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
7172
@Test
7273
public void testMeasureChild_usesStateItemSize() throws Throwable {
7374
layoutManager.setCarouselConfiguration(
74-
new CarouselConfiguration(layoutManager) {
75+
new CarouselConfiguration() {
7576
@Override
76-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
77+
protected KeylineState onFirstChildMeasuredWithMargins(
78+
@NonNull Carousel carousel, @NonNull View child) {
7779
return getTestCenteredKeylineState();
7880
}
7981
});
@@ -84,9 +86,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
8486
@Test
8587
public void testMaskedChild_isStillGivenFullWidthBounds() throws Throwable {
8688
layoutManager.setCarouselConfiguration(
87-
new CarouselConfiguration(layoutManager) {
89+
new CarouselConfiguration() {
8890
@Override
89-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
91+
protected KeylineState onFirstChildMeasuredWithMargins(
92+
@NonNull Carousel carousel, @NonNull View child) {
9093
return new KeylineState.Builder(DEFAULT_ITEM_WIDTH)
9194
.addKeyline(225F, .5F, 225F, true)
9295
.build();
@@ -102,9 +105,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
102105
@Test
103106
public void testMaskedChild_isMaskedToCorrectSize() throws Throwable {
104107
layoutManager.setCarouselConfiguration(
105-
new CarouselConfiguration(layoutManager) {
108+
new CarouselConfiguration() {
106109
@Override
107-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
110+
protected KeylineState onFirstChildMeasuredWithMargins(
111+
@NonNull Carousel carousel, @NonNull View child) {
108112
return new KeylineState.Builder(DEFAULT_ITEM_WIDTH)
109113
.addKeyline(225F, .8F, 90F, true)
110114
.build();
@@ -120,9 +124,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
120124
public void testKnownArrangement_initialScrollPositionHasAllItemsWithinCarouselContainer()
121125
throws Throwable {
122126
layoutManager.setCarouselConfiguration(
123-
new CarouselConfiguration(layoutManager) {
127+
new CarouselConfiguration() {
124128
@Override
125-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
129+
protected KeylineState onFirstChildMeasuredWithMargins(
130+
@NonNull Carousel carousel, @NonNull View child) {
126131
return getTestCenteredKeylineState();
127132
}
128133
});
@@ -142,9 +147,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
142147
public void testScrollToPosition_movesChildToFocalStartKeyline() throws Throwable {
143148
KeylineState keylineState = getTestCenteredKeylineState();
144149
layoutManager.setCarouselConfiguration(
145-
new CarouselConfiguration(layoutManager) {
150+
new CarouselConfiguration() {
146151
@Override
147-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
152+
protected KeylineState onFirstChildMeasuredWithMargins(
153+
@NonNull Carousel carousel, @NonNull View child) {
148154
return keylineState;
149155
}
150156
});
@@ -162,9 +168,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
162168
public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() throws Throwable {
163169
KeylineState keylineState = getTestCenteredKeylineState();
164170
layoutManager.setCarouselConfiguration(
165-
new CarouselConfiguration(layoutManager) {
171+
new CarouselConfiguration() {
166172
@Override
167-
protected KeylineState onFirstChildMeasuredWithMargins(View child) {
173+
protected KeylineState onFirstChildMeasuredWithMargins(
174+
@NonNull Carousel carousel, @NonNull View child) {
168175
return keylineState;
169176
}
170177
});
@@ -182,9 +189,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(View child) {
182189
@Test
183190
public void testInitialFill_shouldFillMinimumItemCountForContainer() throws Throwable {
184191
layoutManager.setCarouselConfiguration(
185-
new CarouselConfiguration(layoutManager) {
192+
new CarouselConfiguration() {
186193
@Override
187-
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
194+
protected KeylineState onFirstChildMeasuredWithMargins(
195+
@NonNull Carousel carousel, @NonNull View child) {
188196
return getTestCenteredKeylineState();
189197
}
190198
});
@@ -197,9 +205,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
197205
public void testScrollAndFill_shouldRecycleAndFillMinimumItemCountForContainer()
198206
throws Throwable {
199207
layoutManager.setCarouselConfiguration(
200-
new CarouselConfiguration(layoutManager) {
208+
new CarouselConfiguration() {
201209
@Override
202-
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
210+
protected KeylineState onFirstChildMeasuredWithMargins(
211+
@NonNull Carousel carousel, @NonNull View child) {
203212
return getTestCenteredKeylineState();
204213
}
205214
});
@@ -212,9 +221,10 @@ protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
212221
@Test
213222
public void testEmptyAdapter_shouldClearAllViewsFromRecyclerView() throws Throwable {
214223
layoutManager.setCarouselConfiguration(
215-
new CarouselConfiguration(layoutManager) {
224+
new CarouselConfiguration() {
216225
@Override
217-
protected KeylineState onFirstChildMeasuredWithMargins(@NonNull View child) {
226+
protected KeylineState onFirstChildMeasuredWithMargins(
227+
@NonNull Carousel carousel, @NonNull View child) {
218228
return getTestCenteredKeylineState();
219229
}
220230
});

lib/javatests/com/google/android/material/carousel/MultiBrowseCarouselConfigurationTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,31 @@ public class MultiBrowseCarouselConfigurationTest {
3535

3636
@Test
3737
public void testOnFirstItemMeasuredWithMargins_createsKeylineStateWithCorrectItemSize() {
38-
MultiBrowseCarouselConfiguration config =
39-
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(2470));
38+
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
4039
View view = createViewWithSize(450, 450);
4140

42-
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
41+
KeylineState keylineState =
42+
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(2470), view);
4343
assertThat(keylineState.getItemSize()).isEqualTo(450F);
4444
}
4545

4646
@Test
4747
public void testItemLargerThanContainer_resizesToFit() {
48-
MultiBrowseCarouselConfiguration config =
49-
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(100));
48+
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
5049
View view = createViewWithSize(400, 400);
5150

52-
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
51+
KeylineState keylineState =
52+
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(100), view);
5353
assertThat(keylineState.getItemSize()).isAtMost(100F);
5454
}
5555

5656
@Test
5757
public void testItemLargerThanContainerSize_defaultsToFullscreen() {
5858
Carousel carousel = createCarouselWithWidth(100);
59-
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration(carousel);
59+
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
6060
View view = createViewWithSize(400, 400);
6161

62-
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(view);
62+
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view);
6363

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

79-
MultiBrowseCarouselConfiguration config =
80-
new MultiBrowseCarouselConfiguration(createCarouselWithWidth(1040));
79+
MultiBrowseCarouselConfiguration config = new MultiBrowseCarouselConfiguration();
8180
View view = createViewWithSize(450, 450);
8281

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

0 commit comments

Comments
 (0)