react-native-navigation的迁移库

RNNControllerFactory.m 3.2KB

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