react-native-navigation的迁移库

RNNViewController.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #import "RNNViewController.h"
  2. #import "RCTRootView.h"
  3. #import "MMDrawerController.h"
  4. #define SCREEN @"screen"
  5. #define SIDE_MENU @"sideMenu"
  6. #define TABS @"tabs"
  7. #define SCREEN_KEY @"key"
  8. #define SIDE_MENU_LEFT @"left"
  9. #define SIDE_MENU_RIGHT @"right"
  10. typedef enum
  11. {
  12. SideMenuModeNone = 0,
  13. SideMenuModeLeft = 1 << 0,
  14. SideMenuModeRight = 1 << 1,
  15. } SideMenuMode;
  16. @implementation RNNViewController
  17. #pragma mark - External methods
  18. + (UIViewController*)controllerWithLayout:(NSDictionary *)layout bridge:(RCTBridge *)bridge {
  19. UIViewController *controller = nil;
  20. SideMenuMode sideMenuMode = [RNNViewController sideMenuModeForLayout:layout];
  21. UIViewController *centerViewController = [RNNViewController centerControllerWithLayout:layout bridge:bridge];
  22. if (sideMenuMode == SideMenuModeNone) {
  23. controller = centerViewController;
  24. }
  25. else {
  26. NSDictionary *sideMenuLayout = layout[SIDE_MENU];
  27. controller = [RNNViewController sideMenuControllerWithSideMenuLayout:sideMenuLayout
  28. sideMenuMode:sideMenuMode
  29. centerViewController:centerViewController
  30. bridge:bridge];
  31. }
  32. return controller;
  33. }
  34. #pragma mark - Helper methods
  35. +(SideMenuMode)sideMenuModeForLayout:(NSDictionary*)layout {
  36. NSDictionary *sideMenu = layout[SIDE_MENU];
  37. SideMenuMode mode = SideMenuModeNone;
  38. if (sideMenu) {
  39. if (sideMenu[SIDE_MENU_LEFT]) {
  40. mode |= SideMenuModeLeft;
  41. }
  42. if (sideMenu[SIDE_MENU_RIGHT]) {
  43. mode |= SideMenuModeRight;
  44. }
  45. }
  46. return mode;
  47. }
  48. +(UIViewController*)centerControllerWithLayout:(NSDictionary*)layout bridge:(RCTBridge*)bridge {
  49. UIViewController *controller = nil;
  50. NSDictionary *tabs = layout[TABS];
  51. NSDictionary *screen = layout[SCREEN];
  52. if (tabs) {
  53. controller = [RNNViewController tabBarControllerWithArray:tabs bridge:bridge];
  54. }
  55. else if (screen) {
  56. NSDictionary *screenLayout = @{SCREEN: screen};
  57. controller = [RNNViewController controllerWithScreenLayout:screenLayout bridge:bridge];
  58. }
  59. return controller;
  60. }
  61. +(MMDrawerController*)sideMenuControllerWithSideMenuLayout:(NSDictionary*)sideMenuLayout sideMenuMode:(SideMenuMode)sideMenuMode centerViewController:(UIViewController*)centerVC bridge:(RCTBridge*)bridge {
  62. UIViewController *leftVC, *rightVC = nil;
  63. if (sideMenuMode & SideMenuModeLeft) {
  64. NSDictionary *leftLayout = sideMenuLayout[SIDE_MENU_LEFT];
  65. NSString *leftScreenKey = leftLayout[SCREEN_KEY];
  66. leftVC = [RNNViewController controllerWithScreenKey:leftScreenKey bridge:bridge];
  67. }
  68. if (sideMenuMode & SideMenuModeRight) {
  69. NSDictionary *rightLayout = sideMenuLayout[SIDE_MENU_RIGHT];
  70. NSString *rightScreenKey = rightLayout[SCREEN_KEY];
  71. rightVC = [RNNViewController controllerWithScreenKey:rightScreenKey bridge:bridge];
  72. }
  73. MMDrawerController *controller = [[MMDrawerController alloc] initWithCenterViewController:centerVC
  74. leftDrawerViewController:leftVC
  75. rightDrawerViewController:rightVC];
  76. controller.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
  77. controller.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
  78. return controller;
  79. }
  80. +(UITabBarController*)tabBarControllerWithArray:(NSArray*)tabs bridge:(RCTBridge*)bridge {
  81. UIViewController *controller = nil;
  82. NSMutableArray *tabsVC = [NSMutableArray new];
  83. for (NSDictionary* tab in tabs) {
  84. UIViewController *tabVC = [RNNViewController controllerWithScreenLayout:tab bridge:bridge];
  85. if (tabVC) {
  86. [tabsVC addObject:tabVC];
  87. }
  88. }
  89. if ([tabsVC count] > 0) {
  90. UITabBarController *tabController = [[UITabBarController alloc] init];
  91. tabController.viewControllers = tabsVC;
  92. controller = tabController;
  93. }
  94. return controller;
  95. }
  96. +(UIViewController*)controllerWithScreenLayout:(NSDictionary*)screenLayout bridge:(RCTBridge*)bridge {
  97. UIViewController *controller = nil;
  98. NSDictionary *screen = screenLayout[SCREEN];
  99. NSDictionary *screenKey = screen[SCREEN_KEY];
  100. if (screen && screenKey) {
  101. UIViewController *rootVC = [RNNViewController controllerWithScreenKey:screenKey bridge:bridge];
  102. if (rootVC) {
  103. controller = [[UINavigationController alloc] initWithRootViewController:rootVC];
  104. [controller.tabBarItem setTitle:screenKey];
  105. }
  106. }
  107. return controller;
  108. }
  109. +(UIViewController*)controllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge*)bridge {
  110. UIViewController *controller = nil;
  111. if (screenKey) {
  112. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:screenKey initialProperties:nil];
  113. if (!reactView) return nil;
  114. controller = [UIViewController new];
  115. controller.view = reactView;
  116. }
  117. return controller;
  118. }
  119. @end