Browse Source

Implement switchToTopTab method (#1145)

Usage is identical to switchToTab, closes #842
Guy Carmeli 7 years ago
parent
commit
a43680ca27

+ 10
- 2
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

52
 
52
 
53
     @ReactMethod
53
     @ReactMethod
54
     public void startApp(final ReadableMap params) {
54
     public void startApp(final ReadableMap params) {
55
-        boolean portraitOnlyMode = false;
56
-        boolean landscapeOnlyMode = false;
57
         NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
55
         NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
58
     }
56
     }
59
 
57
 
118
         NavigationCommandsHandler.selectBottomTabByNavigatorId(navigatorId);
116
         NavigationCommandsHandler.selectBottomTabByNavigatorId(navigatorId);
119
     }
117
     }
120
 
118
 
119
+    @ReactMethod
120
+    public void selectTopTabByTabIndex(String screenInstanceId, int index) {
121
+        NavigationCommandsHandler.selectTopTabByTabIndex(screenInstanceId, index);
122
+    }
123
+
124
+    @ReactMethod
125
+    public void selectTopTabByScreen(String screenInstanceId) {
126
+        NavigationCommandsHandler.selectTopTabByScreen(screenInstanceId);
127
+    }
128
+
121
     @ReactMethod
129
     @ReactMethod
122
     public void toggleSideMenuVisible(boolean animated, String side) {
130
     public void toggleSideMenuVisible(boolean animated, String side) {
123
         NavigationCommandsHandler.toggleSideMenuVisible(animated, Side.fromString(side));
131
         NavigationCommandsHandler.toggleSideMenuVisible(animated, Side.fromString(side));

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/Modal.java View File

82
     public void onSideMenuButtonClick() {
82
     public void onSideMenuButtonClick() {
83
     }
83
     }
84
 
84
 
85
+    void selectTopTabByScreen(String screenInstanceId) {
86
+        layout.selectTopTabByScreen(screenInstanceId);
87
+    }
88
+
89
+    public void selectTopTabByTabIndex(String screenInstanceId, int index) {
90
+        layout.selectTopTabByTabIndex(screenInstanceId, index);
91
+    }
92
+
85
     interface OnModalDismissedListener {
93
     interface OnModalDismissedListener {
86
         void onModalDismissed(Modal modal);
94
         void onModalDismissed(Modal modal);
87
     }
95
     }

+ 12
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/ModalController.java View File

164
     Window getWindow() {
164
     Window getWindow() {
165
         return stack.peek().getWindow();
165
         return stack.peek().getWindow();
166
     }
166
     }
167
+
168
+    void selectTopTabByTabIndex(String screenInstanceId, int index) {
169
+        for (Modal modal : stack) {
170
+            modal.selectTopTabByTabIndex(screenInstanceId, index);
171
+        }
172
+    }
173
+
174
+    void selectTopTabByScreen(String screenInstanceId) {
175
+        for (Modal modal : stack) {
176
+            modal.selectTopTabByScreen(screenInstanceId);
177
+        }
178
+    }
167
 }
179
 }

+ 13
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

5
 import android.content.res.Configuration;
5
 import android.content.res.Configuration;
6
 import android.os.Build;
6
 import android.os.Build;
7
 import android.os.Bundle;
7
 import android.os.Bundle;
8
+import android.support.annotation.NonNull;
8
 import android.support.annotation.Nullable;
9
 import android.support.annotation.Nullable;
9
 import android.support.v7.app.AppCompatActivity;
10
 import android.support.v7.app.AppCompatActivity;
10
 import android.view.KeyEvent;
11
 import android.view.KeyEvent;
298
         layout.setSideMenuVisible(animated, visible, side);
299
         layout.setSideMenuVisible(animated, visible, side);
299
     }
300
     }
300
 
301
 
302
+    public void selectTopTabByTabIndex(String screenInstanceId, int index) {
303
+        layout.selectTopTabByTabIndex(screenInstanceId, index);
304
+        modalController.selectTopTabByTabIndex(screenInstanceId, index);
305
+    }
306
+
307
+    public void selectTopTabByScreen(String screenInstanceId) {
308
+        layout.selectTopTabByScreen(screenInstanceId);
309
+        modalController.selectTopTabByScreen(screenInstanceId);
310
+    }
311
+
301
     public void selectBottomTabByTabIndex(Integer index) {
312
     public void selectBottomTabByTabIndex(Integer index) {
302
         if (layout instanceof BottomTabsLayout) {
313
         if (layout instanceof BottomTabsLayout) {
303
             ((BottomTabsLayout) layout).selectBottomTabByTabIndex(index);
314
             ((BottomTabsLayout) layout).selectBottomTabByTabIndex(index);
398
         requestPermissions(permissions, requestCode);
409
         requestPermissions(permissions, requestCode);
399
     }
410
     }
400
 
411
 
401
-    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
412
+    @Override
413
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
402
         NavigationApplication.instance.getActivityCallbacks().onRequestPermissionsResult(requestCode, permissions, grantResults);
414
         NavigationApplication.instance.getActivityCallbacks().onRequestPermissionsResult(requestCode, permissions, grantResults);
403
         if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
415
         if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
404
             mPermissionListener = null;
416
             mPermissionListener = null;

+ 27
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

303
         });
303
         });
304
     }
304
     }
305
 
305
 
306
+    public static void selectTopTabByTabIndex(final String screenInstanceId, final int index) {
307
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
308
+        if (currentActivity == null) {
309
+            return;
310
+        }
311
+
312
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
313
+            @Override
314
+            public void run() {
315
+                currentActivity.selectTopTabByTabIndex(screenInstanceId, index);
316
+            }
317
+        });
318
+    }
319
+
320
+    public static void selectTopTabByScreen(final String screenInstanceId) {
321
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
322
+        if (currentActivity == null) {
323
+            return;
324
+        }
325
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
326
+            @Override
327
+            public void run() {
328
+                currentActivity.selectTopTabByScreen(screenInstanceId);
329
+            }
330
+        });
331
+    }
332
+
306
     public static void selectBottomTabByTabIndex(final Integer index) {
333
     public static void selectBottomTabByTabIndex(final Integer index) {
307
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
334
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
308
         if (currentActivity == null) {
335
         if (currentActivity == null) {

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

195
         }
195
         }
196
     }
196
     }
197
 
197
 
198
+    @Override
199
+    public void selectTopTabByTabIndex(String screenInstanceId, int index) {
200
+        for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
201
+            screenStacks[i].selectTopTabByTabIndex(screenInstanceId, index);
202
+        }
203
+    }
204
+
205
+    @Override
206
+    public void selectTopTabByScreen(String screenInstanceId) {
207
+        for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
208
+            screenStacks[i].selectTopTabByScreen(screenInstanceId);
209
+        }
210
+    }
211
+
198
     @Override
212
     @Override
199
     public void toggleSideMenuVisible(boolean animated, Side side) {
213
     public void toggleSideMenuVisible(boolean animated, Side side) {
200
         if (sideMenu != null) {
214
         if (sideMenu != null) {

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java View File

57
     void showLightBox(LightBoxParams params);
57
     void showLightBox(LightBoxParams params);
58
 
58
 
59
     void dismissLightBox();
59
     void dismissLightBox();
60
+
61
+    void selectTopTabByTabIndex(String screenInstanceId, int index);
62
+
63
+    void selectTopTabByScreen(String screenInstanceId);
60
 }
64
 }

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

250
         }
250
         }
251
     }
251
     }
252
 
252
 
253
+    @Override
254
+    public void selectTopTabByTabIndex(String screenInstanceId, int index) {
255
+        stack.selectTopTabByTabIndex(screenInstanceId, index);
256
+    }
257
+
258
+    @Override
259
+    public void selectTopTabByScreen(String screenInstanceId) {
260
+        stack.selectTopTabByScreen(screenInstanceId);
261
+    }
262
+
253
     @Override
263
     @Override
254
     public void showSlidingOverlay(final SlidingOverlayParams params) {
264
     public void showSlidingOverlay(final SlidingOverlayParams params) {
255
         slidingOverlaysQueue.add(new SlidingOverlay(this, params));
265
         slidingOverlaysQueue.add(new SlidingOverlay(this, params));

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

179
         return screenParams.getScreenInstanceId();
179
         return screenParams.getScreenInstanceId();
180
     }
180
     }
181
 
181
 
182
+    public boolean hasScreenInstance(String screenInstanceId) {
183
+        return screenParams.getScreenInstanceId().equals(screenInstanceId);
184
+    }
185
+
182
     public abstract String getNavigatorEventId();
186
     public abstract String getNavigatorEventId();
183
 
187
 
184
     public BaseScreenParams getScreenParams() {
188
     public BaseScreenParams getScreenParams() {

+ 21
- 2
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

358
         });
358
         });
359
     }
359
     }
360
 
360
 
361
+    public void selectTopTabByTabIndex(String screenInstanceId, final int index) {
362
+        performOnScreen(screenInstanceId, new Task<Screen>() {
363
+            @Override
364
+            public void run(Screen screen) {
365
+                if (screen.screenParams.hasTopTabs()) {
366
+                    ((ViewPagerScreen) screen).selectTopTabByTabIndex(index);
367
+                }
368
+            }
369
+        });
370
+    }
371
+
372
+    public void selectTopTabByScreen(final String screenInstanceId) {
373
+        performOnScreen(screenInstanceId, new Task<Screen>() {
374
+            @Override
375
+            public void run(Screen screen) {
376
+                ((ViewPagerScreen) screen).selectTopTabByTabByScreen(screenInstanceId);
377
+            }
378
+        });
379
+    }
380
+
361
     public StyleParams getCurrentScreenStyleParams() {
381
     public StyleParams getCurrentScreenStyleParams() {
362
         return stack.peek().getStyleParams();
382
         return stack.peek().getStyleParams();
363
     }
383
     }
375
         if (stack.isEmpty()) {
395
         if (stack.isEmpty()) {
376
             return;
396
             return;
377
         }
397
         }
378
-
379
         for (Screen screen : stack) {
398
         for (Screen screen : stack) {
380
-            if (screen.getScreenInstanceId().equals(screenInstanceId)) {
399
+            if (screen.hasScreenInstance(screenInstanceId)) {
381
                 task.run(screen);
400
                 task.run(screen);
382
                 return;
401
                 return;
383
             }
402
             }

+ 22
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java View File

127
     public String getNavigatorEventId() {
127
     public String getNavigatorEventId() {
128
         return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.navigatorEventId;
128
         return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.navigatorEventId;
129
     }
129
     }
130
+
131
+    public void selectTopTabByTabIndex(int index) {
132
+        viewPager.setCurrentItem(index);
133
+    }
134
+
135
+    @Override
136
+    public boolean hasScreenInstance(String screenInstanceId) {
137
+        for (PageParams topTabParam : screenParams.topTabParams) {
138
+            if(screenInstanceId.equals(topTabParam.getScreenInstanceId())) {
139
+                return true;
140
+            }
141
+        }
142
+        return false;
143
+    }
144
+
145
+    public void selectTopTabByTabByScreen(String screenInstanceId) {
146
+        for (int i = 0; i < screenParams.topTabParams.size(); i++) {
147
+            if (screenParams.topTabParams.get(i).getScreenInstanceId().equals(screenInstanceId)) {
148
+                viewPager.setCurrentItem(i);
149
+            }
150
+        }
151
+    }
130
 }
152
 }

+ 4
- 0
src/Screen.js View File

116
     return platformSpecific.navigatorSwitchToTab(this, params);
116
     return platformSpecific.navigatorSwitchToTab(this, params);
117
   }
117
   }
118
 
118
 
119
+  switchToTopTab(params = {}) {
120
+    return platformSpecific.navigatorSwitchToTopTab(this, params);
121
+  }
122
+
119
   showSnackbar(params = {}) {
123
   showSnackbar(params = {}) {
120
     return platformSpecific.showSnackbar(this, params);
124
     return platformSpecific.showSnackbar(this, params);
121
   }
125
   }

+ 9
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

350
   }
350
   }
351
 }
351
 }
352
 
352
 
353
+function navigatorSwitchToTopTab(navigator, params) {
354
+  if (params.tabIndex >= 0) {
355
+    newPlatformSpecific.selectTopTabByTabIndex(navigator.screenInstanceID, params.tabIndex);
356
+  } else {
357
+    newPlatformSpecific.selectTopTabByScreen(navigator.screenInstanceID);
358
+  }
359
+}
360
+
353
 function navigatorToggleDrawer(navigator, params) {
361
 function navigatorToggleDrawer(navigator, params) {
354
   const animated = !(params.animated === false);
362
   const animated = !(params.animated === false);
355
   if (params.to) {
363
   if (params.to) {
661
   navigatorSetTitle,
669
   navigatorSetTitle,
662
   navigatorSetSubtitle,
670
   navigatorSetSubtitle,
663
   navigatorSwitchToTab,
671
   navigatorSwitchToTab,
672
+  navigatorSwitchToTopTab,
664
   navigatorToggleDrawer,
673
   navigatorToggleDrawer,
665
   navigatorToggleTabs,
674
   navigatorToggleTabs,
666
   navigatorToggleNavBar,
675
   navigatorToggleNavBar,

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

121
   NativeReactModule.setSideMenuVisible(animated, visible, side);
121
   NativeReactModule.setSideMenuVisible(animated, visible, side);
122
 }
122
 }
123
 
123
 
124
+function selectTopTabByTabIndex(screenInstanceId, index) {
125
+  NativeReactModule.selectTopTabByTabIndex(screenInstanceId, index);
126
+}
127
+
128
+function selectTopTabByScreen(screenInstanceId) {
129
+  NativeReactModule.selectTopTabByScreen(screenInstanceId);
130
+}
131
+
124
 function selectBottomTabByNavigatorId(navigatorId) {
132
 function selectBottomTabByNavigatorId(navigatorId) {
125
   NativeReactModule.selectBottomTabByNavigatorId(navigatorId);
133
   NativeReactModule.selectBottomTabByNavigatorId(navigatorId);
126
 }
134
 }
169
   dismissAllModals,
177
   dismissAllModals,
170
   showLightBox,
178
   showLightBox,
171
   dismissLightBox,
179
   dismissLightBox,
180
+  selectTopTabByScreen,
181
+  selectTopTabByTabIndex,
172
   showInAppNotification,
182
   showInAppNotification,
173
   dismissInAppNotification,
183
   dismissInAppNotification,
174
   toggleSideMenuVisible,
184
   toggleSideMenuVisible,