react-native-navigation的迁移库

RNNControllerFactory.m 3.3KB

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