react-native-navigation的迁移库

RNNControllerFactory.m 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. @implementation RNNControllerFactory {
  12. id<RNNComponentViewCreator> _creator;
  13. RNNExternalComponentStore *_store;
  14. RCTBridge *_bridge;
  15. RNNReactComponentRegistry* _componentRegistry;
  16. BottomTabsAttachModeFactory* _bottomTabsAttachModeFactory;
  17. }
  18. # pragma mark public
  19. - (instancetype)initWithRootViewCreator:(id <RNNComponentViewCreator>)creator
  20. eventEmitter:(RNNEventEmitter*)eventEmitter
  21. store:(RNNExternalComponentStore *)store
  22. componentRegistry:(RNNReactComponentRegistry *)componentRegistry
  23. andBridge:(RCTBridge *)bridge
  24. bottomTabsAttachModeFactory:(BottomTabsAttachModeFactory *)bottomTabsAttachModeFactory {
  25. self = [super init];
  26. _creator = creator;
  27. _eventEmitter = eventEmitter;
  28. _bridge = bridge;
  29. _store = store;
  30. _componentRegistry = componentRegistry;
  31. _bottomTabsAttachModeFactory = bottomTabsAttachModeFactory;
  32. return self;
  33. }
  34. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  35. _defaultOptions = defaultOptions;
  36. _bottomTabsAttachModeFactory.defaultOptions = defaultOptions;
  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 createBottomTabs: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. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry:_defaultOptions];
  92. RNNComponentViewController* component = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  93. return component;
  94. }
  95. - (UIViewController *)createExternalComponent:(RNNLayoutNode*)node {
  96. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  97. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  98. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithDefaultOptions:_defaultOptions];
  99. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  100. RNNExternalViewController* component = [[RNNExternalViewController alloc] initWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions viewController:externalVC];
  101. return component;
  102. }
  103. - (UIViewController *)createStack:(RNNLayoutNode*)node {
  104. RNNStackPresenter* presenter = [[RNNStackPresenter alloc] initWithComponentRegistry:_componentRegistry defaultOptions:_defaultOptions];
  105. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  106. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  107. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  108. RNNStackController* stack = [[RNNStackController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  109. return stack;
  110. }
  111. - (UIViewController *)createBottomTabs:(RNNLayoutNode*)node {
  112. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  113. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  114. RNNBottomTabsPresenter* presenter = [[RNNBottomTabsPresenter alloc] initWithDefaultOptions:_defaultOptions];
  115. BottomTabsBaseAttacher* bottomTabsAttacher = [_bottomTabsAttachModeFactory fromOptions:options];
  116. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  117. return [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo
  118. creator:_creator
  119. options:options
  120. defaultOptions:_defaultOptions
  121. presenter:presenter
  122. eventEmitter:_eventEmitter
  123. childViewControllers:childViewControllers
  124. bottomTabsAttacher:bottomTabsAttacher
  125. ];
  126. }
  127. - (UIViewController *)createTopTabs:(RNNLayoutNode*)node {
  128. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  129. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  130. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithDefaultOptions :_defaultOptions];
  131. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  132. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  133. return topTabsController;
  134. }
  135. - (UIViewController *)createSideMenu:(RNNLayoutNode*)node {
  136. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  137. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  138. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] initWithDefaultOptions:_defaultOptions];
  139. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  140. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo creator:_creator childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  141. return sideMenu;
  142. }
  143. - (UIViewController *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  144. UIViewController* childVc = [self fromTree:node.children[0]];
  145. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  146. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  147. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:[[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry:_defaultOptions] eventEmitter:_eventEmitter childViewController:childVc type:type];
  148. return sideMenuChild;
  149. }
  150. - (UIViewController *)createSplitView:(RNNLayoutNode*)node {
  151. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  152. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
  153. RNNSplitViewControllerPresenter* presenter = [[RNNSplitViewControllerPresenter alloc] initWithDefaultOptions:_defaultOptions];
  154. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  155. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo creator:_creator options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter childViewControllers:childViewControllers];
  156. return splitViewController;
  157. }
  158. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  159. NSMutableArray* childrenArray = [NSMutableArray new];
  160. for (NSDictionary* child in node.children) {
  161. UIViewController* childVc = [self fromTree:child];
  162. [childrenArray addObject:childVc];
  163. }
  164. return childrenArray;
  165. }
  166. @end