react-native-navigation的迁移库

RNNControllerFactory.m 3.6KB

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