use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class ScopingTest method scopeServicesArePersistedToStateBundleDelayedScopedServicesCall.
@Test
public void scopeServicesArePersistedToStateBundleDelayedScopedServicesCall() {
final Backstack backstack = new Backstack();
final Service service = new Service();
TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setup(History.of(testKeyWithScope));
// !
backstack.setScopedServices(new ServiceProvider());
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.hasService(testKeyWithScope.getScopeTag(), SERVICE_TAG)).isTrue();
StateBundle stateBundle = backstack.toBundle();
// noinspection ConstantConditions
// backstack.getScopesTag() is internal
assertThat(stateBundle.getBundle(Backstack.getScopesTag()).getBundle(testKeyWithScope.getScopeTag()).getBundle(SERVICE_TAG).getInt("blah")).isEqualTo(5);
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class Backstack method persistViewToState.
// ----- viewstate persistence
/**
* Provides the means to save the provided view's hierarchy state
* and its optional StateBundle via {@link Bundleable} into a {@link SavedState}.
*
* @param view the view that belongs to a certain key
*/
public void persistViewToState(@Nullable View view) {
assertCorrectThread();
if (view != null) {
Object key = KeyContextWrapper.getKey(view.getContext());
if (key == null) {
throw new IllegalArgumentException("The view [" + view + "] contained no key in its context hierarchy. The view or its parent hierarchy should be inflated by a layout inflater from `stateChange.createContext(baseContext, key)`, or a KeyContextWrapper.");
}
SparseArray<Parcelable> viewHierarchyState = new SparseArray<>();
view.saveHierarchyState(viewHierarchyState);
StateBundle bundle = null;
if (view instanceof Bundleable) {
bundle = ((Bundleable) view).toBundle();
}
SavedState previousSavedState = getSavedState(key);
previousSavedState.setViewHierarchyState(viewHierarchyState);
previousSavedState.setViewBundle(bundle);
}
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class Backstack method toBundle.
/**
* Persists the backstack history and view state into a StateBundle.
*
* @return the state bundle
*/
@Nonnull
@Override
public StateBundle toBundle() {
assertCorrectThread();
StateBundle stateBundle = new StateBundle();
ArrayList<Parcelable> history = new ArrayList<>();
for (Object key : getHistory()) {
history.add(keyParceler.toParcelable(key));
}
stateBundle.putParcelableArrayList(getHistoryTag(), history);
ArrayList<ParcelledState> parcelledStates = new ArrayList<>();
for (SavedState savedState : keyStateMap.values()) {
ParcelledState parcelledState = new ParcelledState();
parcelledState.parcelableKey = keyParceler.toParcelable(savedState.getKey());
parcelledState.viewHierarchyState = savedState.getViewHierarchyState();
parcelledState.bundle = savedState.getBundle();
parcelledState.viewBundle = savedState.getViewBundle();
parcelledStates.add(parcelledState);
}
stateBundle.putParcelableArrayList(getStatesTag(), parcelledStates);
stateBundle.putParcelable(getScopesTag(), scopeManager.saveStates());
StateBundle retainedObjectStates = new StateBundle();
for (Map.Entry<String, Object> entry : retainedObjects.entrySet()) {
final String objectTag = entry.getKey();
final Object retainedObject = entry.getValue();
if (retainedObject instanceof Bundleable) {
StateBundle retainedBundle = ((Bundleable) retainedObject).toBundle();
retainedObjectStates.putParcelable(objectTag, retainedBundle);
}
}
stateBundle.putParcelable(getRetainedObjectStatesTag(), retainedObjectStates);
return stateBundle;
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class WordController method toBundle.
@Nonnull
@Override
public StateBundle toBundle() {
StateBundle stateBundle = new StateBundle();
// noinspection ConstantConditions
stateBundle.putStringArrayList("words", new ArrayList<String>(words.getValue()));
return stateBundle;
}
Aggregations