react-native-navigation的迁移库

RNNControllerFactory.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. UIViewController* externalVC = [_store getExternalComponent:name];
  82. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:_defaultOptionsDict];
  83. [options mergeWith:node.data[@"options"]];
  84. NSString* componentId = node.nodeId;
  85. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:YES];
  86. [component addChildViewController:externalVC];
  87. component.view = [[UIView alloc] init];
  88. component.view.backgroundColor = [UIColor whiteColor];
  89. [component.view addSubview:externalVC.view];
  90. return component;
  91. }
  92. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  93. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  94. NSDictionary* options = node.data[@"options"];
  95. NSMutableArray* controllers = [NSMutableArray new];
  96. for (NSDictionary* child in node.children) {
  97. [controllers addObject:[self fromTree:child]];
  98. }
  99. [vc setViewControllers:controllers];
  100. [vc mergeOptions:options];
  101. return vc;
  102. }
  103. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  104. RNNTabBarController* vc = [[RNNTabBarController alloc] init];
  105. NSDictionary* options = node.data[@"options"];
  106. NSMutableArray* controllers = [NSMutableArray new];
  107. for (NSDictionary *child in node.children) {
  108. UIViewController* childVc = (UIViewController*)[self fromTree:child];
  109. RNNRootViewController* rootView = (RNNRootViewController *)childVc.childViewControllers.firstObject;
  110. [rootView applyTabBarItem];
  111. [controllers addObject:childVc];
  112. }
  113. [vc setViewControllers:controllers];
  114. [vc mergeOptions:options];
  115. return vc;
  116. }
  117. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  118. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  119. NSMutableArray* controllers = [NSMutableArray new];
  120. for (NSDictionary *child in node.children) {
  121. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  122. childVc.topTabsViewController = vc;
  123. [controllers addObject:childVc];
  124. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  125. }
  126. [vc setViewControllers:controllers];
  127. return vc;
  128. }
  129. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  130. NSMutableArray* childrenVCs = [NSMutableArray new];
  131. for (NSDictionary *child in node.children) {
  132. UIViewController *vc = [self fromTree:child];
  133. [childrenVCs addObject:vc];
  134. }
  135. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  136. return sideMenu;
  137. }
  138. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  139. UIViewController* child = (UIViewController*)[self fromTree:node.children[0]];
  140. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  141. return sideMenuChild;
  142. }
  143. - (UIViewController<RNNRootViewProtocol> *)createOverlay:(NSDictionary*)layout {
  144. UIViewController<RNNRootViewProtocol> *vc = [self fromTree:layout];
  145. RCTRootView* rootView = (RCTRootView*)vc.view;
  146. rootView.backgroundColor = [UIColor clearColor];
  147. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  148. rootView.frame = CGRectMake(0, 0, availableSize.width, availableSize.height);
  149. [_bridge.uiManager setAvailableSize:availableSize forRootView:vc.view];
  150. return vc;
  151. }
  152. @end