react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #import "RCCManager.h"
  2. #import "RCCViewController.h"
  3. #import <React/RCTBridge.h>
  4. #import <React/RCTRedBox.h>
  5. #import <Foundation/Foundation.h>
  6. @interface RCCManager() <RCTBridgeDelegate>
  7. @property (nonatomic, strong) NSMutableDictionary *modulesRegistry;
  8. @property (nonatomic, strong) RCTBridge *sharedBridge;
  9. @property (nonatomic, strong) NSURL *bundleURL;
  10. @end
  11. @implementation RCCManager
  12. + (instancetype)sharedInstance
  13. {
  14. static RCCManager *sharedInstance = nil;
  15. static dispatch_once_t onceToken = 0;
  16. dispatch_once(&onceToken,^{
  17. if (sharedInstance == nil)
  18. {
  19. sharedInstance = [[RCCManager alloc] init];
  20. }
  21. });
  22. return sharedInstance;
  23. }
  24. + (instancetype)sharedIntance
  25. {
  26. return [RCCManager sharedInstance];
  27. }
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self)
  32. {
  33. self.modulesRegistry = [@{} mutableCopy];
  34. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTReloadNotification object:nil];
  35. }
  36. return self;
  37. }
  38. -(void)clearModuleRegistry
  39. {
  40. [self.modulesRegistry removeAllObjects];
  41. }
  42. -(void)onRNReload
  43. {
  44. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  45. appDelegate.window.rootViewController = nil;
  46. [self clearModuleRegistry];
  47. }
  48. -(void)registerController:(UIViewController*)controller componentId:(NSString*)componentId componentType:(NSString*)componentType
  49. {
  50. if (controller == nil || componentId == nil)
  51. {
  52. return;
  53. }
  54. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  55. if (componentsDic == nil)
  56. {
  57. componentsDic = [@{} mutableCopy];
  58. self.modulesRegistry[componentType] = componentsDic;
  59. }
  60. /*
  61. TODO: we really want this error, but we need to unregister controllers when they dealloc
  62. if (componentsDic[componentId])
  63. {
  64. [self.sharedBridge.redBox showErrorMessage:[NSString stringWithFormat:@"Controllers: controller with id %@ is already registered. Make sure all of the controller id's you use are unique.", componentId]];
  65. }
  66. */
  67. componentsDic[componentId] = controller;
  68. }
  69. -(void)unregisterController:(UIViewController*)vc
  70. {
  71. if (vc == nil) return;
  72. for (NSString *key in [self.modulesRegistry allKeys])
  73. {
  74. NSMutableDictionary *componentsDic = self.modulesRegistry[key];
  75. for (NSString *componentID in [componentsDic allKeys])
  76. {
  77. UIViewController *tmpVc = componentsDic[componentID];
  78. if (tmpVc == vc)
  79. {
  80. [componentsDic removeObjectForKey:componentID];
  81. }
  82. }
  83. }
  84. }
  85. -(id)getControllerWithId:(NSString*)componentId componentType:(NSString*)componentType
  86. {
  87. if (componentId == nil)
  88. {
  89. return nil;
  90. }
  91. id component = nil;
  92. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  93. if (componentsDic != nil)
  94. {
  95. component = componentsDic[componentId];
  96. }
  97. return component;
  98. }
  99. -(NSString*) getIdForController:(UIViewController*)vc
  100. {
  101. if([vc isKindOfClass:[RCCViewController class]])
  102. {
  103. NSString *controllerId = ((RCCViewController*)vc).controllerId;
  104. if(controllerId != nil)
  105. {
  106. return controllerId;
  107. }
  108. }
  109. for (NSString *key in [self.modulesRegistry allKeys])
  110. {
  111. NSMutableDictionary *componentsDic = self.modulesRegistry[key];
  112. for (NSString *componentID in [componentsDic allKeys])
  113. {
  114. UIViewController *tmpVc = componentsDic[componentID];
  115. if (tmpVc == vc)
  116. {
  117. return componentID;
  118. }
  119. }
  120. }
  121. return nil;
  122. }
  123. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL
  124. {
  125. [self initBridgeWithBundleURL :bundleURL launchOptions:nil];
  126. }
  127. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL launchOptions:(NSDictionary *)launchOptions
  128. {
  129. if (self.sharedBridge) return;
  130. self.bundleURL = bundleURL;
  131. self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  132. [self showSplashScreen];
  133. }
  134. -(void)showSplashScreen
  135. {
  136. CGRect screenBounds = [UIScreen mainScreen].bounds;
  137. UIView *splashView = nil;
  138. NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  139. if (launchStoryBoard != nil)
  140. {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
  141. @try
  142. {
  143. splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
  144. if (splashView != nil)
  145. {
  146. splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height);
  147. }
  148. }
  149. @catch(NSException *e)
  150. {
  151. splashView = nil;
  152. }
  153. }
  154. else
  155. {//load the splash from the DEfault image or from LaunchImage in the xcassets
  156. CGFloat screenHeight = screenBounds.size.height;
  157. NSString* imageName = @"Default";
  158. if (screenHeight == 568)
  159. imageName = [imageName stringByAppendingString:@"-568h"];
  160. else if (screenHeight == 667)
  161. imageName = [imageName stringByAppendingString:@"-667h"];
  162. else if (screenHeight == 736)
  163. imageName = [imageName stringByAppendingString:@"-736h"];
  164. //xcassets LaunchImage files
  165. UIImage *image = [UIImage imageNamed:imageName];
  166. if (image == nil)
  167. {
  168. imageName = @"LaunchImage";
  169. if (screenHeight == 480)
  170. imageName = [imageName stringByAppendingString:@"-700"];
  171. if (screenHeight == 568)
  172. imageName = [imageName stringByAppendingString:@"-700-568h"];
  173. else if (screenHeight == 667)
  174. imageName = [imageName stringByAppendingString:@"-800-667h"];
  175. else if (screenHeight == 736)
  176. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  177. image = [UIImage imageNamed:imageName];
  178. }
  179. if (image != nil)
  180. {
  181. splashView = [[UIImageView alloc] initWithImage:image];
  182. }
  183. }
  184. if (splashView != nil)
  185. {
  186. UIViewController *splashVC = [[UIViewController alloc] init];
  187. splashVC.view = splashView;
  188. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  189. appDelegate.window.rootViewController = splashVC;
  190. [appDelegate.window makeKeyAndVisible];
  191. }
  192. }
  193. -(RCTBridge*)getBridge
  194. {
  195. return self.sharedBridge;
  196. }
  197. -(UIWindow*)getAppWindow
  198. {
  199. UIApplication *app = [UIApplication sharedApplication];
  200. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  201. return window;
  202. }
  203. #pragma mark - RCTBridgeDelegate methods
  204. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  205. {
  206. return self.bundleURL;
  207. }
  208. @end