react-native-navigation的迁移库

RNNControllerFactory.m 9.5KB

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