Browse Source

Keep Using navigatorEventId for now

Guy Carmeli 8 years ago
parent
commit
1b8a22a47c

+ 3
- 3
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactEventEmitter.java View File

16
         this.eventEmitter = reactContext.getJSModule(RCTDeviceEventEmitter.class);
16
         this.eventEmitter = reactContext.getJSModule(RCTDeviceEventEmitter.class);
17
     }
17
     }
18
 
18
 
19
-    public void sendEvent(String eventId, String screenInstanceId) {
19
+    public void sendEvent(String eventId, String navigatorEventId) {
20
         WritableMap params = Arguments.createMap();
20
         WritableMap params = Arguments.createMap();
21
         params.putString(KEY_EVENT_TYPE, EVENT_TYPE);
21
         params.putString(KEY_EVENT_TYPE, EVENT_TYPE);
22
         params.putString(KEY_EVENT_ID, eventId);
22
         params.putString(KEY_EVENT_ID, eventId);
23
-        params.putString("navigatorEventID", screenInstanceId);
24
-        eventEmitter.emit(screenInstanceId, params);
23
+        params.putString("navigatorEventID", navigatorEventId);
24
+        eventEmitter.emit(navigatorEventId, params);
25
     }
25
     }
26
 }
26
 }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenLayout.java View File

40
     }
40
     }
41
 
41
 
42
     private void addTitleBar() {
42
     private void addTitleBar() {
43
-        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.screenInstanceId);
43
+        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
44
         topBar.setTitle(screenParams.title);
44
         topBar.setTitle(screenParams.title);
45
     }
45
     }
46
 
46
 
50
     }
50
     }
51
 
51
 
52
     private void addContentView() {
52
     private void addContentView() {
53
-        contentView = new ContentView(getContext(), screenParams.screenId, screenParams.passProps, this);
53
+        contentView = new ContentView(getContext(), screenParams, this);
54
         addView(contentView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
54
         addView(contentView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
55
         contentView.init();
55
         contentView.init();
56
     }
56
     }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/TopBar.java View File

17
         setFitsSystemWindows(true);
17
         setFitsSystemWindows(true);
18
     }
18
     }
19
 
19
 
20
-    public void addTitleBarAndSetButtons(List<TitleBarButtonParams> buttons, String screenInstanceId) {
20
+    public void addTitleBarAndSetButtons(List<TitleBarButtonParams> buttons, String navigatorEventId) {
21
         titleBar = new TitleBar(getContext());
21
         titleBar = new TitleBar(getContext());
22
         addView(titleBar);
22
         addView(titleBar);
23
-        titleBar.setButtons(buttons, screenInstanceId);
23
+        titleBar.setButtons(buttons, navigatorEventId);
24
     }
24
     }
25
 
25
 
26
     public void setTitleBarVisibility(boolean isHidden) {
26
     public void setTitleBarVisibility(boolean isHidden) {

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/ScreenParams.java View File

8
     public String screenId;
8
     public String screenId;
9
     public String screenInstanceId;
9
     public String screenInstanceId;
10
     public Bundle passProps;
10
     public Bundle passProps;
11
+    public String navigatorEventId;
11
     public List<TitleBarButtonParams> buttons;
12
     public List<TitleBarButtonParams> buttons;
12
     public String title;
13
     public String title;
13
     public ScreenStyleParams styleParams;
14
     public ScreenStyleParams styleParams;
15
+    public Bundle navigationParams;
14
     //    public String tabLabel; TODO when tabs are supported move to TabParams
16
     //    public String tabLabel; TODO when tabs are supported move to TabParams
15
     //    public Drawable tabIcon;
17
     //    public Drawable tabIcon;
16
 }
18
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/ScreenParamsParser.java View File

8
     private static final String KEY_TITLE = "title";
8
     private static final String KEY_TITLE = "title";
9
     private static final String KEY_SCREEN_ID = "screenId";
9
     private static final String KEY_SCREEN_ID = "screenId";
10
     private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
10
     private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
11
+    private static final String KEY_NAVIGATOR_EVENT_ID = "navigatorEventID";
11
     private static final String KEY_PROPS = "passProps";
12
     private static final String KEY_PROPS = "passProps";
13
+    private static final String KEY_NAVIGATION_PARAMS = "navigationParams";
12
     private static final String KEY_BUTTONS = "titleBarButtons";
14
     private static final String KEY_BUTTONS = "titleBarButtons";
13
     private static final String STYLE_PARAMS = "styleParams";
15
     private static final String STYLE_PARAMS = "styleParams";
14
 
16
 
17
         result.screenId = params.getString(KEY_SCREEN_ID);
19
         result.screenId = params.getString(KEY_SCREEN_ID);
18
         result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
20
         result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
19
         result.passProps = params.getBundle(KEY_PROPS);
21
         result.passProps = params.getBundle(KEY_PROPS);
22
+        result.navigationParams = params.getBundle(KEY_NAVIGATION_PARAMS);
23
+        assert result.navigationParams != null;
24
+        result.navigatorEventId = result.navigationParams.getString(KEY_NAVIGATOR_EVENT_ID);
20
         result.buttons = TitleBarButtonParamsParser.parse(params.getBundle(KEY_BUTTONS));
25
         result.buttons = TitleBarButtonParamsParser.parse(params.getBundle(KEY_BUTTONS));
21
         result.title = params.getString(KEY_TITLE);
26
         result.title = params.getString(KEY_TITLE);
22
         result.styleParams = ScreenStyleParamsParser.parse(params.getBundle(STYLE_PARAMS));
27
         result.styleParams = ScreenStyleParamsParser.parse(params.getBundle(STYLE_PARAMS));

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/TitleBarButtonParamsParser.java View File

31
         result.color = new ScreenStyleParams.Color(ColorParser.parse(bundle.getString("color")));
31
         result.color = new ScreenStyleParams.Color(ColorParser.parse(bundle.getString("color")));
32
         result.showAsAction = parseShowAsAction(bundle.getString("showAsAction"));
32
         result.showAsAction = parseShowAsAction(bundle.getString("showAsAction"));
33
         result.enabled = bundle.getBoolean("enabled", true);
33
         result.enabled = bundle.getBoolean("enabled", true);
34
+        result.eventId = bundle.getString("id");
34
         return result;
35
         return result;
35
     }
36
     }
36
 
37
 

+ 14
- 4
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

7
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactInstanceManager;
8
 import com.facebook.react.ReactRootView;
8
 import com.facebook.react.ReactRootView;
9
 import com.reactnativenavigation.NavigationApplication;
9
 import com.reactnativenavigation.NavigationApplication;
10
+import com.reactnativenavigation.params.ScreenParams;
10
 import com.reactnativenavigation.react.ReactViewHacks;
11
 import com.reactnativenavigation.react.ReactViewHacks;
11
 
12
 
12
 public class ContentView extends ReactRootView {
13
 public class ContentView extends ReactRootView {
13
 
14
 
14
     private final String screenId;
15
     private final String screenId;
15
     private final Bundle passProps;
16
     private final Bundle passProps;
17
+    private Bundle navigationParams;
16
     private final ScrollDirectionListener.OnScrollChanged scrollListener;
18
     private final ScrollDirectionListener.OnScrollChanged scrollListener;
17
 
19
 
18
-    public ContentView(Context context, String screenId, Bundle passProps, ScrollDirectionListener.OnScrollChanged scrollListener) {
20
+    public ContentView(Context context, ScreenParams screenParams, ScrollDirectionListener.OnScrollChanged scrollListener) {
19
         super(context);
21
         super(context);
20
-        this.screenId = screenId;
21
-        this.passProps = passProps;
22
+        this.screenId = screenParams.screenId;
23
+        this.passProps = screenParams.passProps;
24
+        this.navigationParams = screenParams.navigationParams;
22
         this.scrollListener = scrollListener;
25
         this.scrollListener = scrollListener;
23
     }
26
     }
24
 
27
 
25
     public void init() {
28
     public void init() {
26
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
29
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
27
-        startReactApplication(react, screenId, passProps);
30
+        startReactApplication(react, screenId, mergePropsAndNavigationParams());
28
         new ScrollViewAttacher(this, scrollListener).attach();
31
         new ScrollViewAttacher(this, scrollListener).attach();
29
     }
32
     }
30
 
33
 
34
+    private Bundle mergePropsAndNavigationParams() {
35
+        if (passProps != null) {
36
+            navigationParams.putAll(passProps);
37
+        }
38
+        return navigationParams;
39
+    }
40
+
31
     public void removeFromParentWithoutUnmount() {
41
     public void removeFromParentWithoutUnmount() {
32
         // Hack in order to prevent the react view from getting unmounted
42
         // Hack in order to prevent the react view from getting unmounted
33
         ReactViewHacks.preventUnmountOnDetachedFromWindow(this);
43
         ReactViewHacks.preventUnmountOnDetachedFromWindow(this);

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

18
         super(context);
18
         super(context);
19
     }
19
     }
20
 
20
 
21
-    public void setButtons(List<TitleBarButtonParams> buttons, String screenInstanceId) {
21
+    public void setButtons(List<TitleBarButtonParams> buttons, String navigatorEventId) {
22
         Menu menu = getMenu();
22
         Menu menu = getMenu();
23
         menu.clear();
23
         menu.clear();
24
 
24
 
25
         for (int i = 0; i < buttons.size(); i++) {
25
         for (int i = 0; i < buttons.size(); i++) {
26
-            final TitleBarButton button = new TitleBarButton(menu, this, buttons.get(i), screenInstanceId);
26
+            final TitleBarButton button = new TitleBarButton(menu, this, buttons.get(i), navigatorEventId);
27
             // Add button in reverse order because in iOS index 0 starts at right
27
             // Add button in reverse order because in iOS index 0 starts at right
28
             final int index = buttons.size() - i - 1;
28
             final int index = buttons.size() - i - 1;
29
             button.addToMenu(index);
29
             button.addToMenu(index);

+ 4
- 4
android/app/src/main/java/com/reactnativenavigation/views/TitleBarButton.java View File

18
     private final Menu menu;
18
     private final Menu menu;
19
     private final View parent;
19
     private final View parent;
20
     private TitleBarButtonParams buttonParams;
20
     private TitleBarButtonParams buttonParams;
21
-    private String screenInstanceId;
21
+    private String navigatorEventId;
22
 
22
 
23
-    public TitleBarButton(Menu menu, View parent, TitleBarButtonParams buttonParams, String screenInstanceId) {
23
+    public TitleBarButton(Menu menu, View parent, TitleBarButtonParams buttonParams, String navigatorEventId) {
24
         this.menu = menu;
24
         this.menu = menu;
25
         this.parent = parent;
25
         this.parent = parent;
26
         this.buttonParams = buttonParams;
26
         this.buttonParams = buttonParams;
27
-        this.screenInstanceId = screenInstanceId;
27
+        this.navigatorEventId = navigatorEventId;
28
     }
28
     }
29
 
29
 
30
     public MenuItem addToMenu(int index) {
30
     public MenuItem addToMenu(int index) {
93
 
93
 
94
     @Override
94
     @Override
95
     public boolean onMenuItemClick(MenuItem item) {
95
     public boolean onMenuItemClick(MenuItem item) {
96
-        NavigationApplication.instance.getReactEventEmitter().sendEvent(buttonParams.eventId, screenInstanceId);
96
+        NavigationApplication.instance.getReactEventEmitter().sendEvent(buttonParams.eventId, navigatorEventId);
97
         return true;
97
         return true;
98
     }
98
     }
99
 }
99
 }

+ 6
- 19
src/Screen.js View File

11
 const _allNavigatorEventHandlers = {};
11
 const _allNavigatorEventHandlers = {};
12
 
12
 
13
 class Navigator {
13
 class Navigator {
14
-  constructor(navigatorID, navigatorEventID, screenInstanceId) {
14
+  constructor(navigatorID, navigatorEventID) {
15
     this.navigatorID = navigatorID;
15
     this.navigatorID = navigatorID;
16
     this.navigatorEventID = navigatorEventID;
16
     this.navigatorEventID = navigatorEventID;
17
-    this.screenInstanceId = screenInstanceId;
18
     this.navigatorEventHandler = null;
17
     this.navigatorEventHandler = null;
19
     this.navigatorEventSubscription = null;
18
     this.navigatorEventSubscription = null;
20
   }
19
   }
102
   setOnNavigatorEvent(callback) {
101
   setOnNavigatorEvent(callback) {
103
     this.navigatorEventHandler = callback;
102
     this.navigatorEventHandler = callback;
104
     if (!this.navigatorEventSubscription) {
103
     if (!this.navigatorEventSubscription) {
105
-      if (Platform.OS === 'android') {
106
-        let Emitter = DeviceEventEmitter;
107
-        this.navigatorEventSubscription = Emitter.addListener(this.screenInstanceId, (event) => this.onNavigatorEvent(event));
108
-        _allNavigatorEventHandlers[this.screenInstanceId] = (event) => this.onNavigatorEvent(event);
109
-      } else {
110
-        let Emitter = NativeAppEventEmitter;
111
-        this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
112
-        _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
113
-      }
104
+      let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
105
+      this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
106
+      _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
114
     }
107
     }
115
   }
108
   }
116
 
109
 
145
 
138
 
146
   constructor(props) {
139
   constructor(props) {
147
     super(props);
140
     super(props);
148
-    // TODO: remove this shit
149
-    if (!props.navigatorID) {
150
-      props.navigatorID = _.uniqueId('navigatorId');
141
+    if (props.navigatorID) {
142
+      this.navigator = new Navigator(props.navigatorID, props.navigatorEventID);
151
     }
143
     }
152
-    // TODO: keep this shit?
153
-    if (!props.screenInstanceId) {
154
-      props.screenInstanceId = _.uniqueId('screenInstanceId');
155
-    }
156
-    this.navigator = new Navigator(props.navigatorID, props.navigatorEventID, props.screenInstanceId);
157
   }
144
   }
158
 
145
 
159
   componentWillUnmount() {
146
   componentWillUnmount() {

+ 11
- 0
src/platformSpecific.android.js View File

14
   }
14
   }
15
 
15
 
16
   params.screen = convertNavigationStyleToScreenStyle(screen);
16
   params.screen = convertNavigationStyleToScreenStyle(screen);
17
+  params.screen = createNavigationParams(screen);
17
 
18
 
18
   NativeModules.NavigationReactModule.startApp(params);
19
   NativeModules.NavigationReactModule.startApp(params);
19
 }
20
 }
39
   return screen;
40
   return screen;
40
 }
41
 }
41
 
42
 
43
+function createNavigationParams(screen) {
44
+  screen.navigationParams = {
45
+    screenInstanceID: screen.screenInstanceID,
46
+    navigatorID: screen.navigatorID,
47
+    navigatorEventID: screen.navigatorEventID
48
+  };
49
+  screen = _.omit(screen, ['screenInstanceID', 'navigatorID', 'navigatorEventID']);
50
+  return screen;
51
+}
52
+
42
 module.exports = {
53
 module.exports = {
43
   startApp
54
   startApp
44
 };
55
 };