react-native-navigation的迁移库

RNNControllerFactory.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #import "RNNControllerFactory.h"
  2. #import "RNNLayoutNode.h"
  3. #import "RNNRootViewController.h"
  4. #import "RNNSideMenuController.h"
  5. #import "RNNSideMenuChildVC.h"
  6. #import "RNNNavigationOptions.h"
  7. #import "RNNNavigationController.h"
  8. #import "RNNTabBarController.h"
  9. #import "RNNTopTabsViewController.h"
  10. @implementation RNNControllerFactory {
  11. id<RNNRootViewCreator> _creator;
  12. RNNStore *_store;
  13. RCTBridge *_bridge;
  14. }
  15. # pragma mark public
  16. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  17. store:(RNNStore *)store
  18. eventEmitter:(RNNEventEmitter*)eventEmitter
  19. andBridge:(RCTBridge *)bridge {
  20. self = [super init];
  21. _creator = creator;
  22. _store = store;
  23. _eventEmitter = eventEmitter;
  24. _bridge = bridge;
  25. return self;
  26. }
  27. - (UIViewController<RNNRootViewProtocol> *)createLayoutAndSaveToStore:(NSDictionary*)layout {
  28. return [self fromTree:layout];
  29. }
  30. # pragma mark private
  31. - (UIViewController<RNNRootViewProtocol> *)fromTree:(NSDictionary*)json {
  32. RNNLayoutNode* node = [RNNLayoutNode create:json];
  33. UIViewController<RNNRootViewProtocol> *result;
  34. if (node.isComponent) {
  35. result = [self createComponent:node];
  36. }
  37. else if (node.isStack) {
  38. result = [self createStack:node];
  39. }
  40. else if (node.isTabs) {
  41. result = [self createTabs:node];
  42. }
  43. else if (node.isTopTabs) {
  44. result = [self createTopTabs:node];
  45. }
  46. else if (node.isSideMenuRoot) {
  47. result = [self createSideMenu:node];
  48. }
  49. else if (node.isSideMenuCenter) {
  50. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  51. }
  52. else if (node.isSideMenuLeft) {
  53. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  54. }
  55. else if (node.isSideMenuRight) {
  56. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  57. }
  58. else if (node.isExternalComponent) {
  59. result = [self createExternalComponent:node];
  60. }
  61. if (!result) {
  62. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  63. }
  64. [_store setComponent:result componentId:node.nodeId];
  65. return result;
  66. }
  67. - (UIViewController<RNNRootViewProtocol> *)createComponent:(RNNLayoutNode*)node {
  68. NSString* name = node.data[@"name"];
  69. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:_defaultOptionsDict];
  70. [options mergeWith:node.data[@"options"]];
  71. NSString* componentId = node.nodeId;
  72. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:NO];
  73. if (!component.isCustomViewController) {
  74. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  75. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  76. }
  77. return component;
  78. }
  79. - (UIViewController<RNNRootViewProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  80. NSString* name = node.data[@"name"];
  81. NSDictionary* props = node.data[@"passProps"];
  82. UIViewController* externalVC = [_store getExternalComponent:name props:props bridge:_bridge];
  83. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:_defaultOptionsDict];
  84. [options mergeWith:node.data[@"options"]];
  85. NSString* componentId = node.nodeId;
  86. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:YES];
  87. [component addChildViewController:externalVC];
  88. [component.view addSubview:externalVC.view];
  89. [externalVC didMoveToParentViewController:component];
  90. return component;
  91. }
  92. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  93. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  94. [vc setComponentId:node.nodeId];
  95. NSDictionary* options = node.data[@"options"];
  96. NSMutableArray* controllers = [NSMutableArray new];
  97. for (NSDictionary* child in node.children) {
  98. [controllers addObject:[self fromTree:child]];
  99. }
  100. [vc setViewControllers:controllers];
  101. [vc mergeOptions:options];
  102. return vc;
  103. }
  104. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  105. RNNTabBarController* vc = [[RNNTabBarController alloc] initWithEventEmitter:_eventEmitter];
  106. NSDictionary* options = node.data[@"options"];
  107. NSMutableArray* controllers = [NSMutableArray new];
  108. for (NSDictionary *child in node.children) {
  109. UIViewController* childVc = (UIViewController*)[self fromTree:child];
  110. RNNRootViewController* rootView = (RNNRootViewController *)childVc.childViewControllers.firstObject;
  111. [rootView applyTabBarItem];
  112. [controllers addObject:childVc];
  113. }
  114. [vc setViewControllers:controllers];
  115. [vc mergeOptions:options];
  116. return vc;
  117. }
  118. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  119. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  120. NSMutableArray* controllers = [NSMutableArray new];
  121. for (NSDictionary *child in node.children) {
  122. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  123. childVc.topTabsViewController = vc;
  124. [controllers addObject:childVc];
  125. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  126. }
  127. [vc setViewControllers:controllers];
  128. return vc;
  129. }
  130. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  131. NSMutableArray* childrenVCs = [NSMutableArray new];
  132. for (NSDictionary *child in node.children) {
  133. UIViewController *vc = [self fromTree:child];
  134. [childrenVCs addObject:vc];
  135. }
  136. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  137. return sideMenu;
  138. }
  139. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  140. UIViewController* child = (UIViewController*)[self fromTree:node.children[0]];
  141. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  142. return sideMenuChild;
  143. }
  144. - (UIViewController<RNNRootViewProtocol> *)createOverlay:(NSDictionary*)layout {
  145. UIViewController<RNNRootViewProtocol> *vc = [self fromTree:layout];
  146. __block RCTRootView* rootView = (RCTRootView*)vc.view;
  147. [vc performOnRotation:^{
  148. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  149. [_bridge.uiManager setSize:availableSize forView:rootView];
  150. }];
  151. rootView.backgroundColor = [UIColor clearColor];
  152. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  153. rootView.frame = CGRectMake(0, 0, availableSize.width, availableSize.height);
  154. [_bridge.uiManager setAvailableSize:availableSize forRootView:vc.view];
  155. return vc;
  156. }
  157. @end