react-native-navigation的迁移库

RCCManager.m 7.0KB

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