react-native-navigation的迁移库

RNNControllerFactory.m 8.6KB

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