react-native-navigation的迁移库

RNNControllerFactory.m 8.5KB

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