Przeglądaj źródła

Implement switchToTopTab method (#1145)

Usage is identical to switchToTab, closes #842
Guy Carmeli 7 lat temu
rodzic
commit
a43680ca27

+ 10
- 2
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java Wyświetl plik

@@ -52,8 +52,6 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
52 52
 
53 53
     @ReactMethod
54 54
     public void startApp(final ReadableMap params) {
55
-        boolean portraitOnlyMode = false;
56
-        boolean landscapeOnlyMode = false;
57 55
         NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
58 56
     }
59 57
 
@@ -118,6 +116,16 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
118 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 129
     @ReactMethod
122 130
     public void toggleSideMenuVisible(boolean animated, String side) {
123 131
         NavigationCommandsHandler.toggleSideMenuVisible(animated, Side.fromString(side));

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/Modal.java Wyświetl plik

@@ -82,6 +82,14 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
82 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 93
     interface OnModalDismissedListener {
86 94
         void onModalDismissed(Modal modal);
87 95
     }

+ 12
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/ModalController.java Wyświetl plik

@@ -164,4 +164,16 @@ class ModalController implements ScreenStackContainer, Modal.OnModalDismissedLis
164 164
     Window getWindow() {
165 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 Wyświetl plik

@@ -5,6 +5,7 @@ import android.content.Intent;
5 5
 import android.content.res.Configuration;
6 6
 import android.os.Build;
7 7
 import android.os.Bundle;
8
+import android.support.annotation.NonNull;
8 9
 import android.support.annotation.Nullable;
9 10
 import android.support.v7.app.AppCompatActivity;
10 11
 import android.view.KeyEvent;
@@ -298,6 +299,16 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
298 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 312
     public void selectBottomTabByTabIndex(Integer index) {
302 313
         if (layout instanceof BottomTabsLayout) {
303 314
             ((BottomTabsLayout) layout).selectBottomTabByTabIndex(index);
@@ -398,7 +409,8 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
398 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 414
         NavigationApplication.instance.getActivityCallbacks().onRequestPermissionsResult(requestCode, permissions, grantResults);
403 415
         if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
404 416
             mPermissionListener = null;

+ 27
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java Wyświetl plik

@@ -303,6 +303,33 @@ public class NavigationCommandsHandler {
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 333
     public static void selectBottomTabByTabIndex(final Integer index) {
307 334
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
308 335
         if (currentActivity == null) {

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Wyświetl plik

@@ -195,6 +195,20 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
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 212
     @Override
199 213
     public void toggleSideMenuVisible(boolean animated, Side side) {
200 214
         if (sideMenu != null) {

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java Wyświetl plik

@@ -57,4 +57,8 @@ public interface Layout extends ScreenStackContainer {
57 57
     void showLightBox(LightBoxParams params);
58 58
 
59 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 Wyświetl plik

@@ -250,6 +250,16 @@ public class SingleScreenLayout extends BaseLayout {
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 263
     @Override
254 264
     public void showSlidingOverlay(final SlidingOverlayParams params) {
255 265
         slidingOverlaysQueue.add(new SlidingOverlay(this, params));

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java Wyświetl plik

@@ -179,6 +179,10 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
179 179
         return screenParams.getScreenInstanceId();
180 180
     }
181 181
 
182
+    public boolean hasScreenInstance(String screenInstanceId) {
183
+        return screenParams.getScreenInstanceId().equals(screenInstanceId);
184
+    }
185
+
182 186
     public abstract String getNavigatorEventId();
183 187
 
184 188
     public BaseScreenParams getScreenParams() {

+ 21
- 2
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java Wyświetl plik

@@ -358,6 +358,26 @@ public class ScreenStack {
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 381
     public StyleParams getCurrentScreenStyleParams() {
362 382
         return stack.peek().getStyleParams();
363 383
     }
@@ -375,9 +395,8 @@ public class ScreenStack {
375 395
         if (stack.isEmpty()) {
376 396
             return;
377 397
         }
378
-
379 398
         for (Screen screen : stack) {
380
-            if (screen.getScreenInstanceId().equals(screenInstanceId)) {
399
+            if (screen.hasScreenInstance(screenInstanceId)) {
381 400
                 task.run(screen);
382 401
                 return;
383 402
             }

+ 22
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java Wyświetl plik

@@ -127,4 +127,26 @@ public class ViewPagerScreen extends Screen {
127 127
     public String getNavigatorEventId() {
128 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 Wyświetl plik

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

+ 9
- 0
src/deprecated/platformSpecificDeprecated.android.js Wyświetl plik

@@ -350,6 +350,14 @@ function navigatorSwitchToTab(navigator, params) {
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 361
 function navigatorToggleDrawer(navigator, params) {
354 362
   const animated = !(params.animated === false);
355 363
   if (params.to) {
@@ -661,6 +669,7 @@ export default {
661 669
   navigatorSetTitle,
662 670
   navigatorSetSubtitle,
663 671
   navigatorSwitchToTab,
672
+  navigatorSwitchToTopTab,
664 673
   navigatorToggleDrawer,
665 674
   navigatorToggleTabs,
666 675
   navigatorToggleNavBar,

+ 10
- 0
src/platformSpecific.android.js Wyświetl plik

@@ -121,6 +121,14 @@ function setSideMenuVisible(animated, visible, side) {
121 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 132
 function selectBottomTabByNavigatorId(navigatorId) {
125 133
   NativeReactModule.selectBottomTabByNavigatorId(navigatorId);
126 134
 }
@@ -169,6 +177,8 @@ module.exports = {
169 177
   dismissAllModals,
170 178
   showLightBox,
171 179
   dismissLightBox,
180
+  selectTopTabByScreen,
181
+  selectTopTabByTabIndex,
172 182
   showInAppNotification,
173 183
   dismissInAppNotification,
174 184
   toggleSideMenuVisible,