Browse Source

Initial implementation

Guy Carmeli 8 years ago
parent
commit
57deff049c

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

76
     }
76
     }
77
 
77
 
78
     @ReactMethod
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
     @ReactMethod
83
     @ReactMethod

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

212
     public void setSideMenuVisible(boolean animated, boolean visible) {
212
     public void setSideMenuVisible(boolean animated, boolean visible) {
213
         layout.setSideMenuVisible(animated, visible);
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 View File

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 View File

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
     @Override
175
     @Override
168
     public void push(ScreenParams screenParams) {
176
     public void push(ScreenParams screenParams) {
169
         ScreenStack screenStack = getScreenStack(screenParams.getNavigatorId());
177
         ScreenStack screenStack = getScreenStack(screenParams.getNavigatorId());
170
-        if (screenStack == null) {
171
-            return;
172
-        }
173
-
178
+        screenStack.push(screenParams, createScreenLayoutParams(screenParams));
174
         if (isCurrentStack(screenStack)) {
179
         if (isCurrentStack(screenStack)) {
175
-            screenStack.push(screenParams, createScreenLayoutParams(screenParams));
176
             bottomTabs.setStyleFromScreen(screenParams.styleParams);
180
             bottomTabs.setStyleFromScreen(screenParams.styleParams);
177
-        } else {
178
-            screenStack.push(screenParams, createScreenLayoutParams(screenParams));
179
         }
181
         }
180
     }
182
     }
181
 
183
 
245
         return screenStacks[currentStackIndex];
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
     private boolean isCurrentStack(ScreenStack screenStack) {
270
     private boolean isCurrentStack(ScreenStack screenStack) {

+ 66
- 66
example-redux/src/app.js View File

35
   startApp(root) {
35
   startApp(root) {
36
     switch (root) {
36
     switch (root) {
37
       case 'login':
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
         Navigation.startSingleScreenApp({
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
         return;
106
         return;
107
       case 'after-login':
107
       case 'after-login':
108
         Navigation.startTabBasedApp({
108
         Navigation.startTabBasedApp({

+ 0
- 1
example-redux/src/screens/BottomTabsSideMenu.js View File

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

+ 8
- 0
example-redux/src/screens/SecondTabScreen.js View File

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

+ 1
- 4
src/deprecated/platformSpecificDeprecated.android.js View File

243
 }
243
 }
244
 
244
 
245
 function navigatorSwitchToTab(navigator, params) {
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
 function navigatorToggleDrawer(navigator, params) {
249
 function navigatorToggleDrawer(navigator, params) {

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

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