react-native-navigation的迁移库

RNNViewController.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // RNNViewController.m
  3. // ReactNativeNavigation
  4. //
  5. // Created by Ran Greenberg on 21/12/2016.
  6. // Copyright © 2016 artal. All rights reserved.
  7. //
  8. #import "RNNViewController.h"
  9. #import "RCTRootView.h"
  10. #import "MMDrawerController.h"
  11. #define SCREEN @"screen"
  12. #define SIDE_MENU @"sideMenu"
  13. #define TABS @"tabs"
  14. #define SCREEN_KEY @"key"
  15. #define SIDE_MENU_LEFT @"left"
  16. #define SIDE_MENU_RIGHT @"right"
  17. @interface RNNViewController ()
  18. @end
  19. @implementation RNNViewController
  20. #pragma mark - Static function
  21. + (UIViewController*)controllerWithLayout:(NSDictionary *)layout bridge:(RCTBridge *)bridge {
  22. UIViewController *controller = nil;
  23. NSDictionary *screen = layout[SCREEN];
  24. NSArray *tabs = layout[TABS];
  25. NSDictionary *sideMenu = layout[SIDE_MENU];
  26. if (sideMenu) {
  27. NSDictionary *centerScreenDictionary;
  28. if (screen) {
  29. centerScreenDictionary = @{SCREEN: screen};
  30. }
  31. else if (tabs){
  32. centerScreenDictionary = @{TABS: layout[TABS]};
  33. }
  34. else {
  35. return nil;
  36. }
  37. UIViewController *centerViewController = [RNNViewController controllerWithLayout:centerScreenDictionary bridge:bridge];
  38. controller = [RNNViewController sideMenuWithLayout:sideMenu centerViewController:centerViewController bridge:bridge];
  39. return controller;
  40. }
  41. if (tabs) {
  42. controller = [RNNViewController tabBarWithTabsArray:tabs bridge:bridge];
  43. return controller;
  44. }
  45. if (screen) {
  46. NSString *screenKey = screen[SCREEN_KEY];
  47. if (screenKey) {
  48. controller = [RNNViewController navigationControllerWithScreenKey:screenKey bridge:bridge];
  49. }
  50. return controller;
  51. }
  52. return controller;
  53. }
  54. +(UIViewController*)controllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge *)bridge {
  55. UIViewController *controller = nil;
  56. RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:screenKey initialProperties:nil];
  57. if (!reactView) return nil;
  58. controller = [UIViewController new];
  59. controller.view = reactView;
  60. return controller;
  61. }
  62. +(UINavigationController*)navigationControllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge*)bridge {
  63. UINavigationController *controller = nil;
  64. UIViewController *viewController = [RNNViewController controllerWithScreenKey:screenKey bridge:bridge];
  65. if (!viewController) return nil;
  66. controller = [[UINavigationController alloc] initWithRootViewController:viewController];
  67. [controller.tabBarItem setTitle:@"tab"];
  68. return controller;
  69. }
  70. +(UIViewController*)sideMenuWithLayout:(NSDictionary*)layout centerViewController:(UIViewController*)centerViewController bridge:(RCTBridge*)bridge {
  71. UIViewController *leftViewController, *rightViewController, *sideMenuViewController = nil;
  72. NSDictionary *left = layout[SIDE_MENU_LEFT];
  73. if (left) {
  74. NSString *leftScreenKey = left[SCREEN_KEY];
  75. leftViewController = [RNNViewController controllerWithScreenKey:leftScreenKey bridge:bridge];
  76. }
  77. NSDictionary *right = layout[SIDE_MENU_RIGHT];
  78. if (right) {
  79. NSString *rightScreenKey = right[SCREEN_KEY];
  80. rightViewController = [RNNViewController controllerWithScreenKey:rightScreenKey bridge:bridge];
  81. }
  82. if (rightViewController || leftViewController) {
  83. sideMenuViewController = [[MMDrawerController alloc] initWithCenterViewController:centerViewController leftDrawerViewController:leftViewController rightDrawerViewController:rightViewController];
  84. // TODO: CHANGE THIS
  85. ((MMDrawerController*)sideMenuViewController).openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
  86. ((MMDrawerController*)sideMenuViewController).closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
  87. }
  88. return sideMenuViewController;
  89. }
  90. +(UITabBarController*)tabBarWithTabsArray:(NSArray*)tabsArray bridge:(RCTBridge*)bridge {
  91. UITabBarController *tabBarController = nil;
  92. NSMutableArray *tabsViewControllersArray = [[NSMutableArray alloc] init];
  93. for (NSDictionary *tab in tabsArray) {
  94. UIViewController *tabViewController = [RNNViewController controllerWithLayout:tab bridge:bridge];
  95. if (tabViewController) {
  96. [tabsViewControllersArray addObject:tabViewController];
  97. }
  98. }
  99. if ([tabsViewControllersArray count] > 0) {
  100. tabBarController = [UITabBarController new];
  101. tabBarController.viewControllers = tabsViewControllersArray;
  102. }
  103. return tabBarController;
  104. }
  105. -(void)viewWillAppear:(BOOL)animated {
  106. [super viewWillAppear:animated];
  107. }
  108. @end