react-native-navigation的迁移库

RNNViewController.m 5.8KB

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