react-native-navigation的迁移库

RNNControllerFactory.m 7.5KB

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