react-native-navigation的迁移库

RNNControllerFactory.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #import "RNNControllerFactory.h"
  2. #import "RNNLayoutNode.h"
  3. @interface RNNControllerFactory ()
  4. @property (nonatomic, strong) id<RNNRootViewCreator> creator;
  5. @end
  6. @implementation RNNControllerFactory
  7. # pragma mark public
  8. -(instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator {
  9. self = [super init];
  10. self.creator = creator;
  11. return self;
  12. }
  13. -(UIViewController *)createLayout:(NSDictionary *)layout
  14. {
  15. return [self fromTree:layout];
  16. }
  17. # pragma mark private
  18. -(UIViewController*)fromTree:(NSDictionary*)json
  19. {
  20. RNNLayoutNode* node = [RNNLayoutNode create:json];
  21. if ( node.isContainer) {
  22. return [self createContainer:node];
  23. }
  24. else if (node.isContainerStack) {
  25. return [self createContainerStack:node];
  26. }
  27. else if (node.isTabs) {
  28. return [self createTabs:node];
  29. }
  30. else if (node.isSideMenuRoot) {
  31. return [self createSideMenu:node];
  32. }
  33. else if (node.isSideMenuCenter) {
  34. return [self createSideMenuCenter:node];
  35. }
  36. else if (node.isSideMenuLeft) {
  37. return [self createSideMenuLeft:node];
  38. }
  39. else if (node.isSideMenuRight) {
  40. return [self createSideMenuRight:node];
  41. }
  42. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  43. }
  44. -(RNNRootViewController*)createContainer:(RNNLayoutNode*)node
  45. {
  46. return [[RNNRootViewController alloc] initWithNode:node rootViewCreator:self.creator];
  47. }
  48. -(UINavigationController*)createContainerStack:(RNNLayoutNode*)node
  49. {
  50. UINavigationController* vc = [[UINavigationController alloc] init];
  51. NSMutableArray* controllers = [NSMutableArray new];
  52. for (NSDictionary* child in node.children) {
  53. [controllers addObject:[self fromTree:child]];
  54. }
  55. [vc setViewControllers:controllers];
  56. return vc;
  57. }
  58. -(UITabBarController*)createTabs:(RNNLayoutNode*)node
  59. {
  60. UITabBarController* vc = [[UITabBarController alloc] init];
  61. NSMutableArray* controllers = [NSMutableArray new];
  62. for (NSDictionary *child in node.children) {
  63. UIViewController* childVc = [self fromTree:child];
  64. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  65. [childVc setTabBarItem:item];
  66. [controllers addObject:childVc];
  67. }
  68. [vc setViewControllers:controllers];
  69. return vc;
  70. }
  71. -(UIViewController*)createSideMenu:(RNNLayoutNode*)node
  72. {
  73. // NSArray *childern = node.children;
  74. // id center, left, right;
  75. // for (NSDictionary *child in childern) {
  76. // RNNLayoutNode *childNode = [RNNLayoutNode create:child];
  77. // UIViewController *vc = [self fromTree:child];
  78. //
  79. // if ([childNode isSideMenuLeft]) {
  80. // left = vc;
  81. // }
  82. // else if ([childNode isSideMenuCenter]) {
  83. // center = vc;
  84. // }
  85. // else if ([childNode isSideMenuRight]) {
  86. // right = [self fromTree:child];
  87. // }
  88. // else {
  89. // @throw [NSException exceptionWithName:@"UnknownSideMenuType" reason:[@"Unknown side menu type for side menu root node " stringByAppendingString:childNode.type] userInfo:nil];
  90. // }
  91. // }
  92. // if (!center) {
  93. // @throw [NSException exceptionWithName:@"WrongSideMenuDefinition" reason:@"Side menu without center is illigal" userInfo:nil];
  94. // }
  95. //
  96. // MMDrawerController *sideMenu = [[MMDrawerController alloc] initWithCenterViewController:center leftDrawerViewController:left rightDrawerViewController:right];
  97. NSMutableArray* childrenVCs = [NSMutableArray new];
  98. for (NSDictionary *child in node.children) {
  99. UIViewController *vc = [self fromTree:child];
  100. [childrenVCs addObject:vc];
  101. }
  102. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  103. return sideMenu;
  104. }
  105. -(UIViewController*)createSideMenuCenter:(RNNLayoutNode*)node {
  106. UIViewController* child = [self fromTree:node.children[0]];
  107. RNNSideMenuCenterVC *center = [[RNNSideMenuCenterVC alloc] initWithChild: child];
  108. return center;
  109. }
  110. -(UIViewController*)createSideMenuLeft:(RNNLayoutNode*)node {
  111. UIViewController* child = [self fromTree:node.children[0]];
  112. RNNSideMenuLeftVC *left = [[RNNSideMenuLeftVC alloc] initWithChild:child];
  113. return left;
  114. }
  115. -(UIViewController*)createSideMenuRight:(RNNLayoutNode*)node {
  116. UIViewController* child = [self fromTree:node.children[0]];
  117. RNNSideMenuRightVC *right = [[RNNSideMenuRightVC alloc] initWithChild:child];
  118. return right;
  119. }
  120. @end