Browse Source

Bottom tab selected event (#991)

* Global bottomTabSelected event

* ios - add bottomTabSelected and bottomTabDeselected events
Guy Carmeli 7 years ago
parent
commit
a866eaac2e

+ 15
- 4
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

348
             sendTabReselectedEventToJs();
348
             sendTabReselectedEventToJs();
349
             return false;
349
             return false;
350
         }
350
         }
351
-        
351
+
352
+        final int unselectedTabIndex = currentStackIndex;
352
         hideCurrentStack();
353
         hideCurrentStack();
353
         showNewStack(position);
354
         showNewStack(position);
354
         EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
355
         EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
355
-        sendTabSelectedEventToJs();
356
+        sendTabSelectedEventToJs(position, unselectedTabIndex);
356
         return true;
357
         return true;
357
     }
358
     }
358
 
359
 
359
-    private void sendTabSelectedEventToJs() {
360
-        WritableMap data = Arguments.createMap();
360
+    private void sendTabSelectedEventToJs(int selectedTabIndex, int unselectedTabIndex) {
361
         String navigatorEventId = getCurrentScreenStack().peek().getNavigatorEventId();
361
         String navigatorEventId = getCurrentScreenStack().peek().getNavigatorEventId();
362
+        WritableMap data = createTabSelectedEventData(selectedTabIndex, unselectedTabIndex);
362
         NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("bottomTabSelected", navigatorEventId, data);
363
         NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("bottomTabSelected", navigatorEventId, data);
364
+
365
+        data = createTabSelectedEventData(selectedTabIndex, unselectedTabIndex);
366
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("bottomTabSelected", data);
367
+    }
368
+
369
+    private WritableMap createTabSelectedEventData(int selectedTabIndex, int unselectedTabIndex) {
370
+        WritableMap data = Arguments.createMap();
371
+        data.putInt("selectedTabIndex", selectedTabIndex);
372
+        data.putInt("unselectedTabIndex", unselectedTabIndex);
373
+        return data;
363
     }
374
     }
364
 
375
 
365
     private void sendTabReselectedEventToJs() {
376
     private void sendTabReselectedEventToJs() {

+ 53
- 35
ios/RCCTabBarController.m View File

28
   });
28
   });
29
   
29
   
30
   if (tabBarController.selectedIndex != [tabBarController.viewControllers indexOfObject:viewController]) {
30
   if (tabBarController.selectedIndex != [tabBarController.viewControllers indexOfObject:viewController]) {
31
-    [RCCTabBarController sendScreenTabChangedEvent:viewController];
31
+    NSDictionary *body = @{
32
+                           @"selectedTabIndex": @([tabBarController.viewControllers indexOfObject:viewController]),
33
+                           @"unselectedTabIndex": @(tabBarController.selectedIndex)
34
+                           };
35
+    [RCCTabBarController sendScreenTabChangedEvent:viewController body:body];
36
+    
37
+    [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:@"bottomTabSelected" body:body];
32
   } else {
38
   } else {
33
-    [RCCTabBarController sendScreenTabPressedEvent:viewController];
39
+    [RCCTabBarController sendScreenTabPressedEvent:viewController body:nil];
34
   }
40
   }
35
-
41
+  
42
+  
43
+  
36
   return YES;
44
   return YES;
37
 }
45
 }
38
 
46
 
56
 {
64
 {
57
   self = [super init];
65
   self = [super init];
58
   if (!self) return nil;
66
   if (!self) return nil;
59
-
67
+  
60
   self.delegate = self;
68
   self.delegate = self;
61
-
69
+  
62
   self.tabBar.translucent = YES; // default
70
   self.tabBar.translucent = YES; // default
63
-
71
+  
64
   UIColor *buttonColor = nil;
72
   UIColor *buttonColor = nil;
65
   UIColor *selectedButtonColor = nil;
73
   UIColor *selectedButtonColor = nil;
66
   NSDictionary *tabsStyle = props[@"style"];
74
   NSDictionary *tabsStyle = props[@"style"];
74
       buttonColor = color;
82
       buttonColor = color;
75
       selectedButtonColor = color;
83
       selectedButtonColor = color;
76
     }
84
     }
77
-
85
+    
78
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
86
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
79
     if (tabBarSelectedButtonColor)
87
     if (tabBarSelectedButtonColor)
80
     {
88
     {
82
       self.tabBar.tintColor = color;
90
       self.tabBar.tintColor = color;
83
       selectedButtonColor = color;
91
       selectedButtonColor = color;
84
     }
92
     }
85
-
93
+    
86
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
94
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
87
     if (tabBarBackgroundColor)
95
     if (tabBarBackgroundColor)
88
     {
96
     {
90
       self.tabBar.barTintColor = color;
98
       self.tabBar.barTintColor = color;
91
     }
99
     }
92
   }
100
   }
93
-
101
+  
94
   NSMutableArray *viewControllers = [NSMutableArray array];
102
   NSMutableArray *viewControllers = [NSMutableArray array];
95
-
103
+  
96
   // go over all the tab bar items
104
   // go over all the tab bar items
97
   for (NSDictionary *tabItemLayout in children)
105
   for (NSDictionary *tabItemLayout in children)
98
   {
106
   {
99
     // make sure the layout is valid
107
     // make sure the layout is valid
100
     if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
108
     if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
101
     if (!tabItemLayout[@"props"]) continue;
109
     if (!tabItemLayout[@"props"]) continue;
102
-
110
+    
103
     // get the view controller inside
111
     // get the view controller inside
104
     if (!tabItemLayout[@"children"]) continue;
112
     if (!tabItemLayout[@"children"]) continue;
105
     if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
113
     if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
107
     NSDictionary *childLayout = tabItemLayout[@"children"][0];
115
     NSDictionary *childLayout = tabItemLayout[@"children"][0];
108
     UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
116
     UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
109
     if (!viewController) continue;
117
     if (!viewController) continue;
110
-
118
+    
111
     // create the tab icon and title
119
     // create the tab icon and title
112
     NSString *title = tabItemLayout[@"props"][@"title"];
120
     NSString *title = tabItemLayout[@"props"][@"title"];
113
     UIImage *iconImage = nil;
121
     UIImage *iconImage = nil;
127
     } else {
135
     } else {
128
       iconImageSelected = [RCTConvert UIImage:icon];
136
       iconImageSelected = [RCTConvert UIImage:icon];
129
     }
137
     }
130
-
138
+    
131
     viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
139
     viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
132
     viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
140
     viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
133
     viewController.tabBarItem.selectedImage = iconImageSelected;
141
     viewController.tabBarItem.selectedImage = iconImageSelected;
156
     {
164
     {
157
       viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
165
       viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
158
     }
166
     }
159
-
167
+    
160
     [viewControllers addObject:viewController];
168
     [viewControllers addObject:viewController];
161
   }
169
   }
162
-
170
+  
163
   // replace the tabs
171
   // replace the tabs
164
   self.viewControllers = viewControllers;
172
   self.viewControllers = viewControllers;
165
   
173
   
166
   [self setRotation:props];
174
   [self setRotation:props];
167
-
175
+  
168
   return self;
176
   return self;
169
 }
177
 }
170
 
178
 
177
     if (tabIndex)
185
     if (tabIndex)
178
     {
186
     {
179
       int i = (int)[tabIndex integerValue];
187
       int i = (int)[tabIndex integerValue];
180
-
188
+      
181
       if ([self.viewControllers count] > i)
189
       if ([self.viewControllers count] > i)
182
       {
190
       {
183
         viewController = [self.viewControllers objectAtIndex:i];
191
         viewController = [self.viewControllers objectAtIndex:i];
189
     {
197
     {
190
       viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
198
       viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
191
     }
199
     }
192
-
200
+    
193
     if (viewController)
201
     if (viewController)
194
     {
202
     {
195
       NSObject *badge = actionParams[@"badge"];
203
       NSObject *badge = actionParams[@"badge"];
196
-
204
+      
197
       if (badge == nil || [badge isEqual:[NSNull null]])
205
       if (badge == nil || [badge isEqual:[NSNull null]])
198
       {
206
       {
199
         viewController.tabBarItem.badgeValue = nil;
207
         viewController.tabBarItem.badgeValue = nil;
204
       }
212
       }
205
     }
213
     }
206
   }
214
   }
207
-
215
+  
208
   if ([performAction isEqualToString:@"switchTo"])
216
   if ([performAction isEqualToString:@"switchTo"])
209
   {
217
   {
210
     UIViewController *viewController = nil;
218
     UIViewController *viewController = nil;
212
     if (tabIndex)
220
     if (tabIndex)
213
     {
221
     {
214
       int i = (int)[tabIndex integerValue];
222
       int i = (int)[tabIndex integerValue];
215
-
223
+      
216
       if ([self.viewControllers count] > i)
224
       if ([self.viewControllers count] > i)
217
       {
225
       {
218
         viewController = [self.viewControllers objectAtIndex:i];
226
         viewController = [self.viewControllers objectAtIndex:i];
224
     {
232
     {
225
       viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
233
       viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
226
     }
234
     }
227
-
235
+    
228
     if (viewController)
236
     if (viewController)
229
     {
237
     {
230
       [self setSelectedViewController:viewController];
238
       [self setSelectedViewController:viewController];
231
     }
239
     }
232
   }
240
   }
233
-
241
+  
234
   if ([performAction isEqualToString:@"setTabBarHidden"])
242
   if ([performAction isEqualToString:@"setTabBarHidden"])
235
   {
243
   {
236
     BOOL hidden = [actionParams[@"hidden"] boolValue];
244
     BOOL hidden = [actionParams[@"hidden"] boolValue];
258
   }
266
   }
259
 }
267
 }
260
 
268
 
261
-+(void)sendScreenTabChangedEvent:(UIViewController*)viewController {
262
-  [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController];
269
++(void)sendScreenTabChangedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
270
+  [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController body:body];
263
 }
271
 }
264
 
272
 
265
-+(void)sendScreenTabPressedEvent:(UIViewController*)viewController {
266
-  [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController];
273
++(void)sendScreenTabPressedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
274
+  [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController body:body];
267
 }
275
 }
268
 
276
 
269
-+(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController {
277
++(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController body:(NSDictionary*)body{
270
   if ([viewController.view isKindOfClass:[RCTRootView class]]){
278
   if ([viewController.view isKindOfClass:[RCTRootView class]]){
271
     RCTRootView *rootView = (RCTRootView *)viewController.view;
279
     RCTRootView *rootView = (RCTRootView *)viewController.view;
272
     
280
     
274
       NSString *navigatorID = rootView.appProperties[@"navigatorID"];
282
       NSString *navigatorID = rootView.appProperties[@"navigatorID"];
275
       NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
283
       NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
276
       
284
       
277
-      [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
278
-       {
279
-         @"id": event,
280
-         @"navigatorID": navigatorID,
281
-         @"screenInstanceID": screenInstanceID
282
-       }];
285
+      
286
+      NSMutableDictionary *screenDict = [NSMutableDictionary dictionaryWithDictionary:@
287
+                                         {
288
+                                           @"id": event,
289
+                                           @"navigatorID": navigatorID,
290
+                                           @"screenInstanceID": screenInstanceID
291
+                                         }];
292
+      
293
+      
294
+      if (body) {
295
+        [screenDict addEntriesFromDictionary:body];
296
+      }
297
+      
298
+      [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:screenDict];
283
     }
299
     }
284
   }
300
   }
285
   
301
   
286
   if ([viewController isKindOfClass:[UINavigationController class]]) {
302
   if ([viewController isKindOfClass:[UINavigationController class]]) {
287
     UINavigationController *navigationController = (UINavigationController*)viewController;
303
     UINavigationController *navigationController = (UINavigationController*)viewController;
288
     UIViewController *topViewController = [navigationController topViewController];
304
     UIViewController *topViewController = [navigationController topViewController];
289
-    [RCCTabBarController sendTabEvent:event controller:topViewController];
305
+    [RCCTabBarController sendTabEvent:event controller:topViewController body:body];
290
   }
306
   }
291
 }
307
 }
292
 
308
 
309
+
310
+
293
 @end
311
 @end