Search in sources :

Example 16 with StateBundle

use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.

the class CloudSyncView method toBundle.

@Override
public StateBundle toBundle() {
    StateBundle bundle = new StateBundle();
    bundle.putString("HELLO", "WORLD");
    StateBundle innerBundle = new StateBundle();
    innerBundle.putString("KAPPA", "KAPPA");
    bundle.putBundle("GOOMBA", innerBundle);
    return bundle;
}
Also used : StateBundle(com.zhuinden.statebundle.StateBundle)

Example 17 with StateBundle

use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.

the class NestSupportServiceManager method setupServices.

public void setupServices(StateChange stateChange, boolean isFromCompositeKey) {
    StateBundle states = serviceTree.getNode(rootKey).getService(SERVICE_STATES);
    for (Object _previousKey : stateChange.getPreviousState()) {
        Key previousKey = (Key) _previousKey;
        if (!stateChange.getNewState().contains(previousKey)) {
            activeKeys.remove(previousKey);
            if (!isFromCompositeKey) {
                ServiceTree.Node previousNode = serviceTree.getNode(previousKey);
                if (states != null) {
                    serviceTree.traverseSubtree(previousNode, ServiceTree.Walk.POST_ORDER, (node, cancellationToken) -> {
                        states.remove(node.getKey().toString());
                    });
                }
                serviceTree.removeNodeAndChildren(previousNode);
            }
        }
    }
    for (Object _newKey : stateChange.getNewState()) {
        Key newKey = (Key) _newKey;
        activeKeys.remove(newKey);
        if (newKey == stateChange.topNewState()) {
            activeKeys.add(newKey);
        }
        if (!isFromCompositeKey) {
            buildServices(states, newKey);
        }
    }
}
Also used : ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle) Key(com.zhuinden.simplestackdemonestedstack.application.Key)

Example 18 with StateBundle

use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.

the class NodeStateManager method clearStatesForKey.

public void clearStatesForKey(Object previousKey) {
    final StateBundle rootBundle = serviceTree.getRootService(SERVICE_STATES);
    serviceTree.traverseSubtree(serviceTree.getNode(previousKey), ServiceTree.Walk.POST_ORDER, new ServiceTree.Walk() {

        @Override
        public void execute(@NonNull ServiceTree.Node node, @NonNull CancellationToken cancellationToken) {
            Log.i(TAG, "Removing state for [" + node.getKey().toString() + "]");
            rootBundle.remove(node.getKey().toString());
        }
    });
}
Also used : ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 19 with StateBundle

use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.

the class BackstackManager method toBundle.

/**
     * Persists the backstack history and view state into a StateBundle.
     *
     * @return the state bundle
     */
@NonNull
@Override
public StateBundle toBundle() {
    StateBundle stateBundle = new StateBundle();
    ArrayList<Parcelable> history = new ArrayList<>();
    for (Object key : backstack.getHistory()) {
        history.add(keyParceler.toParcelable(key));
    }
    stateBundle.putParcelableArrayList(getHistoryTag(), history);
    ArrayList<ParcelledState> states = 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();
        states.add(parcelledState);
    }
    stateBundle.putParcelableArrayList(getStatesTag(), states);
    return stateBundle;
}
Also used : ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) StateBundle(com.zhuinden.statebundle.StateBundle) NonNull(android.support.annotation.NonNull)

Example 20 with StateBundle

use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.

the class BackstackDelegateTest method testRestoreViewFromState.

@Test
public void testRestoreViewFromState() {
    BackstackDelegate backstackDelegate = new BackstackDelegate(null);
    TestKey key = new TestKey("hello");
    backstackDelegate.onCreate(null, null, HistoryBuilder.single(key));
    backstackDelegate.setStateChanger(stateChanger);
    Mockito.when(view.getContext()).thenReturn(context);
    StateBundle stateBundle = new StateBundle();
    Mockito.when(((Bundleable) view).toBundle()).thenReturn(stateBundle);
    // noinspection ResourceType
    Mockito.when(context.getSystemService(KeyContextWrapper.TAG)).thenReturn(key);
    backstackDelegate.persistViewToState(view);
    backstackDelegate.restoreViewFromState(view);
    ((Bundleable) Mockito.verify(view, Mockito.times(1))).fromBundle(stateBundle);
}
Also used : StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Aggregations

StateBundle (com.zhuinden.statebundle.StateBundle)25 ServiceTree (com.zhuinden.servicetree.ServiceTree)8 Parcelable (android.os.Parcelable)5 NonNull (android.support.annotation.NonNull)5 Bundleable (com.zhuinden.simplestack.Bundleable)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 SparseArray (android.util.SparseArray)1 NodeClearManager (com.example.mortar.nodes.NodeClearManager)1 NodeCreationManager (com.example.mortar.nodes.NodeCreationManager)1 NodeStateManager (com.example.mortar.nodes.NodeStateManager)1 Key (com.zhuinden.simpleservicesexample.application.Key)1 ServiceManager (com.zhuinden.simpleservicesexample.utils.ServiceManager)1 Key (com.zhuinden.simplestackdemonestedstack.application.Key)1 NestSupportServiceManager (com.zhuinden.simplestackdemonestedstack.util.NestSupportServiceManager)1 PreserveTreeScopesStrategy (com.zhuinden.simplestackdemonestedstack.util.PreserveTreeScopesStrategy)1 List (java.util.List)1