react-native-navigation的迁移库

RNNControllerFactory.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. RNNEventEmitter *_eventEmitter;
  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. if (!result) {
  60. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  61. }
  62. [_store setComponent:result componentId:node.nodeId];
  63. return result;
  64. }
  65. - (UIViewController<RNNRootViewProtocol> *)createComponent:(RNNLayoutNode*)node {
  66. NSString* name = node.data[@"name"];
  67. NSDictionary* customTransition = node.data[@"customTransition"];
  68. RNNAnimator* animator = [[RNNAnimator alloc] initWithAnimationsDictionary:customTransition];
  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 animator:animator];
  73. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  74. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  75. return component;
  76. }
  77. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  78. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  79. NSMutableArray* controllers = [NSMutableArray new];
  80. for (NSDictionary* child in node.children) {
  81. [controllers addObject:[self fromTree:child]];
  82. }
  83. [vc setViewControllers:controllers];
  84. return vc;
  85. }
  86. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  87. RNNTabBarController* vc = [[RNNTabBarController alloc] init];
  88. NSMutableArray* controllers = [NSMutableArray new];
  89. for (NSDictionary *child in node.children) {
  90. UIViewController* childVc = (UIViewController*)[self fromTree:child];
  91. RNNRootViewController* rootView = (RNNRootViewController *)childVc.childViewControllers.firstObject;
  92. [rootView applyTabBarItem];
  93. [controllers addObject:childVc];
  94. }
  95. [vc setViewControllers:controllers];
  96. return vc;
  97. }
  98. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  99. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  100. NSMutableArray* controllers = [NSMutableArray new];
  101. for (NSDictionary *child in node.children) {
  102. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  103. childVc.topTabsViewController = vc;
  104. [controllers addObject:childVc];
  105. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  106. }
  107. [vc setViewControllers:controllers];
  108. return vc;
  109. }
  110. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  111. NSMutableArray* childrenVCs = [NSMutableArray new];
  112. for (NSDictionary *child in node.children) {
  113. UIViewController *vc = [self fromTree:child];
  114. [childrenVCs addObject:vc];
  115. }
  116. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  117. return sideMenu;
  118. }
  119. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  120. UIViewController* child = (UIViewController*)[self fromTree:node.children[0]];
  121. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  122. return sideMenuChild;
  123. }
  124. @end