react-native-navigation的迁移库

RNNControllerFactory.m 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
  35. }
  36. else if (node.isSideMenuLeft) {
  37. return [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
  38. }
  39. else if (node.isSideMenuRight) {
  40. return [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
  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. NSMutableArray* childrenVCs = [NSMutableArray new];
  74. for (NSDictionary *child in node.children) {
  75. UIViewController *vc = [self fromTree:child];
  76. [childrenVCs addObject:vc];
  77. }
  78. RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
  79. return sideMenu;
  80. }
  81. -(UIViewController*)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
  82. UIViewController* child = [self fromTree:node.children[0]];
  83. RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
  84. return sideMenuChild;
  85. }
  86. @end