react-native-navigation的迁移库

RNNControllerFactory.m 8.2KB

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