react-native-navigation的迁移库

RNNControllerFactory.m 9.9KB

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