react-native-navigation的迁移库

RNNControllerFactory.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. NSString* componentId = node.nodeId;
  71. RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter animator:animator];
  72. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  73. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  74. return component;
  75. }
  76. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  77. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  78. NSMutableArray* controllers = [NSMutableArray new];
  79. for (NSDictionary* child in node.children) {
  80. [controllers addObject:[self fromTree:child]];
  81. }
  82. [vc setViewControllers:controllers];
  83. return vc;
  84. }
  85. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  86. RNNTabBarController* vc = [[RNNTabBarController alloc] init];
  87. NSMutableArray* controllers = [NSMutableArray new];
  88. for (NSDictionary *child in node.children) {
  89. UIViewController* childVc = (UIViewController*)[self fromTree:child];
  90. RNNRootViewController* rootView = (RNNRootViewController *)childVc.childViewControllers.firstObject;
  91. [rootView applyTabBarItem];
  92. [controllers addObject:childVc];
  93. }
  94. [vc setViewControllers:controllers];
  95. return vc;
  96. }
  97. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  98. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  99. NSMutableArray* controllers = [NSMutableArray new];
  100. for (NSDictionary *child in node.children) {
  101. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  102. childVc.topTabsViewController = vc;
  103. [controllers addObject:childVc];
  104. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  105. }
  106. [vc setViewControllers:controllers];
  107. return vc;
  108. }
  109. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  110. NSMutableArray* childrenVCs = [NSMutableArray new];
  111. for (NSDictionary *child in node.children) {
  112. UIViewController *vc = [self fromTree:child];
  113. [childrenVCs addObject:vc];
  114. }
  115. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  116. return sideMenu;
  117. }
  118. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  119. UIViewController* child = (UIViewController*)[self fromTree:node.children[0]];
  120. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  121. return sideMenuChild;
  122. }
  123. @end