|  | @@ -1,28 +1,13 @@
 | 
	
		
			
			| 1 | 1 |  package com.reactnativenavigation.react;
 | 
	
		
			
			| 2 | 2 |  
 | 
	
		
			
			| 3 |  | -import android.os.Bundle;
 | 
	
		
			
			| 4 |  | -import android.view.View;
 | 
	
		
			
			| 5 |  | -
 | 
	
		
			
			| 6 |  | -import com.facebook.react.ReactRootView;
 | 
	
		
			
			| 7 | 3 |  import com.facebook.react.bridge.ReactApplicationContext;
 | 
	
		
			
			| 8 | 4 |  import com.facebook.react.bridge.ReactContextBaseJavaModule;
 | 
	
		
			
			| 9 | 5 |  import com.facebook.react.bridge.ReactMethod;
 | 
	
		
			
			| 10 |  | -import com.facebook.react.bridge.ReadableArray;
 | 
	
		
			
			| 11 | 6 |  import com.facebook.react.bridge.ReadableMap;
 | 
	
		
			
			| 12 |  | -import com.facebook.react.bridge.ReadableMapKeySetIterator;
 | 
	
		
			
			| 13 | 7 |  import com.reactnativenavigation.NavigationActivity;
 | 
	
		
			
			| 14 | 8 |  import com.reactnativenavigation.NavigationApplication;
 | 
	
		
			
			| 15 |  | -import com.reactnativenavigation.layout.LayoutFactory;
 | 
	
		
			
			| 16 |  | -import com.reactnativenavigation.layout.parse.LayoutNode;
 | 
	
		
			
			| 17 |  | -import com.reactnativenavigation.layout.StackLayout;
 | 
	
		
			
			| 18 |  | -import com.reactnativenavigation.layout.bottomtabs.BottomTabsCreator;
 | 
	
		
			
			| 19 | 9 |  import com.reactnativenavigation.utils.UiThread;
 | 
	
		
			
			| 20 | 10 |  
 | 
	
		
			
			| 21 |  | -import java.util.ArrayList;
 | 
	
		
			
			| 22 |  | -import java.util.HashMap;
 | 
	
		
			
			| 23 |  | -import java.util.List;
 | 
	
		
			
			| 24 |  | -import java.util.Map;
 | 
	
		
			
			| 25 |  | -
 | 
	
		
			
			| 26 | 11 |  public class NavigationModule extends ReactContextBaseJavaModule {
 | 
	
		
			
			| 27 | 12 |  	private static final String NAME = "RNNBridgeModule";
 | 
	
		
			
			| 28 | 13 |  
 | 
	
	
		
			
			|  | @@ -35,97 +20,42 @@ public class NavigationModule extends ReactContextBaseJavaModule {
 | 
	
		
			
			| 35 | 20 |  		return NAME;
 | 
	
		
			
			| 36 | 21 |  	}
 | 
	
		
			
			| 37 | 22 |  
 | 
	
		
			
			|  | 23 | +	public NavigationActivity activity() {
 | 
	
		
			
			|  | 24 | +		return (NavigationActivity) getCurrentActivity();
 | 
	
		
			
			|  | 25 | +	}
 | 
	
		
			
			|  | 26 | +
 | 
	
		
			
			| 38 | 27 |  	@ReactMethod
 | 
	
		
			
			| 39 | 28 |  	public void setRoot(final ReadableMap layoutTree) {
 | 
	
		
			
			| 40 |  | -		if (getCurrentActivity() == null) return;
 | 
	
		
			
			| 41 |  | -
 | 
	
		
			
			| 42 |  | -		UiThread.post(new Runnable() {
 | 
	
		
			
			|  | 29 | +		handle(new Runnable() {
 | 
	
		
			
			| 43 | 30 |  			@Override
 | 
	
		
			
			| 44 | 31 |  			public void run() {
 | 
	
		
			
			| 45 |  | -				LayoutFactory factory =
 | 
	
		
			
			| 46 |  | -						new LayoutFactory(getCurrentActivity(), new LayoutFactory.ReactRootViewCreator() {
 | 
	
		
			
			| 47 |  | -							@Override
 | 
	
		
			
			| 48 |  | -							public View create(String id, String name) {
 | 
	
		
			
			| 49 |  | -								ReactRootView rootView = new ReactRootView(getCurrentActivity());
 | 
	
		
			
			| 50 |  | -								Bundle opts = new Bundle();
 | 
	
		
			
			| 51 |  | -								opts.putString("id", id);
 | 
	
		
			
			| 52 |  | -								rootView.startReactApplication(NavigationApplication.instance.getReactNativeHost().getReactInstanceManager(), name, opts);
 | 
	
		
			
			| 53 |  | -								return rootView;
 | 
	
		
			
			| 54 |  | -							}
 | 
	
		
			
			| 55 |  | -						}, new BottomTabsCreator());
 | 
	
		
			
			| 56 |  | -
 | 
	
		
			
			| 57 |  | -				final LayoutNode layoutTreeRoot = readableMapToLayoutNode(layoutTree);
 | 
	
		
			
			| 58 |  | -				final View rootView = factory.create(layoutTreeRoot);
 | 
	
		
			
			| 59 |  | -				getCurrentActivity().setContentView(rootView);
 | 
	
		
			
			|  | 32 | +				NavigationApplication.instance.getConfig().commandsHandler.setRoot(activity(), layoutTree);
 | 
	
		
			
			| 60 | 33 |  			}
 | 
	
		
			
			| 61 | 34 |  		});
 | 
	
		
			
			| 62 | 35 |  	}
 | 
	
		
			
			| 63 | 36 |  
 | 
	
		
			
			| 64 | 37 |  	@ReactMethod
 | 
	
		
			
			| 65 |  | -	public void push(String onContainerId, final ReadableMap layout) {
 | 
	
		
			
			| 66 |  | -		if (getCurrentActivity() == null) return;
 | 
	
		
			
			| 67 |  | -
 | 
	
		
			
			| 68 |  | -		UiThread.post(new Runnable() {
 | 
	
		
			
			|  | 38 | +	public void push(final String onContainerId, final ReadableMap layout) {
 | 
	
		
			
			|  | 39 | +		handle(new Runnable() {
 | 
	
		
			
			| 69 | 40 |  			@Override
 | 
	
		
			
			| 70 | 41 |  			public void run() {
 | 
	
		
			
			| 71 |  | -				LayoutFactory factory =
 | 
	
		
			
			| 72 |  | -						new LayoutFactory(getCurrentActivity(), new LayoutFactory.ReactRootViewCreator() {
 | 
	
		
			
			| 73 |  | -							@Override
 | 
	
		
			
			| 74 |  | -							public View create(String id, String name) {
 | 
	
		
			
			| 75 |  | -								ReactRootView rootView = new ReactRootView(getCurrentActivity());
 | 
	
		
			
			| 76 |  | -								Bundle opts = new Bundle();
 | 
	
		
			
			| 77 |  | -								opts.putString("id", id);
 | 
	
		
			
			| 78 |  | -								rootView.startReactApplication(NavigationApplication.instance.getReactNativeHost().getReactInstanceManager(), name, opts);
 | 
	
		
			
			| 79 |  | -								return rootView;
 | 
	
		
			
			| 80 |  | -							}
 | 
	
		
			
			| 81 |  | -						}, new BottomTabsCreator());
 | 
	
		
			
			| 82 |  | -				final LayoutNode layoutNode = readableMapToLayoutNode(layout);
 | 
	
		
			
			| 83 |  | -				final View rootView = factory.create(layoutNode);
 | 
	
		
			
			| 84 |  | -				((StackLayout) ((NavigationActivity) getCurrentActivity()).getContentView()).push(rootView);
 | 
	
		
			
			|  | 42 | +				NavigationApplication.instance.getConfig().commandsHandler.push(activity(), onContainerId, layout);
 | 
	
		
			
			| 85 | 43 |  			}
 | 
	
		
			
			| 86 | 44 |  		});
 | 
	
		
			
			| 87 | 45 |  	}
 | 
	
		
			
			| 88 | 46 |  
 | 
	
		
			
			| 89 | 47 |  	@ReactMethod
 | 
	
		
			
			| 90 |  | -	public void pop(String onContainerId) {
 | 
	
		
			
			| 91 |  | -		if (getCurrentActivity() == null) return;
 | 
	
		
			
			| 92 |  | -
 | 
	
		
			
			| 93 |  | -		UiThread.post(new Runnable() {
 | 
	
		
			
			|  | 48 | +	public void pop(final String onContainerId) {
 | 
	
		
			
			|  | 49 | +		handle(new Runnable() {
 | 
	
		
			
			| 94 | 50 |  			@Override
 | 
	
		
			
			| 95 | 51 |  			public void run() {
 | 
	
		
			
			| 96 |  | -				((StackLayout) ((NavigationActivity) getCurrentActivity()).getContentView()).pop();
 | 
	
		
			
			|  | 52 | +				NavigationApplication.instance.getConfig().commandsHandler.pop(activity(), onContainerId);
 | 
	
		
			
			| 97 | 53 |  			}
 | 
	
		
			
			| 98 | 54 |  		});
 | 
	
		
			
			| 99 | 55 |  	}
 | 
	
		
			
			| 100 | 56 |  
 | 
	
		
			
			| 101 |  | -	private LayoutNode readableMapToLayoutNode(ReadableMap readableMap) {
 | 
	
		
			
			| 102 |  | -		String id = readableMap.getString("id");
 | 
	
		
			
			| 103 |  | -		LayoutNode.Type type = LayoutNode.Type.fromString(readableMap.getString("type"));
 | 
	
		
			
			| 104 |  | -		Map<String, Object> data = readableMapToJavaMap(readableMap.getMap("data"));
 | 
	
		
			
			| 105 |  | -
 | 
	
		
			
			| 106 |  | -		ReadableArray childrenNodes = readableMap.getArray("children");
 | 
	
		
			
			| 107 |  | -		List<LayoutNode> children = new ArrayList<>(childrenNodes.size());
 | 
	
		
			
			| 108 |  | -		for (int i = 0; i < childrenNodes.size(); i++) {
 | 
	
		
			
			| 109 |  | -			ReadableMap child = childrenNodes.getMap(i);
 | 
	
		
			
			| 110 |  | -			children.add(readableMapToLayoutNode(child));
 | 
	
		
			
			| 111 |  | -		}
 | 
	
		
			
			| 112 |  | -
 | 
	
		
			
			| 113 |  | -		return new LayoutNode(id, type, data, children);
 | 
	
		
			
			| 114 |  | -	}
 | 
	
		
			
			| 115 |  | -
 | 
	
		
			
			| 116 |  | -	private Map<String, Object> readableMapToJavaMap(ReadableMap readableMap) {
 | 
	
		
			
			| 117 |  | -		final Map<String, Object> map = new HashMap<>();
 | 
	
		
			
			| 118 |  | -		for (ReadableMapKeySetIterator it = readableMap.keySetIterator(); it.hasNextKey(); ) {
 | 
	
		
			
			| 119 |  | -			final String key = it.nextKey();
 | 
	
		
			
			| 120 |  | -			switch (readableMap.getType(key)) {
 | 
	
		
			
			| 121 |  | -				case String:
 | 
	
		
			
			| 122 |  | -					map.put(key, readableMap.getString(key));
 | 
	
		
			
			| 123 |  | -					break;
 | 
	
		
			
			| 124 |  | -				case Map:
 | 
	
		
			
			| 125 |  | -					map.put(key, readableMapToJavaMap(readableMap.getMap(key)));
 | 
	
		
			
			| 126 |  | -					break;
 | 
	
		
			
			| 127 |  | -			}
 | 
	
		
			
			| 128 |  | -		}
 | 
	
		
			
			| 129 |  | -		return map;
 | 
	
		
			
			|  | 57 | +	private void handle(Runnable task) {
 | 
	
		
			
			|  | 58 | +		if (activity() == null) return;
 | 
	
		
			
			|  | 59 | +		UiThread.post(task);
 | 
	
		
			
			| 130 | 60 |  	}
 | 
	
		
			
			| 131 | 61 |  }
 |