react-native-navigation的迁移库

RNNControllerFactory.m 8.4KB

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