react-native-navigation的迁移库

RNNControllerFactory.m 8.1KB

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