react-native-navigation的迁移库

RNNControllerFactory.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. RNNNavigationOptions* options = [self createOptions:node.data[@"options"]];
  96. RNNNavigationController* vc = [[RNNNavigationController alloc] initWithOptions:options];
  97. [vc setComponentId:node.nodeId];
  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. [childVc applyTabBarItem];
  113. [controllers addObject:childVc];
  114. }
  115. [vc setViewControllers:controllers];
  116. [vc.getLeafViewController mergeOptions:options];
  117. return vc;
  118. }
  119. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  120. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  121. NSMutableArray* controllers = [NSMutableArray new];
  122. for (NSDictionary *child in node.children) {
  123. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  124. // childVc.topTabsViewController = vc;
  125. [controllers addObject:childVc];
  126. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  127. }
  128. [vc setViewControllers:controllers];
  129. return vc;
  130. }
  131. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  132. NSMutableArray* childrenVCs = [NSMutableArray new];
  133. for (NSDictionary *child in node.children) {
  134. UIViewController *vc = [self fromTree:child];
  135. [childrenVCs addObject:vc];
  136. }
  137. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  138. [sideMenu.getLeafViewController mergeOptions:[self createOptions:node.data[@"options"]]];
  139. return sideMenu;
  140. }
  141. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  142. UIViewController<RNNRootViewProtocol>* child = [self fromTree:node.children[0]];
  143. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  144. return sideMenuChild;
  145. }
  146. - (UIViewController<RNNRootViewProtocol> *)createOverlay:(NSDictionary*)layout {
  147. UIViewController<RNNRootViewProtocol> *vc = [self fromTree:layout];
  148. __block RCTRootView* rootView = (RCTRootView*)vc.view;
  149. [vc performOnRotation:^{
  150. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  151. [_bridge.uiManager setSize:availableSize forView:rootView];
  152. }];
  153. rootView.backgroundColor = [UIColor clearColor];
  154. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  155. rootView.frame = CGRectMake(0, 0, availableSize.width, availableSize.height);
  156. [_bridge.uiManager setAvailableSize:availableSize forRootView:vc.view];
  157. return vc;
  158. }
  159. - (UIViewController<RNNRootViewProtocol> *)createSplitView:(RNNLayoutNode*)node {
  160. NSString* componentId = node.nodeId;
  161. RNNSplitViewOptions* options = [[RNNSplitViewOptions alloc] initWithDict:_defaultOptionsDict];
  162. [options mergeWith:node.data[@"options"]];
  163. RNNSplitViewController* svc = [[RNNSplitViewController alloc] initWithOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter];
  164. // We need two children of the node for successful Master / Detail
  165. NSDictionary *master = node.children[0];
  166. NSDictionary *detail = node.children[1];
  167. // Create view controllers
  168. RNNRootViewController* masterVc = (RNNRootViewController*)[self fromTree:master];
  169. RNNRootViewController* detailVc = (RNNRootViewController*)[self fromTree:detail];
  170. // Set the controllers and delegate to masterVC
  171. svc.viewControllers = [NSArray arrayWithObjects:masterVc, detailVc, nil];
  172. svc.delegate = masterVc;
  173. return svc;
  174. }
  175. - (RNNNavigationOptions *)createOptions:(NSDictionary *)optionsDict {
  176. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
  177. options.defaultOptions = [[RNNNavigationOptions alloc] initWithDict:_defaultOptionsDict];
  178. return options;
  179. }
  180. @end