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,11 +16,11 @@ public class NavigationReactEventEmitter {
16 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 20
         WritableMap params = Arguments.createMap();
21 21
         params.putString(KEY_EVENT_TYPE, EVENT_TYPE);
22 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,7 +40,7 @@ public class ScreenLayout extends LinearLayout implements ScrollDirectionListene
40 40
     }
41 41
 
42 42
     private void addTitleBar() {
43
-        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.screenInstanceId);
43
+        topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
44 44
         topBar.setTitle(screenParams.title);
45 45
     }
46 46
 
@@ -50,7 +50,7 @@ public class ScreenLayout extends LinearLayout implements ScrollDirectionListene
50 50
     }
51 51
 
52 52
     private void addContentView() {
53
-        contentView = new ContentView(getContext(), screenParams.screenId, screenParams.passProps, this);
53
+        contentView = new ContentView(getContext(), screenParams, this);
54 54
         addView(contentView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
55 55
         contentView.init();
56 56
     }

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

@@ -17,10 +17,10 @@ public class TopBar extends AppBarLayout {
17 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 21
         titleBar = new TitleBar(getContext());
22 22
         addView(titleBar);
23
-        titleBar.setButtons(buttons, screenInstanceId);
23
+        titleBar.setButtons(buttons, navigatorEventId);
24 24
     }
25 25
 
26 26
     public void setTitleBarVisibility(boolean isHidden) {

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

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

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

@@ -8,7 +8,9 @@ public class ScreenParamsParser {
8 8
     private static final String KEY_TITLE = "title";
9 9
     private static final String KEY_SCREEN_ID = "screenId";
10 10
     private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
11
+    private static final String KEY_NAVIGATOR_EVENT_ID = "navigatorEventID";
11 12
     private static final String KEY_PROPS = "passProps";
13
+    private static final String KEY_NAVIGATION_PARAMS = "navigationParams";
12 14
     private static final String KEY_BUTTONS = "titleBarButtons";
13 15
     private static final String STYLE_PARAMS = "styleParams";
14 16
 
@@ -17,6 +19,9 @@ public class ScreenParamsParser {
17 19
         result.screenId = params.getString(KEY_SCREEN_ID);
18 20
         result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
19 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 25
         result.buttons = TitleBarButtonParamsParser.parse(params.getBundle(KEY_BUTTONS));
21 26
         result.title = params.getString(KEY_TITLE);
22 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,6 +31,7 @@ public class TitleBarButtonParamsParser extends Parser {
31 31
         result.color = new ScreenStyleParams.Color(ColorParser.parse(bundle.getString("color")));
32 32
         result.showAsAction = parseShowAsAction(bundle.getString("showAsAction"));
33 33
         result.enabled = bundle.getBoolean("enabled", true);
34
+        result.eventId = bundle.getString("id");
34 35
         return result;
35 36
     }
36 37
 

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

@@ -7,27 +7,37 @@ import android.view.ViewGroup;
7 7
 import com.facebook.react.ReactInstanceManager;
8 8
 import com.facebook.react.ReactRootView;
9 9
 import com.reactnativenavigation.NavigationApplication;
10
+import com.reactnativenavigation.params.ScreenParams;
10 11
 import com.reactnativenavigation.react.ReactViewHacks;
11 12
 
12 13
 public class ContentView extends ReactRootView {
13 14
 
14 15
     private final String screenId;
15 16
     private final Bundle passProps;
17
+    private Bundle navigationParams;
16 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 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 25
         this.scrollListener = scrollListener;
23 26
     }
24 27
 
25 28
     public void init() {
26 29
         ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
27
-        startReactApplication(react, screenId, passProps);
30
+        startReactApplication(react, screenId, mergePropsAndNavigationParams());
28 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 41
     public void removeFromParentWithoutUnmount() {
32 42
         // Hack in order to prevent the react view from getting unmounted
33 43
         ReactViewHacks.preventUnmountOnDetachedFromWindow(this);

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

@@ -18,12 +18,12 @@ public class TitleBar extends Toolbar {
18 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 22
         Menu menu = getMenu();
23 23
         menu.clear();
24 24
 
25 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 27
             // Add button in reverse order because in iOS index 0 starts at right
28 28
             final int index = buttons.size() - i - 1;
29 29
             button.addToMenu(index);

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

@@ -18,13 +18,13 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
18 18
     private final Menu menu;
19 19
     private final View parent;
20 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 24
         this.menu = menu;
25 25
         this.parent = parent;
26 26
         this.buttonParams = buttonParams;
27
-        this.screenInstanceId = screenInstanceId;
27
+        this.navigatorEventId = navigatorEventId;
28 28
     }
29 29
 
30 30
     public MenuItem addToMenu(int index) {
@@ -93,7 +93,7 @@ public class TitleBarButton implements MenuItem.OnMenuItemClickListener {
93 93
 
94 94
     @Override
95 95
     public boolean onMenuItemClick(MenuItem item) {
96
-        NavigationApplication.instance.getReactEventEmitter().sendEvent(buttonParams.eventId, screenInstanceId);
96
+        NavigationApplication.instance.getReactEventEmitter().sendEvent(buttonParams.eventId, navigatorEventId);
97 97
         return true;
98 98
     }
99 99
 }

+ 6
- 19
src/Screen.js View File

@@ -11,10 +11,9 @@ import _ from 'lodash';
11 11
 const _allNavigatorEventHandlers = {};
12 12
 
13 13
 class Navigator {
14
-  constructor(navigatorID, navigatorEventID, screenInstanceId) {
14
+  constructor(navigatorID, navigatorEventID) {
15 15
     this.navigatorID = navigatorID;
16 16
     this.navigatorEventID = navigatorEventID;
17
-    this.screenInstanceId = screenInstanceId;
18 17
     this.navigatorEventHandler = null;
19 18
     this.navigatorEventSubscription = null;
20 19
   }
@@ -102,15 +101,9 @@ class Navigator {
102 101
   setOnNavigatorEvent(callback) {
103 102
     this.navigatorEventHandler = callback;
104 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,15 +138,9 @@ export default class Screen extends Component {
145 138
 
146 139
   constructor(props) {
147 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 146
   componentWillUnmount() {

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

@@ -14,6 +14,7 @@ function startApp(params) {
14 14
   }
15 15
 
16 16
   params.screen = convertNavigationStyleToScreenStyle(screen);
17
+  params.screen = createNavigationParams(screen);
17 18
 
18 19
   NativeModules.NavigationReactModule.startApp(params);
19 20
 }
@@ -39,6 +40,16 @@ function convertNavigationStyleToScreenStyle(screen) {
39 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 53
 module.exports = {
43 54
   startApp
44 55
 };