Skip to content

Commit

Permalink
Preferred TickerColumn Scrolling Direction (#100)
Browse files Browse the repository at this point in the history
Add support for setPreferredScrollingDirection
  • Loading branch information
mattsilber authored and jinatonic committed May 29, 2019
1 parent c5aca40 commit c5f719f
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ android:gravity="center"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
app:ticker_animationDuration="1500"
app:ticker_preferredScrollingDirection="any"
```

Or Java:
Expand All @@ -79,6 +80,7 @@ tickerView.setTypeface(myCustomTypeface);
tickerView.setAnimationDuration(500);
tickerView.setAnimationInterpolator(new OvershootInterpolator());
tickerView.setGravity(Gravity.START);
tickerView.setPreferredScrollingDirection(TickerView.ScrollingDirection.ANY);
```

For the full list of XML attributes that we support, please refer to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ protected void onCreate(Bundle savedInstanceState) {
ticker2 = findViewById(R.id.ticker2);
ticker3 = findViewById(R.id.ticker3);

ticker1.setPreferredScrollingDirection(TickerView.ScrollingDirection.DOWN);
ticker2.setPreferredScrollingDirection(TickerView.ScrollingDirection.UP);
ticker3.setPreferredScrollingDirection(TickerView.ScrollingDirection.ANY);

findViewById(R.id.perfBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
50 changes: 36 additions & 14 deletions ticker/src/main/java/com/robinhood/ticker/TickerCharacterList.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,55 @@ class TickerCharacterList {
/**
* @param start the character that we want to animate from
* @param end the character that we want to animate to
* @param direction the preferred {@Link TickerView#ScrollingDirection}
* @return a valid pair of start and end indices, or null if the inputs are not supported.
*/
CharacterIndices getCharacterIndices(char start, char end) {
CharacterIndices getCharacterIndices(char start, char end, TickerView.ScrollingDirection direction) {
int startIndex = getIndexOfChar(start);
int endIndex = getIndexOfChar(end);

if (startIndex < 0 || endIndex < 0) {
return null;
}

// see if the wrap-around animation is shorter distance than the original animation
if (start != TickerUtils.EMPTY_CHAR && end != TickerUtils.EMPTY_CHAR) {
if (endIndex < startIndex) {
// If we are potentially going backwards
final int nonWrapDistance = startIndex - endIndex;
final int wrapDistance = numOriginalCharacters - startIndex + endIndex;
if (wrapDistance < nonWrapDistance) {
switch (direction) {
case DOWN:
if (end == TickerUtils.EMPTY_CHAR) {
endIndex = characterList.length;
} else if (endIndex < startIndex) {
endIndex += numOriginalCharacters;
}
} else if (startIndex < endIndex) {
// If we are potentially going forwards
final int nonWrapDistance = endIndex - startIndex;
final int wrapDistance = numOriginalCharacters - endIndex + startIndex;
if (wrapDistance < nonWrapDistance) {

break;
case UP:
if (startIndex < endIndex) {
startIndex += numOriginalCharacters;
}
}

break;
case ANY:
// see if the wrap-around animation is shorter distance than the original animation
if (start != TickerUtils.EMPTY_CHAR && end != TickerUtils.EMPTY_CHAR) {
if (endIndex < startIndex) {
// If we are potentially going backwards
final int nonWrapDistance = startIndex - endIndex;
final int wrapDistance = numOriginalCharacters - startIndex + endIndex;
if (wrapDistance < nonWrapDistance) {
endIndex += numOriginalCharacters;
}
} else if (startIndex < endIndex) {
// If we are potentially going forwards
final int nonWrapDistance = endIndex - startIndex;
final int wrapDistance = numOriginalCharacters - endIndex + startIndex;
if (wrapDistance < nonWrapDistance) {
startIndex += numOriginalCharacters;
}
}
}

break;
}

return new CharacterIndices(startIndex, endIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void setCharacterIndices() {

for (int i = 0; i < characterLists.length; i++) {
final TickerCharacterList.CharacterIndices indices =
characterLists[i].getCharacterIndices(currentChar, targetChar);
characterLists[i].getCharacterIndices(currentChar, targetChar, metrics.getPreferredScrollingDirection());
if (indices != null) {
this.currentCharacterList = this.characterLists[i].getCharacterList();
this.startIndex = indices.startIndex;
Expand Down
10 changes: 10 additions & 0 deletions ticker/src/main/java/com/robinhood/ticker/TickerDrawMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TickerDrawMetrics {
private final Map<Character, Float> charWidths = new HashMap<>(256);
private float charHeight, charBaseline;

private TickerView.ScrollingDirection preferredScrollingDirection = TickerView.ScrollingDirection.ANY;

TickerDrawMetrics(Paint textPaint) {
this.textPaint = textPaint;
invalidate();
Expand Down Expand Up @@ -71,4 +73,12 @@ float getCharHeight() {
float getCharBaseline() {
return charBaseline;
}

TickerView.ScrollingDirection getPreferredScrollingDirection() {
return preferredScrollingDirection;
}

void setPreferredScrollingDirection(TickerView.ScrollingDirection preferredScrollingDirection) {
this.preferredScrollingDirection = preferredScrollingDirection;
}
}
35 changes: 35 additions & 0 deletions ticker/src/main/java/com/robinhood/ticker/TickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
* @author Jin Cao, Robinhood
*/
public class TickerView extends View {

public enum ScrollingDirection {
ANY, UP, DOWN
}

private static final int DEFAULT_TEXT_SIZE = 12;
private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
private static final int DEFAULT_ANIMATION_DURATION = 350;
Expand Down Expand Up @@ -179,6 +184,23 @@ protected void init(Context context, AttributeSet attrs, int defStyleAttr, int d
}
}

final int defaultPreferredScrollingDirection =
arr.getInt(R.styleable.TickerView_ticker_defaultPreferredScrollingDirection, 0);

switch (defaultPreferredScrollingDirection) {
case 0:
metrics.setPreferredScrollingDirection(ScrollingDirection.ANY);
break;
case 1:
metrics.setPreferredScrollingDirection(ScrollingDirection.UP);
break;
case 2:
metrics.setPreferredScrollingDirection(ScrollingDirection.DOWN);
break;
default:
throw new IllegalArgumentException("Unsupported ticker_defaultPreferredScrollingDirection: " + defaultPreferredScrollingDirection);
}

if (isCharacterListsSet()) {
setText(styledAttributes.text, false);
} else {
Expand Down Expand Up @@ -455,6 +477,19 @@ public void setAnimationInterpolator(Interpolator animationInterpolator) {
this.animationInterpolator = animationInterpolator;
}

/**
* Sets the preferred scrolling direction for ticker animations.
* Eligible params include {@link ScrollingDirection#ANY}, {@link ScrollingDirection#UP}
* and {@link ScrollingDirection#DOWN}.
*
* The default value is {@link ScrollingDirection#ANY}.
*
* @param direction the preferred {@link ScrollingDirection}
*/
public void setPreferredScrollingDirection(ScrollingDirection direction) {
this.metrics.setPreferredScrollingDirection(direction);
}

/**
* @return the current text gravity used to align the text. Should be one of the values defined
* in {@link android.view.Gravity}.
Expand Down
5 changes: 5 additions & 0 deletions ticker/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<enum name="number" value="1" />
<enum name="alphabet" value="2" />
</attr>
<attr name="ticker_defaultPreferredScrollingDirection" format="enum">
<enum name="any" value="0" />
<enum name="up" value="1" />
<enum name="down" value="2" />
</attr>

<!-- Custom implementations of common android text attributes -->
<attr name="android:gravity" tools:ignore="ResourceName" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,55 @@ public void test_initialization() {
@Test
public void test_getCharacterIndices() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '1');
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '1', TickerView.ScrollingDirection.ANY);
assertEquals(1, indices.startIndex);
assertEquals(2, indices.endIndex);
}

@Test
public void test_getCharacterIndicesWraparound() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('2', '0');
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('2', '0', TickerView.ScrollingDirection.ANY);
assertEquals(3, indices.startIndex);
assertEquals(4, indices.endIndex);
}

@Test
public void test_getCharacterIndicesWraparound2() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '2');
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '2', TickerView.ScrollingDirection.ANY);
assertEquals(4, indices.startIndex);
assertEquals(3, indices.endIndex);
}

@Test
public void test_getCharacterIndicesForcedDown() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('2', '0', TickerView.ScrollingDirection.DOWN);
assertEquals(3, indices.startIndex);
assertEquals(4, indices.endIndex);
}

@Test
public void test_getCharacterIndicesForcedDown2() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '2', TickerView.ScrollingDirection.DOWN);
assertEquals(1, indices.startIndex);
assertEquals(3, indices.endIndex);
}

@Test
public void test_getCharacterIndicesForcedUp() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('2', '0', TickerView.ScrollingDirection.UP);
assertEquals(3, indices.startIndex);
assertEquals(1, indices.endIndex);
}

@Test
public void test_getCharacterIndicesForcedUp2() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices = list.getCharacterIndices('0', '2', TickerView.ScrollingDirection.UP);
assertEquals(4, indices.startIndex);
assertEquals(3, indices.endIndex);
}
Expand All @@ -42,8 +74,26 @@ public void test_getCharacterIndicesWraparound2() {
public void test_getCharacterIndicesEmptyNoWraparound() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices =
list.getCharacterIndices('2', TickerUtils.EMPTY_CHAR);
list.getCharacterIndices('2', TickerUtils.EMPTY_CHAR, TickerView.ScrollingDirection.ANY);
assertEquals(3, indices.startIndex);
assertEquals(0, indices.endIndex);
}

@Test
public void test_getCharacterIndicesEmptyForcedUp() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices =
list.getCharacterIndices('2', TickerUtils.EMPTY_CHAR, TickerView.ScrollingDirection.UP);
assertEquals(3, indices.startIndex);
assertEquals(0, indices.endIndex);
}

@Test
public void test_getCharacterIndicesEmptyForcedDown() {
final TickerCharacterList list = new TickerCharacterList("012");
final TickerCharacterList.CharacterIndices indices =
list.getCharacterIndices('2', TickerUtils.EMPTY_CHAR, TickerView.ScrollingDirection.DOWN);
assertEquals(3, indices.startIndex);
assertEquals(7, indices.endIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void setup() {

when(metrics.getCharWidth(anyChar())).thenReturn(5f);
when(metrics.getCharWidth(TickerUtils.EMPTY_CHAR)).thenReturn(0f);
when(metrics.getPreferredScrollingDirection()).thenReturn(TickerView.ScrollingDirection.ANY);

tickerColumnManager = new TickerColumnManager(metrics);
tickerColumnManager.setCharacterLists("1234567890");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void setup() {
when(metrics.getCharHeight()).thenReturn(CHAR_HEIGHT);
when(metrics.getCharWidth(anyChar())).thenReturn(DEFAULT_CHAR_WIDTH);
when(metrics.getCharWidth(TickerUtils.EMPTY_CHAR)).thenReturn(0f);
when(metrics.getPreferredScrollingDirection()).thenReturn(TickerView.ScrollingDirection.ANY);

tickerColumn = new TickerColumn(
new TickerCharacterList[] { characterList },
metrics
Expand Down

0 comments on commit c5f719f

Please sign in to comment.