react-native-navigation的迁移库

RNNControllerFactory.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = [self createOptions:node.data[@"options"]];
  75. NSString* componentId = node.nodeId;
  76. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:NO];
  77. if (!component.isCustomViewController) {
  78. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  79. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  80. }
  81. return component;
  82. }
  83. - (UIViewController<RNNRootViewProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  84. NSString* name = node.data[@"name"];
  85. NSDictionary* props = node.data[@"passProps"];
  86. UIViewController* externalVC = [_store getExternalComponent:name props:props bridge:_bridge];
  87. RNNNavigationOptions* options = [self createOptions:node.data[@"options"]];
  88. NSString* componentId = node.nodeId;
  89. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:YES];
  90. [component addChildViewController:externalVC];
  91. [component.view addSubview:externalVC.view];
  92. [externalVC didMoveToParentViewController:component];
  93. return component;
  94. }
  95. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  96. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  97. [vc setComponentId:node.nodeId];
  98. RNNNavigationOptions* options = [self createOptions:node.data[@"options"]];
  99. NSMutableArray* controllers = [NSMutableArray new];
  100. for (NSDictionary* child in node.children) {
  101. [controllers addObject:[self fromTree:child]];
  102. }
  103. [vc setViewControllers:controllers];
  104. [vc mergeOptions:options];
  105. return vc;
  106. }
  107. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  108. RNNTabBarController* vc = [[RNNTabBarController alloc] initWithEventEmitter:_eventEmitter];
  109. RNNNavigationOptions* options = [self createOptions:node.data[@"options"]];
  110. NSMutableArray* controllers = [NSMutableArray new];
  111. for (NSDictionary *child in node.children) {
  112. UIViewController* childVc = (UIViewController*)[self fromTree:child];
  113. RNNRootViewController* rootView = (RNNRootViewController *)childVc.childViewControllers.firstObject;
  114. [rootView applyTabBarItem];
  115. [controllers addObject:childVc];
  116. }
  117. [vc setViewControllers:controllers];
  118. [vc mergeOptions:options];
  119. return vc;
  120. }
  121. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  122. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  123. NSMutableArray* controllers = [NSMutableArray new];
  124. for (NSDictionary *child in node.children) {
  125. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  126. childVc.topTabsViewController = vc;
  127. [controllers addObject:childVc];
  128. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  129. }
  130. [vc setViewControllers:controllers];
  131. return vc;
  132. }
  133. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  134. NSMutableArray* childrenVCs = [NSMutableArray new];
  135. for (NSDictionary *child in node.children) {
  136. UIViewController *vc = [self fromTree:child];
  137. [childrenVCs addObject:vc];
  138. }
  139. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  140. [sideMenu mergeOptions:[self createOptions:node.data[@"options"]]];
  141. return sideMenu;
  142. }
  143. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  144. UIViewController<RNNRootViewProtocol>* child = [self fromTree:node.children[0]];
  145. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  146. return sideMenuChild;
  147. }
  148. - (UIViewController<RNNRootViewProtocol> *)createOverlay:(NSDictionary*)layout {
  149. UIViewController<RNNRootViewProtocol> *vc = [self fromTree:layout];
  150. __block RCTRootView* rootView = (RCTRootView*)vc.view;
  151. [vc performOnRotation:^{
  152. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  153. [_bridge.uiManager setSize:availableSize forView:rootView];
  154. }];
  155. rootView.backgroundColor = [UIColor clearColor];
  156. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  157. rootView.frame = CGRectMake(0, 0, availableSize.width, availableSize.height);
  158. [_bridge.uiManager setAvailableSize:availableSize forRootView:vc.view];
  159. return vc;
  160. }
  161. - (UIViewController<RNNRootViewProtocol> *)createSplitView:(RNNLayoutNode*)node {
  162. NSString* componentId = node.nodeId;
  163. RNNSplitViewOptions* options = [[RNNSplitViewOptions alloc] initWithDict:_defaultOptionsDict];
  164. [options mergeWith:node.data[@"options"]];
  165. RNNSplitViewController* svc = [[RNNSplitViewController alloc] initWithOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter];
  166. // We need two children of the node for successful Master / Detail
  167. NSDictionary *master = node.children[0];
  168. NSDictionary *detail = node.children[1];
  169. // Create view controllers
  170. RNNRootViewController* masterVc = (RNNRootViewController*)[self fromTree:master];
  171. RNNRootViewController* detailVc = (RNNRootViewController*)[self fromTree:detail];
  172. // Set the controllers and delegate to masterVC
  173. svc.viewControllers = [NSArray arrayWithObjects:masterVc, detailVc, nil];
  174. svc.delegate = masterVc;
  175. return svc;
  176. }
  177. - (RNNNavigationOptions *)createOptions:(NSDictionary *)optionsDict {
  178. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
  179. options.defaultOptions = [[RNNNavigationOptions alloc] initWithDict:_defaultOptionsDict];
  180. return options;
  181. }
  182. @end