react-native-navigation的迁移库

RNNViewController.m 5.8KB

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