react-native-navigation的迁移库

RNNControllerFactory.m 8.2KB

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