Skip to content

Commit

Permalink
tests passings
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywilson committed Jul 11, 2017
1 parent fb20a21 commit 33673bc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class ContinuousSort extends RadialSort {

private final long duration;
private final boolean reversed;
private double maxDistance;

/**
* Establishes the delay between object animations and their starting position based on distance,
Expand All @@ -54,6 +53,20 @@ public ContinuousSort(long interObjectDelay, boolean reversed, Position position
@Override
public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children) {
final PointF comparisonPoint = getDistancePoint(parent, children);

double maxDistance = 0;
for (View v1: children) {
for (View v2: children) {
if (v1 != v2) {
double leftDistance = getDistanceBetweenPoints(Utils.viewToPoint(v1), comparisonPoint);
double rightDistance = getDistanceBetweenPoints(Utils.viewToPoint(v2), comparisonPoint);
if (leftDistance > rightDistance && leftDistance > maxDistance) {
maxDistance = leftDistance;
}
}
}
}

List<SpruceTimedView> timedViews = new ArrayList<>();
for (View view : children) {
double normalizedDistance;
Expand All @@ -74,17 +87,14 @@ public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<V
@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 @@ -75,6 +75,18 @@ public ContinuousWeightedSort(long interObjectDelay, boolean reversed, 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 v1, View v2) {
double leftHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(v1)) * horizontalWeight;
double rightHorizontalDistance = Utils.horizontalDistance(comparisonPoint, Utils.viewToPoint(v2)) * horizontalWeight;
double leftVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(v1)) * verticalWeight;
double rightVerticalDistance = Utils.verticalDistance(comparisonPoint, Utils.viewToPoint(v2)) * verticalWeight;
maxHorizontalDistance = calculateMaxDistance(leftHorizontalDistance, rightHorizontalDistance, maxHorizontalDistance);
maxVerticalDistance = calculateMaxDistance(leftVerticalDistance, rightVerticalDistance, maxVerticalDistance);
return 0;
}
});

List<SpruceTimedView> timedViews = new ArrayList<>();
long maxTimeOffset = 1;
Expand All @@ -101,23 +113,7 @@ public List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<V

@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;
}
});
// Do nothing
}

@VisibleForTesting
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/com/willowtreeapps/spruce/sort/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.graphics.PointF;
import android.view.View;

import java.util.List;

/**
* Utility class for distance calculations and view to point conversion
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ public void test_negative_inter_object_delay() {

@Test
public void test_negative_inter_object_delay_with_reversed() {
List<SpruceTimedView> resultViews = new ContinuousWeightedSort(/*interObjectDelay=*/-1,
ContinuousWeightedSort sortFunction = new ContinuousWeightedSort(/*interObjectDelay=*/-1,
/*reversed=*/true,
RadialSort.Position.TOP_LEFT,
ContinuousWeightedSort.MEDIUM_WEIGHT,
ContinuousWeightedSort.MEDIUM_WEIGHT)
.getViewListWithTimeOffsets(mockParent, mockChildren);
ContinuousWeightedSort.MEDIUM_WEIGHT);
sortFunction.sortChildren(mockParent, mockChildren);
List<SpruceTimedView> resultViews = sortFunction.getViewListWithTimeOffsets(mockParent, mockChildren);
Assert.assertEquals(-1, resultViews.get(0).getTimeOffset());
Assert.assertEquals(-1, resultViews.get(1).getTimeOffset());
Assert.assertEquals(0, resultViews.get(2).getTimeOffset());
Expand Down

0 comments on commit 33673bc

Please sign in to comment.