Skip to content

Commit

Permalink
[ProgressIndicator] Invalidated indicator colors properly in animator…
Browse files Browse the repository at this point in the history
… delegate.

Resolves #1469

PiperOrigin-RevId: 320622246
  • Loading branch information
pekingme authored and hunterstich committed Jul 10, 2020
1 parent 2eb0b19 commit 43fe75f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,8 @@ void resetPropertiesForNewStart() {
setIndicatorHeadChangeFraction(0f);
setIndicatorTailChangeFraction(0f);
setIndicatorStartOffset(0f);
indicatorColorIndex = 0;
indicatorCollapsingAnimator.setFloatValues(0f, 1f);
colorFadingAnimator.setIntValues(
drawable.combinedIndicatorColorArray[indicatorColorIndex],
drawable.combinedIndicatorColorArray[getNextIndicatorColorIndex()]);
setDisplayedIndicatorColor(drawable.combinedIndicatorColorArray[indicatorColorIndex]);
resetSegmentColors();
}

@Override
Expand All @@ -191,11 +187,12 @@ void resetPropertiesForNextCycle() {
+ INDICATOR_OFFSET_PER_COLOR_DEGREES
+ INDICATOR_DELTA_DEGREES,
360));
indicatorColorIndex = getNextIndicatorColorIndex();
colorFadingAnimator.setIntValues(
drawable.combinedIndicatorColorArray[indicatorColorIndex],
drawable.combinedIndicatorColorArray[getNextIndicatorColorIndex()]);
setDisplayedIndicatorColor(drawable.combinedIndicatorColorArray[indicatorColorIndex]);
shiftSegmentColors();
}

@Override
public void invalidateSpecValues() {
resetSegmentColors();
}

@Override
Expand Down Expand Up @@ -230,6 +227,24 @@ private void updateSegmentPositions() {
/ 360;
}

/** Shifts the color used in the segment colors to the next available one. */
private void shiftSegmentColors() {
indicatorColorIndex = getNextIndicatorColorIndex();
colorFadingAnimator.setIntValues(
drawable.combinedIndicatorColorArray[indicatorColorIndex],
drawable.combinedIndicatorColorArray[getNextIndicatorColorIndex()]);
setDisplayedIndicatorColor(drawable.combinedIndicatorColorArray[indicatorColorIndex]);
}

/** Resets the segment colors to the first indicator color. */
private void resetSegmentColors() {
indicatorColorIndex = 0;
colorFadingAnimator.setIntValues(
drawable.combinedIndicatorColorArray[indicatorColorIndex],
drawable.combinedIndicatorColorArray[getNextIndicatorColorIndex()]);
setDisplayedIndicatorColor(drawable.combinedIndicatorColorArray[indicatorColorIndex]);
}

// ******************* Getters and setters *******************

private int getDisplayedIndicatorColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ protected void registerDrawable(@NonNull IndeterminateDrawable drawable) {
*/
abstract void resetPropertiesForNextCycle();

/**
* Invalidates the spec values used by the animator delegate. When the spec values are changed in
* {@link ProgressIndicator}, values assigned to animators or segments don't get updated until
* they are explicitly reset. Call this to apply the changes immediately.
*/
public abstract void invalidateSpecValues();

/**
* Registers an {@link AnimationCallback} to the animator for the process what needs to be done
* after the current animation cycle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void startAnimator() {
@Override
public void resetPropertiesForNewStart() {
resetPropertiesForNextCycle();
resetIndicatorColors();
resetSegmentColors();
}

@Override
Expand All @@ -136,7 +136,7 @@ public void resetPropertiesForNextCycle() {
setLine1TailFraction(0f);
setLine2HeadFraction(0f);
setLine2TailFraction(0f);
rotateIndicatorColors();
rotateSegmentColors();
}

@Override
Expand All @@ -159,6 +159,11 @@ public void requestCancelAnimatorAfterCurrentCycle() {
animatorCompleteEndRequested = true;
}

@Override
public void invalidateSpecValues() {
resetSegmentColors();
}

@Override
public void registerAnimatorsCompleteCallback(@NonNull AnimationCallback callback) {
animatorCompleteCallback = callback;
Expand All @@ -171,15 +176,15 @@ public void unregisterAnimatorsCompleteCallback() {

// ******************* Helper methods *******************

/** Rotates the color used in segment colors. */
private void rotateIndicatorColors() {
/** Shifts the color used in the segment colors to the next available one. */
private void rotateSegmentColors() {
displayedSegmentColorIndex =
(displayedSegmentColorIndex + 1) % drawable.combinedIndicatorColorArray.length;
Arrays.fill(segmentColors, drawable.combinedIndicatorColorArray[displayedSegmentColorIndex]);
}

/** Resets segment colors to the first indicator color. */
private void resetIndicatorColors() {
/** Resets the segment colors to the first indicator color. */
private void resetSegmentColors() {
displayedSegmentColorIndex = 0;
Arrays.fill(segmentColors, drawable.combinedIndicatorColorArray[displayedSegmentColorIndex]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public LinearIndeterminateSeamlessAnimatorDelegate() {
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
if (getLineConnectPoint2Fraction() > 0 && getLineConnectPoint2Fraction() < 1) {
rotateIndicatorColors();
shiftSegmentColors();
}
}
});
Expand All @@ -86,7 +86,7 @@ public void onAnimationRepeat(Animator animation) {
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
if (getLineConnectPoint1Fraction() > 0 && getLineConnectPoint1Fraction() < 1) {
rotateIndicatorColors();
shiftSegmentColors();
}
}
});
Expand All @@ -110,7 +110,7 @@ public void startAnimator() {
public void resetPropertiesForNewStart() {
setLineConnectPoint1Fraction(0f);
setLineConnectPoint2Fraction(0f);
resetIndicatorColors();
resetSegmentColors();
}

@Override
Expand All @@ -128,6 +128,11 @@ public void requestCancelAnimatorAfterCurrentCycle() {
// In seamless mode, there's no concept of cycle. This is left as blank in purpose.
}

@Override
public void invalidateSpecValues() {
resetSegmentColors();
}

@Override
public void registerAnimatorsCompleteCallback(AnimationCallback callback) {
// In seamless mode, indeterminate mode cannot be switched. This is left as blank in purpose.
Expand All @@ -140,15 +145,15 @@ public void unregisterAnimatorsCompleteCallback() {

// ******************* Helper methods *******************

/** Rotates the color used in segment colors. */
private void rotateIndicatorColors() {
/** Shifts the color used in the segment colors to the next available one. */
private void shiftSegmentColors() {
referenceSegmentColorIndex =
(referenceSegmentColorIndex + 1) % drawable.combinedIndicatorColorArray.length;
updateSegmentColors();
}

/** Resets segment colors to the first indicator color. */
private void resetIndicatorColors() {
/** Resets the segment colors to the first indicator color. */
private void resetSegmentColors() {
referenceSegmentColorIndex = 0;
updateSegmentColors();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ protected boolean isEffectivelyVisible() {
private void updateColorsInDrawables() {
getProgressDrawable().recalculateColors();
getIndeterminateDrawable().recalculateColors();
getIndeterminateDrawable().getAnimatorDelegate().invalidateSpecValues();
}

/** Returns the animator duration scale from developer options setting. */
Expand Down

0 comments on commit 43fe75f

Please sign in to comment.