Skip to content

Commit

Permalink
fixed code according to ReentranceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuinden committed Jan 16, 2017
1 parent 9fdbf8b commit b65d8f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Change log

-Simple Stack 0.2.1 (2017-01-??)
-Simple Stack 0.2.1 (2017-01-17)
---------------------------------
- Added `ReentranceTest` and ported to `simple-stack-demo` codebase
- Fixed some bugs based on `ReentranceTest`
- Fixed some bugs based on `ReentranceTest` - all tests pass now

-Simple Stack 0.2.0 (2017-01-16)
---------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,23 @@ public void setStateChanger(StateChanger stateChanger, @StateChangerRegisterMode
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;
}
}
ArrayList<Parcelable> newHistory = new ArrayList<>();
newHistory.addAll(stack.isEmpty() ? initialParameters : stack);
stack = initialParameters;
enqueueStateChange(newHistory, StateChange.Direction.REPLACE, true);
if(!didInitialize) {
ArrayList<Parcelable> newHistory = new ArrayList<>();
newHistory.addAll(stack.isEmpty() ? initialParameters : stack);
stack = initialParameters;
enqueueStateChange(newHistory, StateChange.Direction.REPLACE, true);
}
}
} else {
beginStateChangeIfPossible();
}
}

Expand All @@ -87,7 +93,7 @@ public void goTo(Parcelable newKey) {

ArrayList<Parcelable> newHistory = new ArrayList<>();
boolean isNewKey = true;
for(Parcelable key : stack) {
for(Parcelable key : selectActiveHistory()) {
newHistory.add(key);
if(key.equals(newKey)) {
isNewKey = false;
Expand Down Expand Up @@ -115,8 +121,10 @@ public boolean goBack() {
return false;
}
ArrayList<Parcelable> newHistory = new ArrayList<>();
for(int i = 0; i < stack.size() - 1; i++) {
newHistory.add(stack.get(i));

List<Parcelable> activeHistory = selectActiveHistory();
for(int i = 0; i < activeHistory.size() - 1; i++) {
newHistory.add(activeHistory.get(i));
}
enqueueStateChange(newHistory, StateChange.Direction.BACKWARD, false);
return true;
Expand All @@ -127,7 +135,6 @@ public void setHistory(List<Parcelable> newHistory, StateChange.Direction direct
enqueueStateChange(newHistory, direction, false);
}


public List<Parcelable> getHistory() {
List<Parcelable> copy = new ArrayList<>();
copy.addAll(stack);
Expand All @@ -140,6 +147,16 @@ private void enqueueStateChange(List<Parcelable> newHistory, StateChange.Directi
beginStateChangeIfPossible();
}

private List<Parcelable> selectActiveHistory() {
if(stack.isEmpty() && queuedStateChanges.size() <= 0) {
return initialParameters;
} else if(queuedStateChanges.size() <= 0) {
return stack;
} else {
return queuedStateChanges.getLast().newHistory;
}
}

private void beginStateChangeIfPossible() {
if(hasStateChanger() && !queuedStateChanges.isEmpty()) {
PendingStateChange pendingStateChange = queuedStateChanges.get(0);
Expand Down Expand Up @@ -177,17 +194,15 @@ public void stateChangeComplete() {
}

private void completeStateChange(StateChange stateChange) {
if(!stateChange.previousState.isEmpty() || (stateChange.previousState.isEmpty() && initialParameters == stack)) {
if(initialParameters == stack) {
stack = originalStack;
}
stack.clear();
stack.addAll(stateChange.newState);
if(initialParameters == stack) {
stack = originalStack;
}
stack.clear();
stack.addAll(stateChange.newState);

PendingStateChange pendingStateChange = queuedStateChanges.remove(0);
if(pendingStateChange.getStatus() != PendingStateChange.Status.IN_PROGRESS) {
throw new IllegalStateException("An error occurred in backstack state management: " + //
throw new IllegalStateException("An error occurred in state management: " + //
"expected [" + PendingStateChange.Status.IN_PROGRESS + "] but was [" + pendingStateChange.getStatus() + "]");
}
pendingStateChange.setStatus(PendingStateChange.Status.COMPLETED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void handleStateChange(@NonNull StateChange traversal, @NonNull StateChan
}

@Test
public void handleStateChangeerSetInMidFlightWaitsForBootstrap() {
public void handleStateChangerSetInMidFlightWaitsForBootstrap() {
flow = new Backstack(ListBuilder.single(new Catalog()));
flow.setStateChanger(new StateChanger() {
@Override
Expand Down

0 comments on commit b65d8f6

Please sign in to comment.