react-native-navigation的迁移库

RNNControllerFactory.m 5.3KB

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