react-native-navigation的迁移库

RNNControllerFactory.m 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #import "RNNControllerFactory.h"
  2. #import "RNNSplitViewController.h"
  3. #import "RNNSideMenuController.h"
  4. #import "RNNNavigationController.h"
  5. #import "RNNTabBarController.h"
  6. #import "RNNTopTabsViewController.h"
  7. #import "RNNRootViewController.h"
  8. @implementation RNNControllerFactory {
  9. id<RNNRootViewCreator> _creator;
  10. RNNExternalComponentStore *_store;
  11. RCTBridge *_bridge;
  12. RNNReactComponentRegistry* _componentRegistry;
  13. }
  14. # pragma mark public
  15. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  16. eventEmitter:(RNNEventEmitter*)eventEmitter
  17. store:(RNNExternalComponentStore *)store
  18. componentRegistry:(RNNReactComponentRegistry *)componentRegistry
  19. andBridge:(RCTBridge *)bridge {
  20. self = [super init];
  21. _creator = creator;
  22. _eventEmitter = eventEmitter;
  23. _bridge = bridge;
  24. _store = store;
  25. _componentRegistry = componentRegistry;
  26. return self;
  27. }
  28. - (UIViewController *)createLayout:(NSDictionary*)layout {
  29. UIViewController* layoutViewController = [self fromTree:layout];
  30. return layoutViewController;
  31. }
  32. - (NSArray<RNNLayoutProtocol> *)createChildrenLayout:(NSArray*)children {
  33. NSMutableArray<RNNLayoutProtocol>* childViewControllers = [NSMutableArray<RNNLayoutProtocol> new];
  34. for (NSDictionary* layout in children) {
  35. [childViewControllers addObject:[self fromTree:layout]];
  36. }
  37. return childViewControllers;
  38. }
  39. # pragma mark private
  40. - (UIViewController *)fromTree:(NSDictionary*)json {
  41. RNNLayoutNode* node = [RNNLayoutNode create:json];
  42. UIViewController *result;
  43. if (node.isComponent) {
  44. result = [self createComponent:node];
  45. }
  46. else if (node.isStack) {
  47. result = [self createStack:node];
  48. }
  49. else if (node.isTabs) {
  50. result = [self createBottomTabs:node];
  51. }
  52. else if (node.isTopTabs) {
  53. result = [self createTopTabs:node];
  54. }
  55. else if (node.isSideMenuRoot) {
  56. result = [self createSideMenu:node];
  57. }
  58. else if (node.isSideMenuCenter) {
  59. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  60. }
  61. else if (node.isSideMenuLeft) {
  62. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  63. }
  64. else if (node.isSideMenuRight) {
  65. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  66. }
  67. else if (node.isExternalComponent) {
  68. result = [self createExternalComponent:node];
  69. }
  70. else if (node.isSplitView) {
  71. result = [self createSplitView:node];
  72. }
  73. if (!result) {
  74. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  75. }
  76. return result;
  77. }
  78. - (UIViewController *)createComponent:(RNNLayoutNode*)node {
  79. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  80. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  81. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] initWithComponentRegistry:_componentRegistry];
  82. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  83. return component;
  84. }
  85. - (UIViewController *)createExternalComponent:(RNNLayoutNode*)node {
  86. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  87. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  88. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  89. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  90. RNNRootViewController* component = [[RNNRootViewController alloc] initExternalComponentWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  91. [component bindViewController:externalVC];
  92. return component;
  93. }
  94. - (UIViewController *)createStack:(RNNLayoutNode*)node {
  95. RNNNavigationControllerPresenter* presenter = [[RNNNavigationControllerPresenter alloc] initWithComponentRegistry:_componentRegistry];
  96. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  97. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  98. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  99. RNNNavigationController* stack = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  100. return stack;
  101. }
  102. -(UIViewController *)createBottomTabs:(RNNLayoutNode*)node {
  103. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  104. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  105. RNNTabBarPresenter* presenter = [RNNTabBarPresenter new];
  106. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  107. RNNTabBarController* tabsController = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo
  108. creator:_creator
  109. options:options
  110. defaultOptions:_defaultOptions
  111. presenter:presenter
  112. eventEmitter:_eventEmitter
  113. childViewControllers:childViewControllers
  114. ];
  115. return tabsController;
  116. }
  117. - (UIViewController *)createTopTabs:(RNNLayoutNode*)node {
  118. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  119. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  120. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  121. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  122. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  123. return topTabsController;
  124. }
  125. - (UIViewController *)createSideMenu:(RNNLayoutNode*)node {
  126. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  127. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  128. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] init];
  129. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  130. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo creator:_creator childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  131. return sideMenu;
  132. }
  133. - (UIViewController *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  134. UIViewController* childVc = [self fromTree:node.children[0]];
  135. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  136. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  137. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:[[RNNViewControllerPresenter alloc] initWithComponentRegistry:_componentRegistry] eventEmitter:_eventEmitter childViewController:childVc type:type];
  138. return sideMenuChild;
  139. }
  140. - (UIViewController *)createSplitView:(RNNLayoutNode*)node {
  141. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  142. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  143. RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] init];
  144. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  145. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  146. return splitViewController;
  147. }
  148. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  149. NSMutableArray* childrenArray = [NSMutableArray new];
  150. for (NSDictionary* child in node.children) {
  151. UIViewController* childVc = [self fromTree:child];
  152. [childrenArray addObject:childVc];
  153. }
  154. return childrenArray;
  155. }
  156. @end