react-native-navigation的迁移库

RNNControllerFactory.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #import "RNNControllerFactory.h"
  2. #import "RNNLayoutNode.h"
  3. #import "RNNRootViewController.h"
  4. #import "RNNSideMenuController.h"
  5. #import "RNNSideMenuChildVC.h"
  6. #import "RNNNavigationOptions.h"
  7. #import "RNNNavigationController.h"
  8. #import "RNNTabBarController.h"
  9. @implementation RNNControllerFactory {
  10. id<RNNRootViewCreator> _creator;
  11. RNNStore *_store;
  12. RNNEventEmitter *_eventEmitter;
  13. }
  14. # pragma mark public
  15. - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
  16. store:(RNNStore *)store
  17. eventEmitter:(RNNEventEmitter*)eventEmitter {
  18. self = [super init];
  19. _creator = creator;
  20. _store = store;
  21. _eventEmitter = eventEmitter;
  22. return self;
  23. }
  24. - (UIViewController*)createLayoutAndSaveToStore:(NSDictionary*)layout {
  25. return [self fromTree:layout];
  26. }
  27. # pragma mark private
  28. - (UIViewController*)fromTree:(NSDictionary*)json {
  29. RNNLayoutNode* node = [RNNLayoutNode create:json];
  30. UIViewController* result;
  31. if ( node.isContainer) {
  32. result = [self createContainer:node];
  33. }
  34. else if (node.isContainerStack) {
  35. result = [self createContainerStack:node];
  36. }
  37. else if (node.isTabs) {
  38. result = [self createTabs:node];
  39. }
  40. else if (node.isSideMenuRoot) {
  41. result = [self createSideMenu:node];
  42. }
  43. else if (node.isSideMenuCenter) {
  44. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  45. }
  46. else if (node.isSideMenuLeft) {
  47. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  48. }
  49. else if (node.isSideMenuRight) {
  50. result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  51. }
  52. if (!result) {
  53. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  54. }
  55. [_store setContainer:result containerId:node.nodeId];
  56. return result;
  57. }
  58. - (RNNRootViewController*)createContainer:(RNNLayoutNode*)node {
  59. NSString* name = node.data[@"name"];
  60. NSDictionary* customTransition = node.data[@"customTransition"];
  61. RNNAnimator* animator = [[RNNAnimator alloc] initWithAnimationsDictionary:customTransition];
  62. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"navigationOptions"]];
  63. NSString* containerId = node.nodeId;
  64. return [[RNNRootViewController alloc] initWithName:name withOptions:options withContainerId:containerId rootViewCreator:_creator eventEmitter:_eventEmitter animator:animator];
  65. }
  66. - (RNNNavigationController*)createContainerStack:(RNNLayoutNode*)node {
  67. RNNNavigationController* vc = [[RNNNavigationController alloc] init];
  68. NSMutableArray* controllers = [NSMutableArray new];
  69. for (NSDictionary* child in node.children) {
  70. [controllers addObject:[self fromTree:child]];
  71. }
  72. [vc setViewControllers:controllers];
  73. return vc;
  74. }
  75. -(RNNTabBarController*)createTabs:(RNNLayoutNode*)node {
  76. RNNTabBarController* vc = [[RNNTabBarController alloc] init];
  77. NSMutableArray* controllers = [NSMutableArray new];
  78. for (NSDictionary *child in node.children) {
  79. UIViewController* childVc = [self fromTree:child];
  80. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  81. [childVc setTabBarItem:item];
  82. [controllers addObject:childVc];
  83. }
  84. [vc setViewControllers:controllers];
  85. return vc;
  86. }
  87. - (UIViewController*)createSideMenu:(RNNLayoutNode*)node {
  88. NSMutableArray* childrenVCs = [NSMutableArray new];
  89. for (NSDictionary *child in node.children) {
  90. UIViewController *vc = [self fromTree:child];
  91. [childrenVCs addObject:vc];
  92. }
  93. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  94. return sideMenu;
  95. }
  96. - (UIViewController*)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  97. UIViewController* child = [self fromTree:node.children[0]];
  98. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  99. return sideMenuChild;
  100. }
  101. @end