Browse Source

Add support in switchToTab() with no parameters

Amit Davidi 8 years ago
parent
commit
3c154f8203

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

59
     protected static final String KEY_TAB_INDEX = "tabIndex";
59
     protected static final String KEY_TAB_INDEX = "tabIndex";
60
     protected static final String KEY_TITLE = "title";
60
     protected static final String KEY_TITLE = "title";
61
     protected static final String KEY_TO = "to";
61
     protected static final String KEY_TO = "to";
62
+    protected static final String KEY_NAVIGATOR_ID = "navigatorID";
63
+
62
     private static final String TAG = "BaseReactActivity";
64
     private static final String TAG = "BaseReactActivity";
63
     private static final String REDBOX_PERMISSION_MESSAGE =
65
     private static final String REDBOX_PERMISSION_MESSAGE =
64
             "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
66
             "Overlay permissions needs to be granted in order for react native apps to run in dev mode";

+ 19
- 1
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

212
     }
212
     }
213
 
213
 
214
     public void switchToTab(ReadableMap params) {
214
     public void switchToTab(ReadableMap params) {
215
-        int tabIndex = params.getInt(KEY_TAB_INDEX);
215
+        Integer tabIndex;
216
+        if (params.hasKey(KEY_TAB_INDEX)) {
217
+            tabIndex = params.getInt(KEY_TAB_INDEX);
218
+        } else {
219
+            final String navigatorId = params.getString(KEY_NAVIGATOR_ID);
220
+            tabIndex = findNavigatorTabIndex(navigatorId);
221
+        }
216
         mBottomNavigation.setCurrentItem(tabIndex);
222
         mBottomNavigation.setCurrentItem(tabIndex);
217
     }
223
     }
218
 
224
 
255
         }
261
         }
256
     }
262
     }
257
 
263
 
264
+    protected Integer findNavigatorTabIndex(String navigatorId) {
265
+        for (int i = 0; i < mScreenStacks.size(); i++) {
266
+            ScreenStack stack = mScreenStacks.get(i);
267
+            if (!stack.isEmpty() && stack.peek().navigatorId.equals(navigatorId)) {
268
+                return i;
269
+            }
270
+        }
271
+
272
+        return null;
273
+    }
274
+
258
     private void setTabsWithIcons(ArrayList<Screen> screens, Map<Screen, Drawable> icons) {
275
     private void setTabsWithIcons(ArrayList<Screen> screens, Map<Screen, Drawable> icons) {
259
         mScreenStacks = new ArrayList<>();
276
         mScreenStacks = new ArrayList<>();
260
         for (Screen screen : screens) {
277
         for (Screen screen : screens) {
268
         this.onTabSelected(0, false);
285
         this.onTabSelected(0, false);
269
     }
286
     }
270
 
287
 
288
+
271
     @Override
289
     @Override
272
     protected void removeAllReactViews() {
290
     protected void removeAllReactViews() {
273
         for (ScreenStack screenStack : mScreenStacks) {
291
         for (ScreenStack screenStack : mScreenStacks) {

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

99
 
99
 
100
 function navigatorSwitchToTab(navigator, params) {
100
 function navigatorSwitchToTab(navigator, params) {
101
   RctActivity.switchToTab({
101
   RctActivity.switchToTab({
102
+    navigatorID: navigator.navigatorID,
102
     tabIndex: params.tabIndex
103
     tabIndex: params.tabIndex
103
   });
104
   });
104
 }
105
 }