react-native-navigation的迁移库

RNNControllerFactory.m 5.6KB

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