react-native-navigation的迁移库

RNNControllerFactory.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #import "RNNControllerFactory.h"
  2. #import "RNN.h"
  3. #import "RCTRootView.h"
  4. @interface RNNLayoutNode : NSObject
  5. @property NSString* type;
  6. @property NSString* nodeId;
  7. @property NSDictionary* data;
  8. @property NSArray* children;
  9. @end
  10. @implementation RNNLayoutNode
  11. +(instancetype)create:(NSDictionary *)json
  12. {
  13. RNNLayoutNode* node = [RNNLayoutNode new];
  14. node.type = json[@"type"];
  15. node.nodeId = json[@"id"];
  16. node.data = json[@"data"];
  17. node.children = json[@"children"];
  18. return node;
  19. }
  20. -(BOOL)isContainer
  21. {
  22. return [self.type isEqualToString:@"Container"];
  23. }
  24. -(BOOL)isContainerStack
  25. {
  26. return [self.type isEqualToString:@"ContainerStack"];
  27. }
  28. -(BOOL)isTabs
  29. {
  30. return [self.type isEqualToString:@"Tabs"];
  31. }
  32. -(BOOL)isSideMenuRoot
  33. {
  34. return [self.type isEqualToString:@"SideMenuRoot"];
  35. }
  36. -(BOOL)isSideMenuLeft
  37. {
  38. return [self.type isEqualToString:@"SideMenuLeft"];
  39. }
  40. -(BOOL)isSideMenuRight
  41. {
  42. return [self.type isEqualToString:@"SideMenuRight"];
  43. }
  44. -(BOOL)isSideMenuCenter
  45. {
  46. return [self.type isEqualToString:@"SideMenuCenter"];
  47. }
  48. @end
  49. @implementation RNNControllerFactory
  50. -(UIViewController *)createRootViewController:(NSDictionary *)layout
  51. {
  52. return [self fromTree:layout];
  53. }
  54. -(UIViewController*)fromTree:(NSDictionary*)json
  55. {
  56. RNNLayoutNode* node = [RNNLayoutNode create:json];
  57. if (node.isContainer)
  58. {
  59. return [self createContainer:node];
  60. } else if (node.isContainerStack)
  61. {
  62. return [self createContainerStack:node];
  63. } else if (node.isTabs)
  64. {
  65. return [self createTabs:node];
  66. }
  67. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  68. }
  69. -(UIViewController*)createContainer:(RNNLayoutNode*)node
  70. {
  71. NSString* containerName = node.data[@"name"];
  72. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:RNN.instance.bridge
  73. moduleName:containerName
  74. initialProperties:@{@"containerId": node.nodeId}];
  75. UIViewController* controller = [UIViewController new];
  76. controller.view = reactView;
  77. return controller;
  78. }
  79. -(UINavigationController*)createContainerStack:(RNNLayoutNode*)node
  80. {
  81. UINavigationController* vc = [[UINavigationController alloc] init];
  82. NSMutableArray* controllers = [NSMutableArray new];
  83. for (NSDictionary* child in node.children) {
  84. [controllers addObject:[self fromTree:child]];
  85. }
  86. [vc setViewControllers:controllers];
  87. return vc;
  88. }
  89. -(UITabBarController*)createTabs:(RNNLayoutNode*)node
  90. {
  91. UITabBarController* vc = [[UITabBarController alloc] init];
  92. NSMutableArray* controllers = [NSMutableArray new];
  93. for (NSDictionary* child in node.children) {
  94. UIViewController* childVc = [self fromTree:child];
  95. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  96. [childVc setTabBarItem:item];
  97. [controllers addObject:childVc];
  98. }
  99. [vc setViewControllers:controllers];
  100. return vc;
  101. }
  102. @end