react-native-navigation的迁移库

RNNControllerFactory.m 3.2KB

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