Search in sources :

Example 1 with ServiceTree

use of com.zhuinden.servicetree.ServiceTree in project simple-stack by Zhuinden.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    NonConfigurationInstance nonConfigurationInstance = (NonConfigurationInstance) getLastCustomNonConfigurationInstance();
    if (nonConfigurationInstance != null) {
        serviceManager = nonConfigurationInstance.serviceManager;
        serviceTree = serviceManager.getServiceTree();
    } else {
        serviceTree = new ServiceTree();
        serviceTree.createRootNode(TAG);
        serviceManager = new NestSupportServiceManager(serviceTree, TAG);
        serviceManager.setRestoredStates(savedInstanceState != null ? savedInstanceState.getParcelable("SERVICE_BUNDLE") : new StateBundle());
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    backstack = Navigator.configure().setStateChanger(DefaultStateChanger.configure().setExternalStateChanger(this).create(this, root)).setStateClearStrategy(new PreserveTreeScopesStrategy(serviceTree)).install(this, root, HistoryBuilder.single(MainKey.create()));
}
Also used : PreserveTreeScopesStrategy(com.zhuinden.simplestackdemonestedstack.util.PreserveTreeScopesStrategy) NestSupportServiceManager(com.zhuinden.simplestackdemonestedstack.util.NestSupportServiceManager) ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 2 with ServiceTree

use of com.zhuinden.servicetree.ServiceTree in project simple-stack by Zhuinden.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    NonConfigurationInstance nonConfigurationInstance = (NonConfigurationInstance) getLastCustomNonConfigurationInstance();
    if (nonConfigurationInstance != null) {
        serviceManager = nonConfigurationInstance.serviceManager;
        serviceTree = serviceManager.getServiceTree();
    } else {
        serviceTree = new ServiceTree();
        serviceTree.createRootNode(TAG);
        serviceManager = new ServiceManager(serviceTree, TAG);
    }
    serviceManager.setRestoredStates(savedInstanceState != null ? savedInstanceState.getParcelable(ServiceManager.SERVICE_STATES) : new StateBundle());
    Navigator.configure().setStateChanger(DefaultStateChanger.configure().setExternalStateChanger(this).create(this, root)).install(this, root, HistoryBuilder.single(A.create()));
}
Also used : ServiceManager(com.zhuinden.simpleservicesexample.utils.ServiceManager) ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 3 with ServiceTree

use of com.zhuinden.servicetree.ServiceTree in project simple-stack by Zhuinden.

the class ServiceLocator method getService.

public static <T> T getService(Context context, String name) {
    T t = (T) context.getSystemService(name);
    if (t == null) {
        // noinspection ResourceType
        ServiceTree serviceTree = (ServiceTree) context.getSystemService(SERVICE_TREE);
        ServiceTree.Node node = serviceTree.getNode(Backstack.getKey(context));
        return node.getService(name);
    }
    return t;
}
Also used : ServiceTree(com.zhuinden.servicetree.ServiceTree)

Example 4 with ServiceTree

use of com.zhuinden.servicetree.ServiceTree in project simple-stack by Zhuinden.

the class NestSupportServiceManager method handleBack.

public boolean handleBack(Context context) {
    ServiceTree serviceTree = ServiceLocator.getService(context, ServiceLocator.SERVICE_TREE);
    Object lastKey = activeKeys.get(activeKeys.size() - 1);
    Backstack backstack = Navigator.getBackstack(context);
    class Cancellation {

        private boolean cancelled;
    }
    Cancellation cancellation = new Cancellation();
    serviceTree.traverseChain(serviceTree.getNode(lastKey), (node, cancellationToken) -> {
        Object _key = node.getKey();
        if (_key instanceof Key) {
            // ROOT is defined by Activity's TAG
            Key key = (Key) _key;
            if (key.hasNestedStack()) {
                BackstackManager backstackManager = serviceTree.getNode(key).getService(Key.NESTED_STACK);
                if (backstackManager.getBackstack().goBack()) {
                    cancellation.cancelled = true;
                    cancellationToken.cancel();
                }
            }
        }
    });
    if (cancellation.cancelled) {
        return true;
    }
    return backstack.goBack();
}
Also used : Backstack(com.zhuinden.simplestack.Backstack) BackstackManager(com.zhuinden.simplestack.BackstackManager) ServiceTree(com.zhuinden.servicetree.ServiceTree) Key(com.zhuinden.simplestackdemonestedstack.application.Key)

Example 5 with ServiceTree

use of com.zhuinden.servicetree.ServiceTree in project simple-stack by Zhuinden.

the class MortarDemoApplication method getSystemService.

@Override
public Object getSystemService(String name) {
    if (serviceTree == null) {
        serviceTree = new ServiceTree();
        serviceTree.registerRootService(DaggerService.SERVICE_NAME, DaggerSingletonComponent.builder().rootModule(new RootModule(serviceTree)).build());
        serviceTree.registerRootService(NodeStateManager.SERVICE_STATES, new StateBundle());
    // MortarScope.buildRootScope()
    //.withService(ObjectGraphService.SERVICE_NAME, ObjectGraph.create(new RootModule()))
    //.build("Root");
    }
    if (serviceTree.hasRootService(name)) {
        return serviceTree.getRootService(name);
    }
    if (TreeNodes.NODE_TAG.equals(name)) {
        return serviceTree.getTreeRoot();
    }
    return super.getSystemService(name);
}
Also used : ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle)

Aggregations

ServiceTree (com.zhuinden.servicetree.ServiceTree)7 StateBundle (com.zhuinden.statebundle.StateBundle)4 NodeClearManager (com.example.mortar.nodes.NodeClearManager)1 NodeCreationManager (com.example.mortar.nodes.NodeCreationManager)1 NodeStateManager (com.example.mortar.nodes.NodeStateManager)1 ServiceManager (com.zhuinden.simpleservicesexample.utils.ServiceManager)1 Backstack (com.zhuinden.simplestack.Backstack)1 BackstackManager (com.zhuinden.simplestack.BackstackManager)1 Key (com.zhuinden.simplestackdemonestedstack.application.Key)1 NestSupportServiceManager (com.zhuinden.simplestackdemonestedstack.util.NestSupportServiceManager)1 PreserveTreeScopesStrategy (com.zhuinden.simplestackdemonestedstack.util.PreserveTreeScopesStrategy)1