Browse Source

[iOS] Allow set view controllers to be called with multiple components (#2247)

* Refactor resetTo view controller creation into function

This will allow for multiple view controllers to call the same function to create their instance.

* Configure resetTo to take multiple components to set view controllers for

This allows for a navigation stack to be recreated with multiple view controllers rather than just one.

* Breakup view controller initializer into multiple lines for legibility
Eli Perkins 6 years ago
parent
commit
3240f8eaf6
1 changed files with 77 additions and 27 deletions
  1. 77
    27
      ios/RCCNavigationController.m

+ 77
- 27
ios/RCCNavigationController.m View File

255
   // resetTo
255
   // resetTo
256
   if ([performAction isEqualToString:@"resetTo"])
256
   if ([performAction isEqualToString:@"resetTo"])
257
   {
257
   {
258
+    NSArray<UIViewController *> *viewControllers;
259
+
258
     NSString *component = actionParams[@"component"];
260
     NSString *component = actionParams[@"component"];
259
-    if (!component) return;
260
-    
261
-    NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
262
-    passProps[@"commandType"] = @"resetTo";
263
-    NSDictionary *navigatorStyle = actionParams[@"style"];
264
-    
265
-    RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
266
-    viewController.controllerId = passProps[@"screenInstanceID"];
267
-    
268
-    viewController.navigationItem.hidesBackButton = YES;
269
-    
270
-    [self processTitleView:viewController
271
-                     props:actionParams
272
-                     style:navigatorStyle];
273
-    NSArray *leftButtons = actionParams[@"leftButtons"];
274
-    if (leftButtons)
275
-    {
276
-      [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
277
-    }
278
-    
279
-    NSArray *rightButtons = actionParams[@"rightButtons"];
280
-    if (rightButtons)
281
-    {
282
-      [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
261
+    NSArray<NSDictionary *> *componentConfigs = actionParams[@"components"];
262
+    if (component) {
263
+      NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
264
+      passProps[@"commandType"] = @"resetTo";
265
+      NSDictionary *style = actionParams[@"style"];
266
+      NSArray *leftButtons = actionParams[@"leftButtons"];
267
+      NSArray *rightButtons = actionParams[@"rightButtons"];
268
+
269
+      UIViewController *viewController = [self viewControllerWithComponent:component
270
+                                                                     props:[passProps copy]
271
+                                                                     style:style
272
+                                                               leftButtons:leftButtons
273
+                                                              rightButtons:rightButtons
274
+                                                                    bridge:bridge];
275
+
276
+      viewControllers = @[viewController];
277
+    } else if (componentConfigs) {
278
+      NSMutableArray *mutableViewControllers = [NSMutableArray arrayWithCapacity:[componentConfigs count]];
279
+      [componentConfigs enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
280
+        NSString *component = config[@"component"];
281
+
282
+        NSMutableDictionary *props = [config[@"passProps"] mutableCopy];
283
+        props[@"commandType"] = @"resetTo";
284
+        NSDictionary *style = config[@"style"];
285
+        NSArray *leftButtons = config[@"leftButtons"];
286
+        NSArray *rightButtons = config[@"rightButtons"];
287
+
288
+        UIViewController *viewController = [self viewControllerWithComponent:component
289
+                                                                       props:[props copy]
290
+                                                                       style:style
291
+                                                                 leftButtons:leftButtons
292
+                                                                rightButtons:rightButtons
293
+                                                                      bridge:bridge];
294
+
295
+        [mutableViewControllers addObject:viewController];
296
+      }];
297
+      viewControllers = [mutableViewControllers copy];
283
     }
298
     }
284
-    
299
+
300
+    if (!viewControllers) return;
301
+
285
     BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
302
     BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
286
     
303
     
287
     NSString *animationType = actionParams[@"animationType"];
304
     NSString *animationType = actionParams[@"animationType"];
292
       transition.type = kCATransitionFade;
309
       transition.type = kCATransitionFade;
293
       
310
       
294
       [self.view.layer addAnimation:transition forKey:kCATransition];
311
       [self.view.layer addAnimation:transition forKey:kCATransition];
295
-      [self setViewControllers:@[viewController] animated:NO];
312
+      [self setViewControllers:viewControllers animated:NO];
296
     }
313
     }
297
     else
314
     else
298
     {
315
     {
299
-      [self setViewControllers:@[viewController] animated:animated];
316
+      [self setViewControllers:viewControllers animated:animated];
300
     }
317
     }
301
     return;
318
     return;
302
   }
319
   }
360
   }
377
   }
361
 }
378
 }
362
 
379
 
380
+- (UIViewController *)viewControllerWithComponent:(NSString *)component
381
+                                            props:(NSDictionary *)props
382
+                                            style:(NSDictionary *)style
383
+                                      leftButtons:(NSArray<NSDictionary *> *)leftButtons
384
+                                     rightButtons:(NSArray<NSDictionary *> *)rightButtons
385
+                                           bridge:(RCTBridge *)bridge
386
+{
387
+  RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component
388
+                                                                         passProps:props
389
+                                                                    navigatorStyle:style
390
+                                                                       globalProps:nil
391
+                                                                            bridge:bridge];
392
+  viewController.controllerId = props[@"screenInstanceID"];
393
+
394
+  viewController.navigationItem.hidesBackButton = YES;
395
+
396
+  [self processTitleView:viewController
397
+                   props:props
398
+                   style:style];
399
+
400
+  if (leftButtons)
401
+  {
402
+    [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
403
+  }
404
+
405
+  if (rightButtons)
406
+  {
407
+    [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
408
+  }
409
+
410
+  return viewController;
411
+}
412
+
363
 -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
413
 -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
364
 {
414
 {
365
   NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
415
   NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);