react-native-navigation的迁移库

RNNControllerFactory.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. @implementation RNNControllerFactory {
  18. id<RNNRootViewCreator> _creator;
  19. RNNStore *_store;
  20. RCTBridge *_bridge;
  21. }
  22. # pragma mark public
  23. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  24. eventEmitter:(RNNEventEmitter*)eventEmitter
  25. andBridge:(RCTBridge *)bridge {
  26. self = [super init];
  27. _creator = creator;
  28. _eventEmitter = eventEmitter;
  29. _bridge = bridge;
  30. return self;
  31. }
  32. - (UIViewController<RNNParentProtocol> *)createLayout:(NSDictionary*)layout saveToStore:(RNNStore *)store {
  33. _store = store;
  34. UIViewController<RNNParentProtocol>* layoutViewController = [self fromTree:layout];
  35. _store = nil;
  36. return layoutViewController;
  37. }
  38. - (NSArray<RNNLayoutProtocol> *)createChildrenLayout:(NSArray*)children saveToStore:(RNNStore *)store {
  39. _store = store;
  40. NSMutableArray<RNNLayoutProtocol>* childViewControllers = [NSMutableArray<RNNLayoutProtocol> new];
  41. for (NSDictionary* layout in children) {
  42. [childViewControllers addObject:[self fromTree:layout]];
  43. }
  44. _store = nil;
  45. return childViewControllers;
  46. }
  47. # pragma mark private
  48. - (UIViewController<RNNParentProtocol> *)fromTree:(NSDictionary*)json {
  49. RNNLayoutNode* node = [RNNLayoutNode create:json];
  50. UIViewController<RNNParentProtocol> *result;
  51. if (node.isComponent) {
  52. result = [self createComponent:node];
  53. }
  54. else if (node.isStack) {
  55. result = [self createStack:node];
  56. }
  57. else if (node.isTabs) {
  58. result = [self createTabs:node];
  59. }
  60. else if (node.isTopTabs) {
  61. result = [self createTopTabs:node];
  62. }
  63. else if (node.isSideMenuRoot) {
  64. result = [self createSideMenu:node];
  65. }
  66. else if (node.isSideMenuCenter) {
  67. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  68. }
  69. else if (node.isSideMenuLeft) {
  70. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  71. }
  72. else if (node.isSideMenuRight) {
  73. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  74. }
  75. else if (node.isExternalComponent) {
  76. result = [self createExternalComponent:node];
  77. }
  78. else if (node.isSplitView) {
  79. result = [self createSplitView:node];
  80. }
  81. if (!result) {
  82. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  83. }
  84. [_store setComponent:result componentId:node.nodeId];
  85. return result;
  86. }
  87. - (UIViewController<RNNParentProtocol> *)createComponent:(RNNLayoutNode*)node {
  88. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  89. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  90. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  91. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  92. if (!component.isExternalViewController) {
  93. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  94. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  95. }
  96. return (UIViewController<RNNParentProtocol> *)component;
  97. }
  98. - (UIViewController<RNNParentProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  99. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  100. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  101. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  102. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  103. RNNRootViewController* component = [[RNNRootViewController alloc] initExternalComponentWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options defaultOptions:_defaultOptions];
  104. [component bindViewController:externalVC];
  105. return (UIViewController<RNNParentProtocol> *)component;
  106. }
  107. - (UIViewController<RNNParentProtocol> *)createStack:(RNNLayoutNode*)node {
  108. RNNNavigationControllerPresenter* presenter = [[RNNNavigationControllerPresenter alloc] init];
  109. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  110. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  111. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  112. RNNNavigationController* stack = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  113. return stack;
  114. }
  115. -(UIViewController<RNNParentProtocol> *)createTabs:(RNNLayoutNode*)node {
  116. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  117. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  118. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  119. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  120. RNNTabBarController* tabsController = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter eventEmitter:_eventEmitter];
  121. return tabsController;
  122. }
  123. - (UIViewController<RNNParentProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  124. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  125. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  126. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  127. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  128. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  129. return topTabsController;
  130. }
  131. - (UIViewController<RNNParentProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  132. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  133. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  134. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] init];
  135. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  136. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  137. return sideMenu;
  138. }
  139. - (UIViewController<RNNParentProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  140. UIViewController<RNNParentProtocol>* childVc = [self fromTree:node.children[0]];
  141. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  142. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  143. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo childViewControllers:@[childVc] options:options defaultOptions:_defaultOptions presenter:[[RNNViewControllerPresenter alloc] init] type:type];
  144. return sideMenuChild;
  145. }
  146. - (UIViewController<RNNParentProtocol> *)createSplitView:(RNNLayoutNode*)node {
  147. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  148. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  149. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  150. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  151. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:_defaultOptions presenter:presenter];
  152. return splitViewController;
  153. }
  154. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  155. NSMutableArray* childrenArray = [NSMutableArray new];
  156. for (NSDictionary* child in node.children) {
  157. UIViewController* childVc = [self fromTree:child];
  158. [childrenArray addObject:childVc];
  159. }
  160. return childrenArray;
  161. }
  162. @end