Просмотр исходного кода

Make tab bar selected event only once (if tab index actually changed) (#664)

Ran 8 лет назад
Родитель
Сommit
beef28d0c3
1 измененных файлов: 82 добавлений и 80 удалений
  1. 82
    80
      ios/RCCTabBarController.m

+ 82
- 80
ios/RCCTabBarController.m Просмотреть файл

@@ -20,8 +20,10 @@
20 20
     [[[RCCManager sharedInstance].getBridge uiManager] configureNextLayoutAnimation:nil withCallback:^(NSArray* arr){} errorCallback:^(NSArray* arr){}];
21 21
   });
22 22
   
23
-  [RCCTabBarController sendScreenTabChangedEvent:viewController];
24
-  
23
+  if (tabBarController.selectedIndex != [tabBarController.viewControllers indexOfObject:viewController]) {
24
+    [RCCTabBarController sendScreenTabChangedEvent:viewController];
25
+  }
26
+
25 27
   return YES;
26 28
 }
27 29
 
@@ -45,11 +47,11 @@
45 47
 {
46 48
   self = [super init];
47 49
   if (!self) return nil;
48
-  
50
+
49 51
   self.delegate = self;
50
-  
52
+
51 53
   self.tabBar.translucent = YES; // default
52
-  
54
+
53 55
   UIColor *buttonColor = nil;
54 56
   UIColor *selectedButtonColor = nil;
55 57
   NSDictionary *tabsStyle = props[@"style"];
@@ -63,7 +65,7 @@
63 65
       buttonColor = color;
64 66
       selectedButtonColor = color;
65 67
     }
66
-    
68
+
67 69
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
68 70
     if (tabBarSelectedButtonColor)
69 71
     {
@@ -71,7 +73,7 @@
71 73
       self.tabBar.tintColor = color;
72 74
       selectedButtonColor = color;
73 75
     }
74
-    
76
+
75 77
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
76 78
     if (tabBarBackgroundColor)
77 79
     {
@@ -116,19 +118,19 @@
116 118
     viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
117 119
     viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
118 120
     viewController.tabBarItem.selectedImage = iconImageSelected;
119
-    
121
+
120 122
     if (buttonColor)
121 123
     {
122 124
       [viewController.tabBarItem setTitleTextAttributes:
123 125
        @{NSForegroundColorAttributeName : buttonColor} forState:UIControlStateNormal];
124 126
     }
125
-    
127
+
126 128
     if (selectedButtonColor)
127 129
     {
128 130
       [viewController.tabBarItem setTitleTextAttributes:
129 131
        @{NSForegroundColorAttributeName : selectedButtonColor} forState:UIControlStateSelected];
130 132
     }
131
-    
133
+
132 134
     // create badge
133 135
     NSObject *badge = tabItemLayout[@"props"][@"badge"];
134 136
     if (badge == nil || [badge isEqual:[NSNull null]])
@@ -151,92 +153,92 @@
151 153
 
152 154
 - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
153 155
 {
154
-    if ([performAction isEqualToString:@"setBadge"])
156
+  if ([performAction isEqualToString:@"setBadge"])
157
+  {
158
+    UIViewController *viewController = nil;
159
+    NSNumber *tabIndex = actionParams[@"tabIndex"];
160
+    if (tabIndex)
155 161
     {
156
-      UIViewController *viewController = nil;
157
-      NSNumber *tabIndex = actionParams[@"tabIndex"];
158
-      if (tabIndex)
159
-      {
160
-        int i = (int)[tabIndex integerValue];
161
-      
162
-        if ([self.viewControllers count] > i)
163
-        {
164
-          viewController = [self.viewControllers objectAtIndex:i];
165
-        }
166
-      }
167
-      NSString *contentId = actionParams[@"contentId"];
168
-      NSString *contentType = actionParams[@"contentType"];
169
-      if (contentId && contentType)
170
-      {
171
-        viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
172
-      }
173
-      
174
-      if (viewController)
162
+      int i = (int)[tabIndex integerValue];
163
+
164
+      if ([self.viewControllers count] > i)
175 165
       {
176
-        NSObject *badge = actionParams[@"badge"];
177
-        
178
-        if (badge == nil || [badge isEqual:[NSNull null]])
179
-        {
180
-          viewController.tabBarItem.badgeValue = nil;
181
-        }
182
-        else
183
-        {
184
-          viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
185
-        }
166
+        viewController = [self.viewControllers objectAtIndex:i];
186 167
       }
187 168
     }
188
-  
189
-    if ([performAction isEqualToString:@"switchTo"])
169
+    NSString *contentId = actionParams[@"contentId"];
170
+    NSString *contentType = actionParams[@"contentType"];
171
+    if (contentId && contentType)
172
+    {
173
+      viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
174
+    }
175
+
176
+    if (viewController)
190 177
     {
191
-      UIViewController *viewController = nil;
192
-      NSNumber *tabIndex = actionParams[@"tabIndex"];
193
-      if (tabIndex)
178
+      NSObject *badge = actionParams[@"badge"];
179
+
180
+      if (badge == nil || [badge isEqual:[NSNull null]])
194 181
       {
195
-        int i = (int)[tabIndex integerValue];
196
-      
197
-        if ([self.viewControllers count] > i)
198
-        {
199
-          viewController = [self.viewControllers objectAtIndex:i];
200
-        }
182
+        viewController.tabBarItem.badgeValue = nil;
201 183
       }
202
-      NSString *contentId = actionParams[@"contentId"];
203
-      NSString *contentType = actionParams[@"contentType"];
204
-      if (contentId && contentType)
184
+      else
205 185
       {
206
-        viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
186
+        viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
207 187
       }
208
-    
209
-      if (viewController)
188
+    }
189
+  }
190
+
191
+  if ([performAction isEqualToString:@"switchTo"])
192
+  {
193
+    UIViewController *viewController = nil;
194
+    NSNumber *tabIndex = actionParams[@"tabIndex"];
195
+    if (tabIndex)
196
+    {
197
+      int i = (int)[tabIndex integerValue];
198
+
199
+      if ([self.viewControllers count] > i)
210 200
       {
211
-        [self setSelectedViewController:viewController];
201
+        viewController = [self.viewControllers objectAtIndex:i];
212 202
       }
213 203
     }
214
-
215
-    if ([performAction isEqualToString:@"setTabBarHidden"])
204
+    NSString *contentId = actionParams[@"contentId"];
205
+    NSString *contentType = actionParams[@"contentType"];
206
+    if (contentId && contentType)
216 207
     {
217
-        BOOL hidden = [actionParams[@"hidden"] boolValue];
218
-        [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
219
-                              delay: 0
220
-             usingSpringWithDamping: 0.75
221
-              initialSpringVelocity: 0
222
-                            options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
223
-                         animations:^()
224
-         {
225
-             self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
226
-         }
227
-                         completion:^(BOOL finished)
228
-        {
229
-            if (completion != nil)
230
-            {
231
-                completion();
232
-            }
233
-        }];
234
-        return;
208
+      viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
235 209
     }
236
-    else if (completion != nil)
210
+
211
+    if (viewController)
237 212
     {
238
-      completion();
213
+      [self setSelectedViewController:viewController];
239 214
     }
215
+  }
216
+
217
+  if ([performAction isEqualToString:@"setTabBarHidden"])
218
+  {
219
+    BOOL hidden = [actionParams[@"hidden"] boolValue];
220
+    [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
221
+                          delay: 0
222
+         usingSpringWithDamping: 0.75
223
+          initialSpringVelocity: 0
224
+                        options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
225
+                     animations:^()
226
+     {
227
+       self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
228
+     }
229
+                     completion:^(BOOL finished)
230
+     {
231
+       if (completion != nil)
232
+       {
233
+         completion();
234
+       }
235
+     }];
236
+    return;
237
+  }
238
+  else if (completion != nil)
239
+  {
240
+    completion();
241
+  }
240 242
 }
241 243
 
242 244
 +(void)sendScreenTabChangedEvent:(UIViewController*)viewController {