react-native-navigation的迁移库

RNNControllerFactory.m 3.7KB

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