react-native-navigation的迁移库

RNNControllerFactory.m 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. [presenter setDefaultOptions:_defaultOptions];
  83. RNNRootViewController* component = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:_eventEmitter presenter:presenter options:options];
  84. if (!component.isExternalViewController) {
  85. CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;
  86. [_bridge.uiManager setAvailableSize:availableSize forRootView:component.view];
  87. }
  88. return (UIViewController<RNNParentProtocol> *)component;
  89. }
  90. - (UIViewController<RNNParentProtocol> *)createExternalComponent:(RNNLayoutNode*)node {
  91. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  92. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  93. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  94. [presenter setDefaultOptions:_defaultOptions];
  95. UIViewController* externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
  96. RNNRootViewController* component = [[RNNRootViewController alloc] initExternalComponentWithLayoutInfo:layoutInfo eventEmitter:_eventEmitter presenter:presenter options:options];
  97. [component bindViewController:externalVC];
  98. return (UIViewController<RNNParentProtocol> *)component;
  99. }
  100. - (UIViewController<RNNParentProtocol> *)createStack:(RNNLayoutNode*)node {
  101. RNNNavigationControllerPresenter* presenter = [[RNNNavigationControllerPresenter alloc] init];
  102. [presenter setDefaultOptions:_defaultOptions];
  103. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  104. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  105. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  106. RNNNavigationController* stack = [[RNNNavigationController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  107. return stack;
  108. }
  109. -(UIViewController<RNNParentProtocol> *)createTabs:(RNNLayoutNode*)node {
  110. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  111. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  112. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  113. [presenter setDefaultOptions:_defaultOptions];
  114. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  115. RNNTabBarController* tabsController = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter eventEmitter:_eventEmitter];
  116. return tabsController;
  117. }
  118. - (UIViewController<RNNParentProtocol> *)createTopTabs:(RNNLayoutNode*)node {
  119. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  120. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  121. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  122. [presenter setDefaultOptions:_defaultOptions];
  123. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  124. RNNTopTabsViewController* topTabsController = [[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  125. return topTabsController;
  126. }
  127. - (UIViewController<RNNParentProtocol> *)createSideMenu:(RNNLayoutNode*)node {
  128. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  129. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  130. RNNSideMenuPresenter* presenter = [[RNNSideMenuPresenter alloc] init];
  131. [presenter setDefaultOptions:_defaultOptions];
  132. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  133. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  134. return sideMenu;
  135. }
  136. - (UIViewController<RNNParentProtocol> *)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  137. UIViewController<RNNParentProtocol>* childVc = [self fromTree:node.children[0]];
  138. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  139. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  140. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithLayoutInfo:layoutInfo childViewControllers:@[childVc] options:options presenter:[[RNNViewControllerPresenter alloc] init] type:type];
  141. return sideMenuChild;
  142. }
  143. - (UIViewController<RNNParentProtocol> *)createSplitView:(RNNLayoutNode*)node {
  144. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
  145. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];;
  146. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  147. [presenter setDefaultOptions:_defaultOptions];
  148. NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
  149. RNNSplitViewController* splitViewController = [[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  150. return splitViewController;
  151. }
  152. - (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
  153. NSMutableArray* childrenArray = [NSMutableArray new];
  154. for (NSDictionary* child in node.children) {
  155. UIViewController* childVc = [self fromTree:child];
  156. [childrenArray addObject:childVc];
  157. }
  158. return childrenArray;
  159. }
  160. @end