Kaynağa Gözat

Bottom tab selected event (#991)

* Global bottomTabSelected event

* ios - add bottomTabSelected and bottomTabDeselected events
Guy Carmeli 7 yıl önce
ebeveyn
işleme
a866eaac2e

+ 15
- 4
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Dosyayı Görüntüle

@@ -348,18 +348,29 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
348 348
             sendTabReselectedEventToJs();
349 349
             return false;
350 350
         }
351
-        
351
+
352
+        final int unselectedTabIndex = currentStackIndex;
352 353
         hideCurrentStack();
353 354
         showNewStack(position);
354 355
         EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
355
-        sendTabSelectedEventToJs();
356
+        sendTabSelectedEventToJs(position, unselectedTabIndex);
356 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 361
         String navigatorEventId = getCurrentScreenStack().peek().getNavigatorEventId();
362
+        WritableMap data = createTabSelectedEventData(selectedTabIndex, unselectedTabIndex);
362 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 376
     private void sendTabReselectedEventToJs() {

+ 53
- 35
ios/RCCTabBarController.m Dosyayı Görüntüle

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