Skip to content

Commit

Permalink
Merge branch 'decoupling-sort-and-offsets' into ui-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywilson committed Jul 10, 2017
2 parents f9ffe29 + 61ddd08 commit 10839f6
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 75 deletions.
1 change: 1 addition & 0 deletions lib/src/main/java/com/willowtreeapps/spruce/Spruce.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private AnimatorSet getAnimatorSetForSort(Animator[] animators, SortFunction sor
children.add(viewGroup.getChildAt(i));
}

sortFunction.sortChildren(viewGroup, children);
childrenWithTime = sortFunction.getViewListWithTimeOffsets(viewGroup, children);
animatorSet = new AnimatorSet();
List<Animator> animatorsList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ public ContinuousSort(long interObjectDelay, boolean reversed, Position position
@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = getDistanceBetweenPoints(Utils.viewToPoint(left), comparisonPoint);
double rightDistance = getDistanceBetweenPoints(Utils.viewToPoint(right), comparisonPoint);
if (leftDistance > rightDistance && leftDistance > maxDistance) {
maxDistance = leftDistance;
}
return Double.compare(leftDistance, rightDistance);
}
});

List<SpruceTimedView> timedViews = new ArrayList<>();
for (View view : children) {
double normalizedDistance;
Expand All @@ -83,4 +70,21 @@ public int compare(View left, View right) {

return timedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = getDistanceBetweenPoints(Utils.viewToPoint(left), comparisonPoint);
double rightDistance = getDistanceBetweenPoints(Utils.viewToPoint(right), comparisonPoint);
if (leftDistance > rightDistance && leftDistance > maxDistance) {
maxDistance = leftDistance;
}
return Double.compare(leftDistance, rightDistance);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,6 @@ public ContinuousWeightedSort(long interObjectDelay, boolean reversed, Position
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

// non-sort to calculate max horizontal/vertical values
Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(left)) * horizontalWeight;
double rightHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(right)) * horizontalWeight;
double leftVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(left)) * verticalWeight;
double rightVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(right)) * verticalWeight;

maxHorizontalDistance = calculateMaxDistance(leftHorizontalDistance, rightHorizontalDistance, maxHorizontalDistance);
maxVerticalDistance = calculateMaxDistance(leftVerticalDistance, rightVerticalDistance, maxVerticalDistance);

return 0;
}
});

List<SpruceTimedView> timedViews = new ArrayList<>();
long maxTimeOffset = 1;
for (View view : children) {
Expand All @@ -115,6 +99,27 @@ public int compare(View left, View right) {
return timedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

// non-sort to calculate max horizontal/vertical values
Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(left)) * horizontalWeight;
double rightHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(right)) * horizontalWeight;
double leftVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(left)) * verticalWeight;
double rightVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(right)) * verticalWeight;

maxHorizontalDistance = calculateMaxDistance(leftHorizontalDistance, rightHorizontalDistance, maxHorizontalDistance);
maxVerticalDistance = calculateMaxDistance(leftVerticalDistance, rightVerticalDistance, maxVerticalDistance);

return 0;
}
});
}

@VisibleForTesting
double calculateMaxDistance(double leftHorizontalDistance, double rightHorizontalDistance, double maximum) {
if (leftHorizontalDistance > rightHorizontalDistance && leftHorizontalDistance > maximum) {
Expand Down
28 changes: 16 additions & 12 deletions lib/src/main/java/com/willowtreeapps/spruce/sort/CorneredSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<V
List<SpruceTimedView> timedViews = new ArrayList<>();
long currentTimeOffset = 0;

Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = Math.abs(comparisonPoint.x - left.getX()) + Math.abs(comparisonPoint.y - left.getY());
double rightDistance = Math.abs(comparisonPoint.x - right.getX()) + Math.abs(comparisonPoint.y - right.getY());
if (reversed) {
return Double.compare(rightDistance, leftDistance);
}
return Double.compare(leftDistance, rightDistance);
}
});

double lastDistance = 0;
for (View view : children) {
double viewDistance = getDistanceBetweenPoints(Utils.viewToPoint(view), comparisonPoint);
Expand All @@ -92,6 +80,22 @@ public int compare(View left, View right) {
return timedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);
Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = Math.abs(comparisonPoint.x - left.getX()) + Math.abs(comparisonPoint.y - left.getY());
double rightDistance = Math.abs(comparisonPoint.x - right.getX()) + Math.abs(comparisonPoint.y - right.getY());
if (reversed) {
return Double.compare(rightDistance, leftDistance);
}
return Double.compare(leftDistance, rightDistance);
}
});
}

@Override
public PointF getDistancePoint(ViewGroup parent, List<View> children) {
PointF distancePoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<V
return childTimedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
// Do nothing
}
}
29 changes: 16 additions & 13 deletions lib/src/main/java/com/willowtreeapps/spruce/sort/DistancedSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,6 @@ class DistancedSort extends SortFunction {
@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = getDistanceBetweenPoints(Utils.viewToPoint(left), comparisonPoint);
double rightDistance = getDistanceBetweenPoints(Utils.viewToPoint(right), comparisonPoint);
if (reversed) {
return Double.compare(rightDistance, leftDistance);
}
return Double.compare(leftDistance, rightDistance);
}
});

double lastDistance = getDistanceBetweenPoints(Utils.viewToPoint(children.get(0)), comparisonPoint);
long currentTimeOffset = 0L;
List<SpruceTimedView> childViews = new ArrayList<>();
Expand All @@ -81,6 +68,22 @@ public int compare(View left, View right) {
return childViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);
Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
double leftDistance = getDistanceBetweenPoints(Utils.viewToPoint(left), comparisonPoint);
double rightDistance = getDistanceBetweenPoints(Utils.viewToPoint(right), comparisonPoint);
if (reversed) {
return Double.compare(rightDistance, leftDistance);
}
return Double.compare(leftDistance, rightDistance);
}
});
}

/**
* Get the point that's closest to the start point
*
Expand Down
27 changes: 15 additions & 12 deletions lib/src/main/java/com/willowtreeapps/spruce/sort/InlineSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,24 @@ public InlineSort(long interObjectDelay, boolean reversed, Corner corner) {

@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);
List<SpruceTimedView> timedViews = new ArrayList<>();
long currentTimeOffset = 0;

if (reversed) {
Collections.reverse(children);
}

for (View view : children) {
timedViews.add(new SpruceTimedView(view, currentTimeOffset));
currentTimeOffset += interObjectDelay;
}

return timedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);
Collections.sort(children, new Comparator<View>() {
@Override
public int compare(View left, View right) {
Expand All @@ -71,17 +85,6 @@ public int compare(View left, View right) {
return 1;
}
});

if (reversed) {
Collections.reverse(children);
}

for (View view : children) {
timedViews.add(new SpruceTimedView(view, currentTimeOffset));
currentTimeOffset += interObjectDelay;
}

return timedViews;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ public LinearSort(long interObjectDelay, boolean reversed, Direction direction)
this.direction = direction;
}

@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
return super.getViewListWithTimeOffsets(parent, children);
}


@Override
public PointF getDistancePoint(ViewGroup parent, List<View> children) {
PointF point = super.getDistancePoint(parent, children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public RandomSort(long interObjectDelay) {

@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
// randomize view list
Collections.shuffle(children);

List<SpruceTimedView> timedViews = new ArrayList<>();
long currentTimeOffset = 0;

Expand All @@ -58,4 +55,8 @@ public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<V
return timedViews;
}

@Override
public void sortChildren(ViewGroup parent, List<View> children) {
Collections.shuffle(children);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@

public abstract class SortFunction {

/**
* Sorts the children that will be animated in the view.
*
* @param parent ViewGroup parent of the collection of child views.
* @param children The children to be sorted
*/
public abstract void sortChildren(ViewGroup parent, List<View> children);

/**
* Get a list of SpruceTimedView
*
Expand Down

0 comments on commit 10839f6

Please sign in to comment.