react-native-navigation的迁移库

RNNControllerFactory.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 childViewControllers:childViewControllers];
  110. return stack;
  111. }
  112. -(UIViewController *)createTabs:(RNNLayoutNode*)node {
  113. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  114. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  115. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  116. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  117. RNNTabBarController* tabsController = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  118. return tabsController;
  119. }
  120. - (UIViewController *)createTopTabs:(RNNLayoutNode*)node {
  121. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  122. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  123. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  124. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  125. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  126. return topTabsController;
  127. }
  128. - (UIViewController *)createSideMenu:(RNNLayoutNode*)node {
  129. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  130. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  131. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] init];
  132. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  133. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo creator:_creator childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  134. return sideMenu;
  135. }
  136. - (UIViewController *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  137. UIViewController* childVc = [self fromTree:node.children[0]];
  138. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  139. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  140. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:[[RNNViewControllerPresenter alloc] init] eventEmitter:_eventEmitter childViewController:childVc type:type];
  141. return sideMenuChild;
  142. }
  143. - (UIViewController *)createSplitView:(RNNLayoutNode*)node {
  144. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  145. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  146. RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] init];
  147. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  148. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  149. return splitViewController;
  150. }
  151. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  152. NSMutableArray* childrenArray = [NSMutableArray new];
  153. for (NSDictionary* child in node.children) {
  154. UIViewController* childVc = [self fromTree:child];
  155. [childrenArray addObject:childVc];
  156. }
  157. return childrenArray;
  158. }
  159. @end