react-native-navigation的迁移库

RNNControllerFactory.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #import "RNNControllerFactory.h"
  2. #import "RNNLayoutNode.h"
  3. #import "RNNSplitViewController.h"
  4. #import "RNNSplitViewOptions.h"
  5. #import "RNNSideMenuController.h"
  6. #import "RNNSideMenuChildVC.h"
  7. #import "RNNNavigationController.h"
  8. #import "RNNTabBarController.h"
  9. #import "RNNTopTabsViewController.h"
  10. #import "RNNLayoutInfo.h"
  11. #import "RNNOptionsManager.h"
  12. @implementation RNNControllerFactory {
  13. id<RNNRootViewCreator> _creator;
  14. RNNStore *_store;
  15. RCTBridge *_bridge;
  16. }
  17. # pragma mark public
  18. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  19. store:(RNNStore *)store
  20. eventEmitter:(RNNEventEmitter*)eventEmitter
  21. andBridge:(RCTBridge *)bridge {
  22. self = [super init];
  23. _creator = creator;
  24. _store = store;
  25. _eventEmitter = eventEmitter;
  26. _bridge = bridge;
  27. _optionsManager = [RNNOptionsManager new];
  28. return self;
  29. }
  30. - (UIViewController<RNNRootViewProtocol> *)createLayoutAndSaveToStore:(NSDictionary*)layout {
  31. return [self fromTree:layout];
  32. }
  33. # pragma mark private
  34. - (UIViewController<RNNRootViewProtocol> *)fromTree:(NSDictionary*)json {
  35. RNNLayoutNode* node = [RNNLayoutNode create:json];
  36. UIViewController<RNNRootViewProtocol> *result;
  37. if (node.isComponent) {
  38. result = [self createComponent:node];
  39. }
  40. else if (node.isStack) {
  41. result = [self createStack:node];
  42. }
  43. else if (node.isTabs) {
  44. result = [self createTabs:node];
  45. }
  46. else if (node.isTopTabs) {
  47. result = [self createTopTabs:node];
  48. }
  49. else if (node.isSideMenuRoot) {
  50. result = [self createSideMenu:node];
  51. }
  52. else if (node.isSideMenuCenter) {
  53. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  54. }
  55. else if (node.isSideMenuLeft) {
  56. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  57. }
  58. else if (node.isSideMenuRight) {
  59. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  60. }
  61. else if (node.isExternalComponent) {
  62. result = [self createExternalComponent:node];
  63. }
  64. else if (node.isSplitView) {
  65. result = [self createSplitView:node];
  66. }
  67. if (!result) {
  68. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  69. }
  70. [_store setComponent:result componentId:node.nodeId];
  71. return result;
  72. }
  73. - (UIViewController<RNNRootViewProtocol> *)createComponent:(RNNLayoutNode*)node {
  74. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  75. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:NO];
  76. if (!component.isCustomViewController) {
  77. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  78. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  79. }
  80. return (UIViewController<RNNRootViewProtocol> *)component;
  81. }
  82. - (UIViewController<RNNRootViewProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  83. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  84. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  85. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter isExternalComponent:YES];
  86. [component addChildViewController:externalVC];
  87. [component.view addSubview:externalVC.view];
  88. [externalVC didMoveToParentViewController:component];
  89. return (UIViewController<RNNRootViewProtocol> *)component;
  90. }
  91. - (UIViewController<RNNRootViewProtocol> *)createStack:(RNNLayoutNode*)node {
  92. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  93. RNNNavigationController* vc = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo];
  94. NSMutableArray* controllers = [NSMutableArray new];
  95. for (NSDictionary* child in node.children) {
  96. [controllers addObject:[self fromTree:child]];
  97. }
  98. [vc setViewControllers:controllers];
  99. return vc;
  100. }
  101. -(UIViewController<RNNRootViewProtocol> *)createTabs:(RNNLayoutNode*)node {
  102. RNNTabBarController* vc = [[RNNTabBarController alloc] initWithEventEmitter:_eventEmitter];
  103. vc.layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  104. NSMutableArray* controllers = [NSMutableArray new];
  105. for (NSDictionary *child in node.children) {
  106. UIViewController<RNNRootViewProtocol>* childVc = [self fromTree:child];
  107. [childVc.layoutInfo.options applyOn:childVc];
  108. [childVc.getLeafViewController.layoutInfo.options applyOn:childVc.getLeafViewController];
  109. [controllers addObject:childVc];
  110. }
  111. [vc setViewControllers:controllers];
  112. return vc;
  113. }
  114. - (UIViewController<RNNRootViewProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  115. RNNTopTabsViewController* vc = [[RNNTopTabsViewController alloc] init];
  116. vc.layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  117. NSMutableArray* controllers = [NSMutableArray new];
  118. for (NSDictionary *child in node.children) {
  119. RNNRootViewController* childVc = (RNNRootViewController*)[self fromTree:child];
  120. [controllers addObject:childVc];
  121. [_bridge.uiManager setAvailableSize:vc.contentView.bounds.size forRootView:childVc.view];
  122. }
  123. [vc setViewControllers:controllers];
  124. return vc;
  125. }
  126. - (UIViewController<RNNRootViewProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  127. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node optionsManager:_optionsManager];
  128. NSMutableArray* childrenVCs = [NSMutableArray new];
  129. for (NSDictionary *child in node.children) {
  130. UIViewController *vc = [self fromTree:child];
  131. [childrenVCs addObject:vc];
  132. }
  133. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  134. sideMenu.layoutInfo = layoutInfo;
  135. return sideMenu;
  136. }
  137. - (UIViewController<RNNRootViewProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  138. UIViewController<RNNRootViewProtocol>* child = [self fromTree:node.children[0]];
  139. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  140. return sideMenuChild;
  141. }
  142. - (UIViewController<RNNRootViewProtocol> *)createSplitView:(RNNLayoutNode*)node {
  143. NSString* componentId = node.nodeId;
  144. RNNSplitViewOptions* options = [[RNNSplitViewOptions alloc] initWithDict:_optionsManager.defaultOptionsDict];
  145. [options mergeWith:node.data[@"options"]];
  146. RNNSplitViewController* svc = [[RNNSplitViewController alloc] initWithOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter];
  147. // We need two children of the node for successful Master / Detail
  148. NSDictionary *master = node.children[0];
  149. NSDictionary *detail = node.children[1];
  150. // Create view controllers
  151. RNNRootViewController* masterVc = (RNNRootViewController*)[self fromTree:master];
  152. RNNRootViewController* detailVc = (RNNRootViewController*)[self fromTree:detail];
  153. // Set the controllers and delegate to masterVC
  154. svc.viewControllers = [NSArray arrayWithObjects:masterVc, detailVc, nil];
  155. svc.delegate = masterVc;
  156. return svc;
  157. }
  158. @end