Search in sources :

Example 41 with StateBundle

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);
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 42 with StateBundle

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);
    }
}
Also used : SparseArray(android.util.SparseArray) Parcelable(android.os.Parcelable) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 43 with StateBundle

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;
}
Also used : ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StateBundle(com.zhuinden.statebundle.StateBundle) Nonnull(javax.annotation.Nonnull)

Example 44 with 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;
}
Also used : StateBundle(com.zhuinden.statebundle.StateBundle) Nonnull(javax.annotation.Nonnull)

Aggregations

StateBundle (com.zhuinden.statebundle.StateBundle)44 Test (org.junit.Test)17 Parcelable (android.os.Parcelable)12 TestKey (com.zhuinden.simplestack.helpers.TestKey)11 ArrayList (java.util.ArrayList)10 ServiceTree (com.zhuinden.servicetree.ServiceTree)8 Nonnull (javax.annotation.Nonnull)6 NonNull (android.support.annotation.NonNull)5 Bundleable (com.zhuinden.simplestack.Bundleable)4 ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Nullable (javax.annotation.Nullable)4 SparseArray (android.util.SparseArray)2 AbstractMap (java.util.AbstractMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 List (java.util.List)2 Context (android.content.Context)1 View (android.view.View)1