Skip to content

Commit 5d8b822

Browse files
committed
2.6.2: Add Backstack.canSetScopeProviders()`
1 parent b8d9d90 commit 5d8b822

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change log
22

3+
-Simple Stack 2.6.2 (2021-06-07)
4+
--------------------------------
5+
6+
- ADDED: `Backstack.canSetScopeProviders()`.
7+
8+
This is in conjunction with the 2.6.1 change, while making it safe to use them without extra checks such as `if(lastNonConfigurationInstance == null) {`.
9+
310
-Simple Stack 2.6.1 (2021-05-03)
411
--------------------------------
512

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ and then, add the dependency to your module's `build.gradle.kts` (or `build.grad
4949

5050
``` kotlin
5151
// build.gradle.kts
52-
implementation("com.github.Zhuinden:simple-stack:2.6.1")
52+
implementation("com.github.Zhuinden:simple-stack:2.6.2")
5353
implementation("com.github.Zhuinden:simple-stack-extensions:2.2.1")
5454
```
5555

5656
or
5757

5858
``` groovy
5959
// build.gradle
60-
implementation 'com.github.Zhuinden:simple-stack:2.6.1'
60+
implementation 'com.github.Zhuinden:simple-stack:2.6.2'
6161
implementation 'com.github.Zhuinden:simple-stack-extensions:2.2.1'
6262
```
6363

simple-stack/src/main/java/com/zhuinden/simplestack/Backstack.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,26 @@ public void setStateClearStrategy(@Nonnull StateClearStrategy stateClearStrategy
242242
this.stateClearStrategy = stateClearStrategy;
243243
}
244244

245+
/**
246+
* Specifies if setting a scope provider, such as either {@link Backstack#setScopedServices(ScopedServices)} or {@link Backstack#setGlobalServices(GlobalServices)} is allowed.
247+
* <p>
248+
* This can be useful in case of deferred initialization, as scope providers shouldn't be set and aren't allowed once the initial state change happens.
249+
*
250+
* @return true if scope provider can be set
251+
*/
252+
public final boolean canSetScopeProviders() {
253+
return !didRunInitialStateChange;
254+
}
255+
245256
/**
246257
* Specifies a {@link ScopedServices} to allow handling the creation of scoped services.
247258
* <p>
248-
* Must be called before the initial state change.
259+
* Must be called before the initial state change. Call {@link Backstack#canSetScopeProviders()} to see if it's allowed.
249260
*
250261
* @param scopedServices the {@link ScopedServices}.
251262
*/
252263
public void setScopedServices(@Nonnull ScopedServices scopedServices) {
253-
if(didRunInitialStateChange) {
264+
if(!canSetScopeProviders()) {
254265
throw new IllegalStateException("Scope provider should be set before the initial state change!");
255266
}
256267
if(scopedServices == null) {
@@ -262,14 +273,14 @@ public void setScopedServices(@Nonnull ScopedServices scopedServices) {
262273
/**
263274
* Specifies a {@link GlobalServices} that describes the services of the global scope.
264275
*
265-
* Must be called before the initial state change.
276+
* Must be called before the initial state change. Call {@link Backstack#canSetScopeProviders()} to see if it's allowed.
266277
*
267278
* Please note that setting a {@link GlobalServices.Factory} overrides this configuration option.
268279
*
269280
* @param globalServices the {@link GlobalServices}.
270281
*/
271282
public void setGlobalServices(@Nonnull GlobalServices globalServices) {
272-
if(didRunInitialStateChange) {
283+
if(!canSetScopeProviders()) {
273284
throw new IllegalStateException("Scope provider should be set before the initial state change!");
274285
}
275286
if(globalServices == null) {
@@ -281,7 +292,7 @@ public void setGlobalServices(@Nonnull GlobalServices globalServices) {
281292
/**
282293
* Specifies a {@link GlobalServices.Factory} that describes the services of the global scope that are deferred until first creation.
283294
*
284-
* Must be called before the initial state change.
295+
* Must be called before the initial state change. Call {@link Backstack#canSetScopeProviders()} to see if it's allowed.
285296
*
286297
* Please note that a strong reference is kept to the factory, and the {@link Backstack} is typically preserved across configuration change.
287298
* It is recommended that it is NOT an anonymous inner class or normal inner class in an Activity,
@@ -292,7 +303,7 @@ public void setGlobalServices(@Nonnull GlobalServices globalServices) {
292303
* @param globalServiceFactory the {@link GlobalServices.Factory}.
293304
*/
294305
public void setGlobalServices(@Nonnull GlobalServices.Factory globalServiceFactory) {
295-
if(didRunInitialStateChange) {
306+
if(!canSetScopeProviders()) {
296307
throw new IllegalStateException("Scope provider should be set before the initial state change!");
297308
}
298309
if(globalServiceFactory == null) {
@@ -301,7 +312,6 @@ public void setGlobalServices(@Nonnull GlobalServices.Factory globalServiceFacto
301312
this.scopeManager.setGlobalServices(globalServiceFactory);
302313
}
303314

304-
305315
NavigationCore core;
306316

307317
Map<Object, SavedState> keyStateMap = new HashMap<>();

simple-stack/src/test/java/com/zhuinden/simplestack/ScopingTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,37 @@ public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callbac
162162
}
163163
}
164164

165+
@Test
166+
public void serviceProviderCanBeSetWorksCorrectly() {
167+
Backstack backstack = new Backstack();
168+
169+
assertThat(backstack.canSetScopeProviders()).isTrue();
170+
backstack.setup(History.of(testKey1));
171+
172+
assertThat(backstack.canSetScopeProviders()).isTrue();
173+
174+
backstack.setScopedServices(new ServiceProvider());
175+
backstack.setGlobalServices(GlobalServices.builder().build());
176+
177+
assertThat(backstack.canSetScopeProviders()).isTrue();
178+
179+
backstack.setStateChanger(new StateChanger() {
180+
@Override
181+
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
182+
completionCallback.stateChangeComplete();
183+
}
184+
});
185+
186+
assertThat(backstack.canSetScopeProviders()).isFalse();
187+
188+
try {
189+
backstack.setScopedServices(new ServiceProvider());
190+
Assert.fail();
191+
} catch(IllegalStateException e) {
192+
// OK!
193+
}
194+
}
195+
165196
@Test
166197
public void scopedServicesThrowIfNoScopedServicesAreDefinedAndServicesAreToBeBound() {
167198
Backstack backstack = new Backstack();

0 commit comments

Comments
 (0)