react-native-navigation的迁移库

RNNControllerFactory.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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] init];
  80. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options];
  81. if (!component.isExternalViewController) {
  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. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  91. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  92. RNNRootViewController* component = [[RNNRootViewController alloc] initExternalComponentWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options];
  93. [component bindViewController:externalVC];
  94. return (UIViewController<RNNParentProtocol> *)component;
  95. }
  96. - (UIViewController<RNNParentProtocol> *)createStack:(RNNLayoutNode*)node {
  97. RNNNavigationControllerPresenter* presenter = [[RNNNavigationControllerPresenter alloc] init];
  98. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  99. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  100. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  101. RNNNavigationController* stack = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  102. return stack;
  103. }
  104. -(UIViewController<RNNParentProtocol> *)createTabs:(RNNLayoutNode*)node {
  105. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  106. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  107. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  108. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  109. RNNTabBarController* tabsController = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter eventEmitter:_eventEmitter];
  110. return tabsController;
  111. }
  112. - (UIViewController<RNNParentProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  113. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  114. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  115. RNNBasePresenter* presenter = [[RNNBasePresenter alloc] init];
  116. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  117. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  118. return topTabsController;
  119. }
  120. - (UIViewController<RNNParentProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  121. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  122. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  123. RNNBasePresenter* presenter = [[RNNBasePresenter alloc] init];
  124. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  125. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  126. return sideMenu;
  127. }
  128. - (UIViewController<RNNParentProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  129. UIViewController<RNNParentProtocol>* childVc = [self fromTree:node.children[0]];
  130. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  131. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  132. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo childViewControllers:@[childVc] options:options presenter:[[RNNBasePresenter alloc] init] type:type];
  133. return sideMenuChild;
  134. }
  135. - (UIViewController<RNNParentProtocol> *)createSplitView:(RNNLayoutNode*)node {
  136. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  137. RNNNavigationOptions* options = [_optionsManager createOptions:node.data[@"options"]];
  138. RNNBasePresenter* presenter = [[RNNBasePresenter alloc] init];
  139. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  140. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  141. return splitViewController;
  142. }
  143. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  144. NSMutableArray* childrenArray = [NSMutableArray new];
  145. for (NSDictionary* child in node.children) {
  146. UIViewController* childVc = [self fromTree:child];
  147. [childrenArray addObject:childVc];
  148. }
  149. return childrenArray;
  150. }
  151. @end