react-native-navigation的迁移库

RCCManager.m 6.6KB

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