Skip to content

Commit

Permalink
[Carousel] Fixed formatted for KeylineState and KeylineStateList
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 565711087
  • Loading branch information
hunterstich authored and leticiarossi committed Sep 15, 2023
1 parent 7151714 commit b80d9a5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
21 changes: 10 additions & 11 deletions lib/java/com/google/android/material/carousel/KeylineState.java
Expand Up @@ -45,9 +45,9 @@
* <p>Keylines can be either focal or non-focal. A focal keyline is a keyline where items are
* considered visible or interactable in their fullest form. This usually means where items will be
* fully unmaksed and viewable. There must be at least one focal keyline in a KeylineState. The
* focal keylines are important for usability and alignment. Start-aligned strategies should
* place focal keylines at the beginning of the scroll container, center-aligned strategies at
* the center of the scroll container, etc.
* focal keylines are important for usability and alignment. Start-aligned strategies should place
* focal keylines at the beginning of the scroll container, center-aligned strategies at the center
* of the scroll container, etc.
*/
final class KeylineState {

Expand Down Expand Up @@ -126,7 +126,7 @@ Keyline getFirstNonAnchorKeyline() {
/** Returns the last non-anchor keyline. */
@Nullable
Keyline getLastNonAnchorKeyline() {
for (int i = keylines.size()-1; i >= 0; i--) {
for (int i = keylines.size() - 1; i >= 0; i--) {
Keyline keyline = keylines.get(i);
if (!keyline.isAnchor) {
return keyline;
Expand Down Expand Up @@ -338,8 +338,7 @@ Builder addKeyline(
}
if (isAnchor) {
if (isFocal) {
throw new IllegalArgumentException(
"Anchor keylines cannot be focal.");
throw new IllegalArgumentException("Anchor keylines cannot be focal.");
}
if (latestAnchorKeylineIndex != NO_INDEX && latestAnchorKeylineIndex != 0) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -421,8 +420,8 @@ Builder addKeyline(
// Calculate if the item will be cut off on either side. Currently we do not support an item
// cut off on both sides as we do not not support that use case. If an item is cut off on both
// sides, only the end cutoff will be included in the cutoff.
float keylineStart = offsetLoc - maskedItemSize/2F;
float keylineEnd = offsetLoc + maskedItemSize/2F;
float keylineStart = offsetLoc - maskedItemSize / 2F;
float keylineEnd = offsetLoc + maskedItemSize / 2F;
if (keylineEnd > availableSpace) {
cutoff = Math.abs(keylineEnd - max(keylineEnd - maskedItemSize, availableSpace));
} else if (keylineStart < 0) {
Expand All @@ -436,9 +435,9 @@ Builder addKeyline(
* Adds an anchor keyline along the scrolling axis where an object should be masked by the given
* {@code mask} and positioned at {@code offsetLoc}.
*
* <p>Anchor keylines are keylines that are added to increase motion of carousel items going
* out of bounds of the carousel, and are 'anchored' (ie. does not shift). These keylines must
* be at the start or end of all keylines.
* <p>Anchor keylines are keylines that are added to increase motion of carousel items going out
* of bounds of the carousel, and are 'anchored' (ie. does not shift). These keylines must be at
* the start or end of all keylines.
*
* <p>Note that calls to {@link #addKeyline(float, float, float, boolean)} and {@link
* #addKeylineRange(float, float, float, int)} are added in order. This method should be called
Expand Down
76 changes: 37 additions & 39 deletions lib/java/com/google/android/material/carousel/KeylineStateList.java
Expand Up @@ -144,7 +144,7 @@ public KeylineState getShiftedState(
* been shifted exactly according to the scroll offset.
* @return a {@link KeylineState} that has been shifted according on the scroll offset.
*/
KeylineState getShiftedState(
KeylineState getShiftedState(
float scrollOffset,
float minScrollOffset,
float maxScrollOffset,
Expand All @@ -154,34 +154,34 @@ KeylineState getShiftedState(
List<KeylineState> steps;
float[] interpolationPoints;
float interpolation;
if (scrollOffset < startShiftOffset) {
interpolation =
AnimationUtils.lerp(
/* outputMin= */ 1F,
/* outputMax= */ 0F,
/* inputMin: */ minScrollOffset,
/* inputMax= */ startShiftOffset,
/* value= */ scrollOffset);
steps = startStateSteps;
interpolationPoints = startStateStepsInterpolationPoints;
} else if (scrollOffset > endShiftOffset) {
interpolation =
AnimationUtils.lerp(
/* outputMin= */ 0F,
/* outputMax= */ 1F,
/* inputMin= */ endShiftOffset,
/* inputMax= */ maxScrollOffset,
/* value= */ scrollOffset);
steps = endStateSteps;
interpolationPoints = endStateStepsInterpolationPoints;
} else {
return defaultState;
}

if (roundToNearestStep) {
return closestStateStepFromInterpolation(steps, interpolation, interpolationPoints);
}
return lerp(steps, interpolation, interpolationPoints);
if (scrollOffset < startShiftOffset) {
interpolation =
AnimationUtils.lerp(
/* outputMin= */ 1F,
/* outputMax= */ 0F,
/* inputMin: */ minScrollOffset,
/* inputMax= */ startShiftOffset,
/* value= */ scrollOffset);
steps = startStateSteps;
interpolationPoints = startStateStepsInterpolationPoints;
} else if (scrollOffset > endShiftOffset) {
interpolation =
AnimationUtils.lerp(
/* outputMin= */ 0F,
/* outputMax= */ 1F,
/* inputMin= */ endShiftOffset,
/* inputMax= */ maxScrollOffset,
/* value= */ scrollOffset);
steps = endStateSteps;
interpolationPoints = endStateStepsInterpolationPoints;
} else {
return defaultState;
}

if (roundToNearestStep) {
return closestStateStepFromInterpolation(steps, interpolation, interpolationPoints);
}
return lerp(steps, interpolation, interpolationPoints);
}

/**
Expand Down Expand Up @@ -213,12 +213,12 @@ private static KeylineState lerp(
* @param stateSteps The steps in which to determine the 2 state steps we are in between.
* @param interpolation The interpolation of the state steps we are at.
* @param stateStepsInterpolationPoints The state step interpolation points; each interpolation
* point corresponds to at which interpolation we are at the corresponding state step.
* point corresponds to at which interpolation we are at the corresponding state step.
* @return an array of the form [progress, fromIndex, toIndex] where progress represents the
* progress in between the state steps at fromIndex and toIndex.
* progress in between the state steps at fromIndex and toIndex.
*/
private static float[] getStateStepsRange(List<KeylineState> stateSteps,
float interpolation, float[] stateStepsInterpolationPoints) {
private static float[] getStateStepsRange(
List<KeylineState> stateSteps, float interpolation, float[] stateStepsInterpolationPoints) {
int numberOfSteps = stateSteps.size();
// Find the step that contains `interpolation` and remap the the surrounding interpolation
// points lower and upper bounds to its own 0-1 value.
Expand Down Expand Up @@ -365,8 +365,7 @@ private static List<KeylineState> getStateStepsStart(
// If the first focal item is already at the left of the container or there are no in bounds
// keylines, return a list of steps that only includes the default state (there is nowhere to
// shift).
if (isFirstFocalItemAtLeftOfContainer(defaultState)
|| firstNonAnchorKeylineIndex == NO_INDEX) {
if (isFirstFocalItemAtLeftOfContainer(defaultState) || firstNonAnchorKeylineIndex == NO_INDEX) {
return steps;
}

Expand Down Expand Up @@ -438,8 +437,7 @@ private static List<KeylineState> getStateStepsStart(
* last state will be the right state or the state that has the focal range at the right of the
* carousel.
*/
private static List<KeylineState> getStateStepsEnd(
Carousel carousel, KeylineState defaultState) {
private static List<KeylineState> getStateStepsEnd(Carousel carousel, KeylineState defaultState) {
List<KeylineState> steps = new ArrayList<>();
steps.add(defaultState);
int lastNonAnchorKeylineIndex = findLastNonAnchorKeylineIndex(defaultState);
Expand Down Expand Up @@ -643,9 +641,9 @@ Map<Integer, KeylineState> getKeylineStateForPositionMap(
keylineStates.put(
position,
startStateSteps.get(MathUtils.clamp(startStepsIndex, 0, startStateSteps.size() - 1)));
startStepsIndex++;
}
startStepsIndex++;
}
}
return keylineStates;
}
}

0 comments on commit b80d9a5

Please sign in to comment.