Pārlūkot izejas kodu

Initial implementation

Guy Carmeli 8 gadus atpakaļ
vecāks
revīzija
57deff049c

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java Parādīt failu

@@ -76,7 +76,8 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
76 76
     }
77 77
 
78 78
     @ReactMethod
79
-    public void selectBottomTab(final ReadableMap params) {
79
+    public void selectBottomTab(String navigatorId, Integer index) {
80
+        NavigationCommandsHandler.selectBottomTab(navigatorId, index);
80 81
     }
81 82
 
82 83
     @ReactMethod

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java Parādīt failu

@@ -212,4 +212,10 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
212 212
     public void setSideMenuVisible(boolean animated, boolean visible) {
213 213
         layout.setSideMenuVisible(animated, visible);
214 214
     }
215
+
216
+    public void selectBottomTab(String navigatorId, Integer index) {
217
+        if (layout instanceof BottomTabsLayout) {
218
+            ((BottomTabsLayout) layout).selectBottomTab(navigatorId, index);
219
+        }
220
+    }
215 221
 }

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java Parādīt failu

@@ -238,4 +238,18 @@ public class NavigationCommandsHandler {
238 238
             }
239 239
         });
240 240
     }
241
+
242
+    public static void selectBottomTab(final String navigatorId, final Integer index) {
243
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
244
+        if (currentActivity == null) {
245
+            return;
246
+        }
247
+
248
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
249
+            @Override
250
+            public void run() {
251
+                currentActivity.selectBottomTab(navigatorId, index);
252
+            }
253
+        });
254
+    }
241 255
 }

+ 25
- 12
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Parādīt failu

@@ -164,18 +164,20 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
164 164
         }
165 165
     }
166 166
 
167
+    public void selectBottomTab(String navigatorId, Integer index) {
168
+        if (index != null) {
169
+            bottomTabs.setCurrentItem(index);
170
+        } else {
171
+            bottomTabs.setCurrentItem(getScreenStackIndex(navigatorId));
172
+        }
173
+    }
174
+
167 175
     @Override
168 176
     public void push(ScreenParams screenParams) {
169 177
         ScreenStack screenStack = getScreenStack(screenParams.getNavigatorId());
170
-        if (screenStack == null) {
171
-            return;
172
-        }
173
-
178
+        screenStack.push(screenParams, createScreenLayoutParams(screenParams));
174 179
         if (isCurrentStack(screenStack)) {
175
-            screenStack.push(screenParams, createScreenLayoutParams(screenParams));
176 180
             bottomTabs.setStyleFromScreen(screenParams.styleParams);
177
-        } else {
178
-            screenStack.push(screenParams, createScreenLayoutParams(screenParams));
179 181
         }
180 182
     }
181 183
 
@@ -245,13 +247,24 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
245 247
         return screenStacks[currentStackIndex];
246 248
     }
247 249
 
248
-    private @Nullable ScreenStack getScreenStack(String navigatorId) {
249
-        for (ScreenStack screenStack : screenStacks) {
250
-            if (screenStack.getNavigatorId().equals(navigatorId)) {
251
-                return screenStack;
250
+    private @NonNull ScreenStack getScreenStack(String navigatorId) {
251
+        int index = getScreenStackIndex(navigatorId);
252
+        return screenStacks[index];
253
+    }
254
+
255
+    private int getScreenStackIndex(String navigatorId) throws ScreenStackNotFoundException {
256
+        for (int i = 0; i < screenStacks.length; i++) {
257
+            if (screenStacks[i].getNavigatorId().equals(navigatorId)) {
258
+                return i;
252 259
             }
253 260
         }
254
-        return null;
261
+        throw new ScreenStackNotFoundException("Stack " + navigatorId + " not found");
262
+    }
263
+
264
+    private class ScreenStackNotFoundException extends RuntimeException {
265
+        public ScreenStackNotFoundException(String navigatorId) {
266
+            super(navigatorId);
267
+        }
255 268
     }
256 269
 
257 270
     private boolean isCurrentStack(ScreenStack screenStack) {

+ 66
- 66
example-redux/src/app.js Parādīt failu

@@ -35,74 +35,74 @@ export default class App {
35 35
   startApp(root) {
36 36
     switch (root) {
37 37
       case 'login':
38
-        // Navigation.startSingleScreenApp({
39
-        //  screen: {
40
-        //    screen: 'example.LoginScreen',
41
-        //    title: 'Login',
42
-        //    navigatorStyle: {}
43
-        //  },
44
-        //  passProps: {
45
-        //    str: 'This is a prop passed in \'startSingleScreenApp()\'!',
46
-        //    obj: {
47
-        //      str: 'This is a prop passed in an object!',
48
-        //      arr: [
49
-        //        {
50
-        //          str: 'This is a prop in an object in an array in an object!'
51
-        //        }
52
-        //      ],
53
-        //      arr2: [
54
-        //        [
55
-        //          'array of strings',
56
-        //          'with two strings'
57
-        //        ],
58
-        //        [
59
-        //          1, 2, 3
60
-        //        ]
61
-        //      ]
62
-        //    },
63
-        //    num: 1234,
64
-        //    fn: function() {
65
-        //      return 'Hello from a function!';
66
-        //    }
67
-        //  }
68
-        // });
69 38
         Navigation.startSingleScreenApp({
70
-          screen: {
71
-            screen: 'example.FirstTabScreen',
72
-            title: 'Login',
73
-            topTabs: [
74
-              {
75
-                screenId: 'example.FirstTabScreen',
76
-                title: 'Tab1',
77
-                passProps: {
78
-                  str: 'This is a prop passed to Tab4',
79
-                  fn: () => 'Hello from a function passed as passProps!'
80
-                }
81
-              },
82
-              {
83
-                screenId: 'example.PushedScreen',
84
-                title: 'Tab3',
85
-                passProps: {
86
-                  str: 'This is a prop passed to Tab2'
87
-                }
88
-              },
89
-              {
90
-                screenId: 'example.ListScreen',
91
-                title: 'Tab2',
92
-                passProps: {
93
-                  str: 'This is a prop passed to Tab1'
94
-                }
95
-              }
96
-            ],
97
-            navigatorStyle: {}
98
-          },
99
-          drawer: { // optional, add this if you want a side menu drawer in your app
100
-            left: { // optional, define if you want a drawer from the left
101
-              screen: 'example.SideMenu' // unique ID registered with Navigation.registerScreen
102
-            },
103
-            disableOpenGesture: false // optional, can the drawer be opened with a swipe instead of button
104
-          }
39
+         screen: {
40
+           screen: 'example.LoginScreen',
41
+           title: 'Login',
42
+           navigatorStyle: {}
43
+         },
44
+         passProps: {
45
+           str: 'This is a prop passed in \'startSingleScreenApp()\'!',
46
+           obj: {
47
+             str: 'This is a prop passed in an object!',
48
+             arr: [
49
+               {
50
+                 str: 'This is a prop in an object in an array in an object!'
51
+               }
52
+             ],
53
+             arr2: [
54
+               [
55
+                 'array of strings',
56
+                 'with two strings'
57
+               ],
58
+               [
59
+                 1, 2, 3
60
+               ]
61
+             ]
62
+           },
63
+           num: 1234,
64
+           fn: function() {
65
+             return 'Hello from a function!';
66
+           }
67
+         }
105 68
         });
69
+        // Navigation.startSingleScreenApp({
70
+        //   screen: {
71
+        //     screen: 'example.FirstTabScreen',
72
+        //     title: 'Login',
73
+        //     topTabs: [
74
+        //       {
75
+        //         screenId: 'example.FirstTabScreen',
76
+        //         title: 'Tab1',
77
+        //         passProps: {
78
+        //           str: 'This is a prop passed to Tab4',
79
+        //           fn: () => 'Hello from a function passed as passProps!'
80
+        //         }
81
+        //       },
82
+        //       {
83
+        //         screenId: 'example.PushedScreen',
84
+        //         title: 'Tab3',
85
+        //         passProps: {
86
+        //           str: 'This is a prop passed to Tab2'
87
+        //         }
88
+        //       },
89
+        //       {
90
+        //         screenId: 'example.ListScreen',
91
+        //         title: 'Tab2',
92
+        //         passProps: {
93
+        //           str: 'This is a prop passed to Tab1'
94
+        //         }
95
+        //       }
96
+        //     ],
97
+        //     navigatorStyle: {}
98
+        //   },
99
+        //   drawer: { // optional, add this if you want a side menu drawer in your app
100
+        //     left: { // optional, define if you want a drawer from the left
101
+        //       screen: 'example.SideMenu' // unique ID registered with Navigation.registerScreen
102
+        //     },
103
+        //     disableOpenGesture: false // optional, can the drawer be opened with a swipe instead of button
104
+        //   }
105
+        // });
106 106
         return;
107 107
       case 'after-login':
108 108
         Navigation.startTabBasedApp({

+ 0
- 1
example-redux/src/screens/BottomTabsSideMenu.js Parādīt failu

@@ -69,7 +69,6 @@ class SideMenu extends Component {
69 69
   }
70 70
 
71 71
   onPushScreenToSecondTab() {
72
-    console.log('SideMenu', 'onPushScreenToSecondTab ' + 'tab2/pushScreen/example.PushedScreen');
73 72
     this.props.navigator.handleDeepLink({
74 73
       link: 'tab2/pushScreen/example.PushedScreen'
75 74
     });

+ 8
- 0
example-redux/src/screens/SecondTabScreen.js Parādīt failu

@@ -41,6 +41,10 @@ class SecondTabScreen extends Component {
41 41
             <Text style={styles.button}>Increment Counter</Text>
42 42
           </TouchableOpacity>
43 43
 
44
+          <TouchableOpacity onPress={ this.onSelectFirstTabPress.bind(this) }>
45
+            <Text style={styles.button}>Select First Tab</Text>
46
+          </TouchableOpacity>
47
+
44 48
           <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
45 49
           <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
46 50
           <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
@@ -60,6 +64,10 @@ class SecondTabScreen extends Component {
60 64
     this.props.dispatch(counterActions.increment());
61 65
   }
62 66
 
67
+  onSelectFirstTabPress() {
68
+    this.props.navigator.switchToTab()
69
+  }
70
+
63 71
   onSetButton() {
64 72
     this.props.navigator.setButtons({
65 73
       rightButtons: [

+ 1
- 4
src/deprecated/platformSpecificDeprecated.android.js Parādīt failu

@@ -243,10 +243,7 @@ function navigatorSetTitle(navigator, params) {
243 243
 }
244 244
 
245 245
 function navigatorSwitchToTab(navigator, params) {
246
-  //RctActivity.switchToTab({
247
-  //  navigatorID: navigator.navigatorID,
248
-  //  tabIndex: params.tabIndex
249
-  //});
246
+  newPlatformSpecific.selectBottomTab(navigator.navigatorID, params.tabIndex);
250 247
 }
251 248
 
252 249
 function navigatorToggleDrawer(navigator, params) {

+ 6
- 1
src/platformSpecific.android.js Parādīt failu

@@ -92,6 +92,10 @@ function setSideMenuVisible(animated, visible) {
92 92
   NativeReactModule.setSideMenuVisible(animated, visible);
93 93
 }
94 94
 
95
+function selectBottomTab(navigatorId, index) {
96
+  NativeReactModule.selectBottomTab(navigatorId, index);
97
+}
98
+
95 99
 module.exports = {
96 100
   startApp,
97 101
   push,
@@ -106,5 +110,6 @@ module.exports = {
106 110
   dismissTopModal,
107 111
   dismissAllModals,
108 112
   toggleSideMenuVisible,
109
-  setSideMenuVisible
113
+  setSideMenuVisible,
114
+  selectBottomTab
110 115
 };