Skip to content

Commit

Permalink
prettify setStateChanger method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuinden committed Jan 17, 2017
1 parent 86ba63a commit a98687b
Showing 1 changed file with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,16 @@ public void setStateChanger(StateChanger stateChanger, @StateChangerRegisterMode
throw new NullPointerException("New state changer cannot be null");
}
this.stateChanger = stateChanger;
if(registerMode == INITIALIZE) {
if(queuedStateChanges.size() <= 1 || stack.isEmpty()) {
boolean didInitialize = false;
if(!queuedStateChanges.isEmpty()) {
PendingStateChange pendingStateChange = queuedStateChanges.get(0);
if(pendingStateChange.getStatus() == PendingStateChange.Status.ENQUEUED) {
beginStateChangeIfPossible();
didInitialize = true;
}
}
if(!didInitialize) {
ArrayList<Parcelable> newHistory = new ArrayList<>();
newHistory.addAll(stack.isEmpty() ? initialParameters : stack);
stack = initialParameters;
enqueueStateChange(newHistory, StateChange.Direction.REPLACE, true);
}
} else {
beginStateChangeIfPossible();
if(registerMode == INITIALIZE && (queuedStateChanges.size() <= 1 || stack.isEmpty())) {
if(!beginStateChangeIfPossible()) {
ArrayList<Parcelable> newHistory = new ArrayList<>();
newHistory.addAll(stack.isEmpty() ? initialParameters : stack);
stack = initialParameters;
enqueueStateChange(newHistory, StateChange.Direction.REPLACE, true);
}
} else {
beginStateChangeIfPossible();
return;
}
beginStateChangeIfPossible();
}

public void removeStateChanger() {
Expand Down Expand Up @@ -159,14 +147,16 @@ private List<Parcelable> selectActiveHistory() {
}
}

private void beginStateChangeIfPossible() {
private boolean beginStateChangeIfPossible() {
if(hasStateChanger() && !queuedStateChanges.isEmpty()) {
PendingStateChange pendingStateChange = queuedStateChanges.get(0);
if(pendingStateChange.getStatus() == PendingStateChange.Status.ENQUEUED) {
pendingStateChange.setStatus(PendingStateChange.Status.IN_PROGRESS);
changeState(pendingStateChange);
return true;
}
}
return false;
}

private void changeState(final PendingStateChange pendingStateChange) {
Expand Down

0 comments on commit a98687b

Please sign in to comment.