Search in sources :

Example 11 with StateBundle

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

the class BackstackManagerTest method afterClearAndRestorationTheFilteredAreNotRestored.

@Test
public void afterClearAndRestorationTheFilteredAreNotRestored() {
    final TestKey initial = new TestKey("initial");
    final TestKey restored = new TestKey("restored");
    final TestKey filtered = new TestKey("filtered");
    ArrayList<Parcelable> history = new ArrayList<>();
    history.add(restored);
    history.add(filtered);
    StateBundle stateBundle = new StateBundle();
    stateBundle.putParcelableArrayList(BackstackManager.getHistoryTag(), history);
    BackstackManager backstackManager = new BackstackManager();
    backstackManager.setKeyFilter(new KeyFilter() {

        @NonNull
        @Override
        public List<Object> filterHistory(@NonNull List<Object> restoredKeys) {
            restoredKeys.remove(filtered);
            return restoredKeys;
        }
    });
    backstackManager.setup(HistoryBuilder.single(initial));
    backstackManager.fromBundle(stateBundle);
    backstackManager.setStateChanger(stateChanger);
    Backstack backstack = backstackManager.getBackstack();
    assertThat(backstack.getHistory()).contains(restored);
    assertThat(backstack.getHistory()).doesNotContain(filtered);
    //// would restore properly
    backstackManager = new BackstackManager();
    backstackManager.setup(HistoryBuilder.single(initial));
    backstackManager.fromBundle(stateBundle);
    backstackManager.setStateChanger(stateChanger);
    backstack = backstackManager.getBackstack();
    assertThat(backstack.getHistory()).contains(restored, filtered);
    //// if both are filtered, restore initial
    backstackManager = new BackstackManager();
    backstackManager.setKeyFilter(new KeyFilter() {

        @NonNull
        @Override
        public List<Object> filterHistory(@NonNull List<Object> restoredKeys) {
            restoredKeys.remove(restored);
            restoredKeys.remove(filtered);
            return restoredKeys;
        }
    });
    backstackManager.setup(HistoryBuilder.single(initial));
    backstackManager.fromBundle(stateBundle);
    backstackManager.setStateChanger(stateChanger);
    backstack = backstackManager.getBackstack();
    assertThat(backstack.getHistory()).doesNotContain(restored, filtered);
    assertThat(backstack.getHistory()).contains(initial);
}
Also used : ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) StateBundle(com.zhuinden.statebundle.StateBundle) NonNull(android.support.annotation.NonNull) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with StateBundle

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

the class NodeStateManager method persistStates.

public StateBundle persistStates() {
    final StateBundle rootBundle = serviceTree.getRootService(SERVICE_STATES);
    serviceTree.traverseTree(ServiceTree.Walk.PRE_ORDER, true, new ServiceTree.Walk() {

        @Override
        public void execute(@NonNull ServiceTree.Node node, @NonNull CancellationToken cancellationToken) {
            StateBundle localBundle = rootBundle.getBundle(node.getKey().toString());
            if (localBundle == null) {
                localBundle = new StateBundle();
            }
            for (ServiceTree.Node.Entry entry : node.getBoundServices()) {
                Log.i(TAG, "Persisting state for [" + entry.getName() + "] in [" + node.getKey().toString() + "]");
                if (entry.getService() instanceof Bundleable) {
                    localBundle.putBundle(entry.getName(), ((Bundleable) entry.getService()).toBundle());
                }
            }
            rootBundle.putBundle(node.getKey().toString(), localBundle);
        }
    });
    return rootBundle;
}
Also used : Bundleable(com.zhuinden.simplestack.Bundleable) ServiceTree(com.zhuinden.servicetree.ServiceTree) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 13 with StateBundle

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

the class AddOrEditTaskPresenter method toBundle.

public StateBundle toBundle() {
    StateBundle bundle = new StateBundle();
    bundle.putString("title", title);
    bundle.putString("description", description);
    return bundle;
}
Also used : StateBundle(com.zhuinden.statebundle.StateBundle)

Example 14 with StateBundle

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

the class TasksPresenter method toBundle.

@Override
@NonNull
public StateBundle toBundle() {
    StateBundle bundle = new StateBundle();
    bundle.putString("FILTERING", filterType.getValue().name());
    return bundle;
}
Also used : StateBundle(com.zhuinden.statebundle.StateBundle) NonNull(android.support.annotation.NonNull)

Example 15 with StateBundle

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

the class ServiceManager method persistStates.

public StateBundle persistStates() {
    StateBundle serviceStates = new StateBundle();
    serviceTree.traverseTree(ServiceTree.Walk.PRE_ORDER, (node, cancellationToken) -> {
        StateBundle keyBundle = new StateBundle();
        for (ServiceTree.Node.Entry entry : node.getBoundServices()) {
            if (entry.getService() instanceof Bundleable) {
                keyBundle.putParcelable(entry.getName(), ((Bundleable) entry.getService()).toBundle());
            }
        }
        serviceStates.putParcelable(node.getKey().toString(), keyBundle);
    });
    return serviceStates;
}
Also used : Bundleable(com.zhuinden.simplestack.Bundleable) StateBundle(com.zhuinden.statebundle.StateBundle)

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