Skip to content

Commit

Permalink
Add startWith and startAt to TapTargetSequence (KeepSafe#228)
Browse files Browse the repository at this point in the history
Previously it was cumbersome to restore state in a TapTargetSequence
after rotation due to the fact that you needed to manually modify the
list of targets you pass in to get back to your desired target. Now the
class supports starting from a specified target id or position in the
queue, allowing callers to keep a static list of targets for their
sequence.
  • Loading branch information
xiphirx authored Nov 19, 2017
1 parent ddf5ea0 commit 6898f18
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,46 @@ public void start() {
showNext();
}

/** Immediately starts the sequence from the given targetId's position in the queue */
public void startWith(int targetId) {
if (active) {
return;
}

while (targets.peek() != null && targets.peek().id() != targetId) {
targets.poll();
}

TapTarget peekedTarget = targets.peek();
if (peekedTarget == null || peekedTarget.id() != targetId) {
throw new IllegalStateException("Given target " + targetId + " not in sequence");
}

start();
}

/** Immediately starts the sequence at the specified zero-based index in the queue */
public void startAt(int index) {
if (active) {
return;
}

if (index < 0 || index >= targets.size()) {
throw new IllegalArgumentException("Given invalid index " + index);
}

final int expectedSize = targets.size() - index;
while (targets.peek() != null && targets.size() != expectedSize) {
targets.poll();
}

if (targets.size() != expectedSize) {
throw new IllegalStateException("Given index " + index + " not in sequence");
}

start();
}

/**
* Cancels the sequence, if the current target is cancelable.
* When the sequence is canceled, the current target is dismissed and the remaining targets are
Expand Down

0 comments on commit 6898f18

Please sign in to comment.