Skip to content

Commit

Permalink
1.11.6: Ensure reverse order of onExitScope() for service bind order
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuinden committed Aug 14, 2018
1 parent be16e96 commit ddd0723
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Change log

-Simple Stack 1.11.5 (2018-08-14)
-Simple Stack 1.11.6 (2018-08-14)
--------------------------------
- ADDED: `Navigator.hasScope(scopeTag)`, `BackstackDelegate.hasScope(scopeTag)`, `BackstackManager.hasScope(scopeTag)`.

- ADDED: `Navigator.canFindService(Context, serviceTag)`, `BackstackDelegate.canFindService(serviceTag)`, `BackstackManager.canFindService(serviceTag)` to check if `lookup` can find the service.

- ADDED: `ServiceBinder.lookup()` and `ServiceBinder.canFind()` to inherit from currently existing scopes while creating service binding.

- CHANGE: `onExitScope(scopeTag)` is now ensured to happen in reverse order compared to `onEnterScope(scopeTag)`.
- CHANGE: `onExitScope(scopeTag)` is now ensured to happen in reverse order compared to `onEnterScope(scopeTag)` (both in terms of scope creation order and service binding order).

-Simple Stack 1.11.4 (2018-08-10)
--------------------------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ In order to use Simple Stack, you need to add jitpack to your project root gradl

and add the compile dependency to your module level gradle.

compile 'com.github.Zhuinden:simple-stack:1.11.5'
compile 'com.github.Zhuinden:simple-stack:1.11.6'

## How does it work?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public void finalizeScopes() {
scopeSet.add(((ScopeKey) key).getScopeTag());
}
}
for(String scope : scopeSet) {
List<String> scopes = new ArrayList<>(scopeSet);
Collections.reverse(scopes);
for(String scope : scopes) {
scopeManager.destroyScope(scope);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ void clearScopesNotIn(List<Object> newState) {
for(String activeScope: scopeSet) {
if(!currentScopes.contains(activeScope)) {
Map<String, Object> scope = scopes.get(activeScope);
for(Object service : scope.values()) {
List<Object> services = new ArrayList<>(scope.values());
Collections.reverse(services);
for(Object service : services) {
if(service instanceof ScopedServices.Scoped) {
((ScopedServices.Scoped) service).onExitScope(activeScope);
}
Expand All @@ -102,8 +104,10 @@ void clearScopesNotIn(List<Object> newState) {

void destroyScope(String scopeTag) {
if(scopes.containsKey(scopeTag)) {
Map<String, Object> services = scopes.remove(scopeTag);
for(Object service : services.values()) {
Map<String, Object> serviceMap = scopes.remove(scopeTag);
List<Object> services = new ArrayList<>(serviceMap.values());
Collections.reverse(services);
for(Object service : services) {
if(service instanceof ScopedServices.Scoped) {
((ScopedServices.Scoped) service).onExitScope(scopeTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,62 @@ public String getScopeTag() {

assertThat(enteredScope).containsExactly(service1, service2);
assertThat(exitedScope).containsExactly(service2, service1);
}

@Test
public void serviceCreationAndDestructionHappensInForwardAndReverseOrder() {
BackstackManager backstackManager = new BackstackManager();
backstackManager.setScopedServices(new ServiceProvider());

final List<Object> enteredScope = new ArrayList<>();
final List<Object> exitedScope = new ArrayList<>();

class MyService implements ScopedServices.Scoped {
@Override
public void onEnterScope(@NonNull String scope) {
enteredScope.add(this);
}

@Override
public void onExitScope(@NonNull String scope) {
exitedScope.add(this);
}
}

final MyService service1 = new MyService();
final MyService service2 = new MyService();


TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ScopedServices.ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());

serviceBinder.add("SERVICE1", service1);
serviceBinder.add("SERVICE2", service2);
}

@NonNull
@Override
public String getScopeTag() {
return "beep";
}
};

TestKey bye = new TestKey("bye");

backstackManager.setup(History.of(beep));

assertThat(enteredScope).isEmpty();
assertThat(exitedScope).isEmpty();
backstackManager.setStateChanger(stateChanger);

assertThat(enteredScope).containsExactly(service1, service2);
assertThat(exitedScope).isEmpty();

backstackManager.getBackstack().setHistory(History.of(bye), StateChange.REPLACE);

assertThat(enteredScope).containsExactly(service1, service2);
assertThat(exitedScope).containsExactly(service2, service1);
}
}

0 comments on commit ddd0723

Please sign in to comment.