react-native-navigation的迁移库

RNNControllerFactory.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. NSDictionary* options = node.data[@"options"];
  77. NSMutableArray* controllers = [NSMutableArray new];
  78. for (NSDictionary* child in node.children) {
  79. [controllers addObject:[self fromTree:child]];
  80. }
  81. [vc setViewControllers:controllers];
  82. [vc mergeOptions:options];
  83. return vc;
  84. }
  85. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  86. RNNTabBarController* vc = [[RNNTabBarController alloc] init];
  87. NSDictionary* options = node.data[@"options"];
  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. [vc mergeOptions:options];
  97. return vc;
  98. }
  99. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  100. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  101. NSMutableArray* controllers = [NSMutableArray new];
  102. for (NSDictionary *child in node.children) {
  103. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  104. childVc.topTabsViewController = vc;
  105. [controllers addObject:childVc];
  106. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  107. }
  108. [vc setViewControllers:controllers];
  109. return vc;
  110. }
  111. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  112. NSMutableArray* childrenVCs = [NSMutableArray new];
  113. for (NSDictionary *child in node.children) {
  114. UIViewController *vc = [self fromTree:child];
  115. [childrenVCs addObject:vc];
  116. }
  117. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  118. return sideMenu;
  119. }
  120. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  121. UIViewController* child = (UIViewController*)[self fromTree:node.children[0]];
  122. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  123. return sideMenuChild;
  124. }
  125. - (UIViewController<RNNRootViewProtocol> *)createOverlay:(NSDictionary*)layout {
  126. UIViewController<RNNRootViewProtocol> *vc = [self fromTree:layout];
  127. RCTRootView* rootView = (RCTRootView*)vc.view;
  128. rootView.backgroundColor = [UIColor clearColor];
  129. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  130. rootView.frame = CGRectMake(0, 0, availableSize.width, availableSize.height);
  131. [_bridge.uiManager setAvailableSize:availableSize forRootView:vc.view];
  132. return vc;
  133. }
  134. @end