react-native-navigation的迁移库

RNNControllerFactory.m 8.6KB

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 "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. RNNStore *_store;
  21. RCTBridge *_bridge;
  22. RNNReactComponentRegistry* _componentRegistry;
  23. }
  24. # pragma mark public
  25. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  26. eventEmitter:(RNNEventEmitter*)eventEmitter
  27. store:(RNNStore *)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<RNNParentProtocol> *)createLayout:(NSDictionary*)layout {
  39. UIViewController<RNNParentProtocol>* 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<RNNParentProtocol> *)fromTree:(NSDictionary*)json {
  51. RNNLayoutNode* node = [RNNLayoutNode create:json];
  52. UIViewController<RNNParentProtocol> *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. [_store setComponent:result componentId:node.nodeId];
  87. return result;
  88. }
  89. - (UIViewController<RNNParentProtocol> *)createComponent:(RNNLayoutNode*)node {
  90. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  91. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  92. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] initWithcomponentRegistry:_componentRegistry];
  93. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  94. return (UIViewController<RNNParentProtocol> *)component;
  95. }
  96. - (UIViewController<RNNParentProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  97. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  98. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  99. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  100. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  101. RNNRootViewController* component = [[RNNRootViewController alloc] initExternalComponentWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  102. [component bindViewController:externalVC];
  103. return (UIViewController<RNNParentProtocol> *)component;
  104. }
  105. - (UIViewController<RNNParentProtocol> *)createStack:(RNNLayoutNode*)node {
  106. RNNNavigationControllerPresenter* presenter = [[RNNNavigationControllerPresenter alloc] initWithcomponentRegistry:_componentRegistry];
  107. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  108. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  109. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  110. RNNNavigationController* stack = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo creator:_creator childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  111. return stack;
  112. }
  113. -(UIViewController<RNNParentProtocol> *)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 childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  119. return tabsController;
  120. }
  121. - (UIViewController<RNNParentProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  122. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  123. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  124. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  125. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  126. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  127. return topTabsController;
  128. }
  129. - (UIViewController<RNNParentProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  130. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  131. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  132. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] init];
  133. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  134. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  135. return sideMenu;
  136. }
  137. - (UIViewController<RNNParentProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  138. UIViewController<RNNParentProtocol>* childVc = [self fromTree:node.children[0]];
  139. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  140. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  141. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo childViewControllers:@[childVc] options:options defaultOptions:_defaultOptions presenter:[[RNNViewControllerPresenter alloc] init] type:type];
  142. return sideMenuChild;
  143. }
  144. - (UIViewController<RNNParentProtocol> *)createSplitView:(RNNLayoutNode*)node {
  145. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  146. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  147. RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] init];
  148. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  149. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  150. return splitViewController;
  151. }
  152. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  153. NSMutableArray* childrenArray = [NSMutableArray new];
  154. for (NSDictionary* child in node.children) {
  155. UIViewController* childVc = [self fromTree:child];
  156. [childrenArray addObject:childVc];
  157. }
  158. return childrenArray;
  159. }
  160. @end