react-native-navigation的迁移库

RNNControllerFactory.m 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @end
  33. @implementation RNNControllerFactory
  34. -(UIViewController *)createRootViewController:(NSDictionary *)layout
  35. {
  36. return [self fromTree:layout];
  37. }
  38. -(UIViewController*)fromTree:(NSDictionary*)json
  39. {
  40. RNNLayoutNode* node = [RNNLayoutNode create:json];
  41. if (node.isContainer)
  42. {
  43. return [self createContainer:node];
  44. } else if(node.isContainerStack)
  45. {
  46. return [self createContainerStack:node];
  47. } else if(node.isTabs)
  48. {
  49. return [self createTabs:node];
  50. }
  51. @throw @"unknown container type";
  52. }
  53. -(UIViewController*)createContainer:(RNNLayoutNode*)node
  54. {
  55. NSString* containerName = node.data[@"name"];
  56. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:RNN.instance.bridge
  57. moduleName:containerName
  58. initialProperties:@{@"containerId": node.nodeId}];
  59. UIViewController* controller = [UIViewController new];
  60. controller.view = reactView;
  61. return controller;
  62. }
  63. -(UINavigationController*)createContainerStack:(RNNLayoutNode*)node
  64. {
  65. UINavigationController* vc = [[UINavigationController 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. -(UITabBarController*)createTabs:(RNNLayoutNode*)node
  74. {
  75. UITabBarController* vc = [[UITabBarController alloc] init];
  76. NSMutableArray* controllers = [NSMutableArray new];
  77. for (NSDictionary* child in node.children) {
  78. UIViewController* childVc = [self fromTree:child];
  79. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  80. [childVc setTabBarItem:item];
  81. [controllers addObject:childVc];
  82. }
  83. [vc setViewControllers:controllers];
  84. return vc;
  85. }
  86. @end