Search in sources :

Example 26 with StateBundle

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

the class MortarDemoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        // no need to restore if no process death could have occurred
        DID_RESTORE_AFTER_PROCESS_DEATH = true;
    }
    if (savedInstanceState != null && !DID_RESTORE_AFTER_PROCESS_DEATH) {
        DID_RESTORE_AFTER_PROCESS_DEATH = true;
        StateBundle rootBundle = savedInstanceState.getParcelable(NodeStateManager.SERVICE_STATES);
        if (rootBundle != null) {
            // global service state is restored after process death
            // not yet injected by field injection
            SingletonComponent singletonComponent = DaggerService.get(this);
            ServiceTree _serviceTree = singletonComponent.serviceTree();
            NodeStateManager _nodeStateManager = singletonComponent.nodeStateManager();
            _serviceTree.registerRootService(NodeStateManager.SERVICE_STATES, rootBundle);
            // root services should be restored after process death
            _nodeStateManager.restoreStatesForNode(_serviceTree.getTreeRoot());
        }
    }
    ServiceTree.Node parentScope = TreeNodes.getNode(getApplication());
    String scopeName = getLocalClassName() + "-task-" + getTaskId();
    if (!parentScope.hasChild(scopeName)) {
        parentScope.createChild(scopeName);
    }
    activityScope = parentScope.getChild(scopeName);
    DaggerService.<SingletonComponent>get(this).inject(this);
    actionBarOwner.takeView(this);
    setContentView(R.layout.root_layout);
    container = (FrameLayout) findViewById(R.id.container);
    Navigator.configure().setKeyParceler(keyParceler).addStateChangeCompletionListener(new NodeClearManager(serviceTree, // to delete un-used mortar scopes
    nodeStateManager)).setStateChanger(DefaultStateChanger.configure().setExternalStateChanger(this).setContextCreationStrategy(new NodeCreationManager(serviceTree, activityScope, nodeStateManager)).create(this, container)).install(this, container, HistoryBuilder.single(ChatListScreen.create()));
}
Also used : NodeClearManager(com.example.mortar.nodes.NodeClearManager) ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle) NodeStateManager(com.example.mortar.nodes.NodeStateManager) NodeCreationManager(com.example.mortar.nodes.NodeCreationManager)

Example 27 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 28 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 29 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 30 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)

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