react-native-navigation的迁移库

RNNControllerFactory.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #import "RNNControllerFactory.h"
  2. #import "RNNSplitViewController.h"
  3. #import "RNNSideMenuController.h"
  4. #import "RNNStackController.h"
  5. #import "RNNBottomTabsController.h"
  6. #import "RNNTopTabsViewController.h"
  7. #import "RNNComponentViewController.h"
  8. @implementation RNNControllerFactory {
  9. id<RNNComponentViewCreator> _creator;
  10. RNNExternalComponentStore *_store;
  11. RCTBridge *_bridge;
  12. RNNReactComponentRegistry* _componentRegistry;
  13. }
  14. # pragma mark public
  15. - (instancetype)initWithRootViewCreator:(id <RNNComponentViewCreator>)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. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry:_defaultOptions];
  82. RNNComponentViewController* component = [[RNNComponentViewController 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. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithDefaultOptions:_defaultOptions];
  89. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  90. RNNComponentViewController* component = [[RNNComponentViewController 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. RNNStackPresenter* presenter = [[RNNStackPresenter alloc] initWithComponentRegistry:_componentRegistry :_defaultOptions];
  96. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  97. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  98. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  99. RNNStackController* stack = [[RNNStackController 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. RNNBottomTabsPresenter* presenter = [[RNNBottomTabsPresenter alloc] initWithDefaultOptions:_defaultOptions];
  106. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  107. return [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo
  108. creator:_creator
  109. options:options
  110. defaultOptions:_defaultOptions
  111. presenter:presenter
  112. eventEmitter:_eventEmitter
  113. childViewControllers:childViewControllers
  114. ];
  115. }
  116. - (UIViewController *)createTopTabs:(RNNLayoutNode*)node {
  117. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  118. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  119. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithDefaultOptions :_defaultOptions];
  120. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  121. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  122. return topTabsController;
  123. }
  124. - (UIViewController *)createSideMenu:(RNNLayoutNode*)node {
  125. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  126. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  127. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] initWithDefaultOptions:_defaultOptions];
  128. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  129. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo creator:_creator childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  130. return sideMenu;
  131. }
  132. - (UIViewController *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  133. UIViewController* childVc = [self fromTree:node.children[0]];
  134. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  135. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  136. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:[[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry:_defaultOptions] eventEmitter:_eventEmitter childViewController:childVc type:type];
  137. return sideMenuChild;
  138. }
  139. - (UIViewController *)createSplitView:(RNNLayoutNode*)node {
  140. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  141. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  142. RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] initWithDefaultOptions:_defaultOptions];
  143. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  144. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  145. return splitViewController;
  146. }
  147. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  148. NSMutableArray* childrenArray = [NSMutableArray new];
  149. for (NSDictionary* child in node.children) {
  150. UIViewController* childVc = [self fromTree:child];
  151. [childrenArray addObject:childVc];
  152. }
  153. return childrenArray;
  154. }
  155. @end