react-native-navigation的迁移库

RNNControllerFactory.m 7.7KB

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