react-native-navigation的迁移库

RCCManager.m 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 class] showSplashScreen];
  136. }
  137. -(RCTBridge*)getBridge
  138. {
  139. return self.sharedBridge;
  140. }
  141. -(UIWindow*)getAppWindow
  142. {
  143. UIApplication *app = [UIApplication sharedApplication];
  144. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  145. return window;
  146. }
  147. #pragma mark - Splash Screen
  148. + (void)showSplashScreen
  149. {
  150. UIViewController* viewControllerFromLaunchStoryboard;
  151. viewControllerFromLaunchStoryboard = [self viewControllerFromLaunchStoryboard];
  152. if (viewControllerFromLaunchStoryboard)
  153. {
  154. [self showSplashScreenViewController:viewControllerFromLaunchStoryboard];
  155. return;
  156. }
  157. CGRect screenBounds = [UIScreen mainScreen].bounds;
  158. UIViewController* viewControllerFromLaunchNib = [self viewControllerFromLaunchNibForScreenBounds:screenBounds];
  159. if (viewControllerFromLaunchNib)
  160. {
  161. [self showSplashScreenViewController:viewControllerFromLaunchNib];
  162. return;
  163. }
  164. UIViewController* viewControllerFromLaunchImage = [self viewControllerFromLaunchImageForScreenBounds:screenBounds];
  165. if (viewControllerFromLaunchImage)
  166. {
  167. [self showSplashScreenViewController:viewControllerFromLaunchImage];
  168. return;
  169. }
  170. UIViewController* viewController = [[UIViewController alloc] init];
  171. viewController.view.frame = screenBounds;
  172. viewController.view.backgroundColor = [UIColor whiteColor];
  173. [self showSplashScreenViewController:viewController];
  174. }
  175. + (UIViewController *)viewControllerFromLaunchStoryboard
  176. {
  177. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  178. if (launchStoryboardName == nil)
  179. {
  180. return nil;
  181. }
  182. @try {
  183. UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:nil];
  184. return storyboard.instantiateInitialViewController;
  185. }
  186. @catch(NSException *exception) {
  187. return nil;
  188. }
  189. }
  190. + (UIViewController *)viewControllerFromLaunchNibForScreenBounds:(CGRect)screenBounds
  191. {
  192. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  193. if (launchStoryboardName == nil)
  194. {
  195. return nil;
  196. }
  197. @try {
  198. id nibContents = [[NSBundle mainBundle] loadNibNamed:launchStoryboardName owner:self options:nil];
  199. if (!nibContents || [nibContents count] == 0)
  200. {
  201. return nil;
  202. }
  203. id firstObject = [nibContents firstObject];
  204. if (![firstObject isKindOfClass:[UIView class]])
  205. {
  206. return nil;
  207. }
  208. UIViewController* viewController = [[UIViewController alloc] init];
  209. viewController.view = (UIView *)firstObject;
  210. viewController.view.frame = screenBounds;
  211. return viewController;
  212. }
  213. @catch(NSException *exception) {
  214. return nil;
  215. }
  216. }
  217. + (UIViewController *)viewControllerFromLaunchImageForScreenBounds:(CGRect)screenBounds
  218. {
  219. //load the splash from the default image or from LaunchImage in the xcassets
  220. CGFloat screenHeight = screenBounds.size.height;
  221. NSString* imageName = @"Default";
  222. if (screenHeight == 568)
  223. imageName = [imageName stringByAppendingString:@"-568h"];
  224. else if (screenHeight == 667)
  225. imageName = [imageName stringByAppendingString:@"-667h"];
  226. else if (screenHeight == 736)
  227. imageName = [imageName stringByAppendingString:@"-736h"];
  228. //xcassets LaunchImage files
  229. UIImage *image = [UIImage imageNamed:imageName];
  230. if (image == nil)
  231. {
  232. imageName = @"LaunchImage";
  233. if (screenHeight == 480)
  234. imageName = [imageName stringByAppendingString:@"-700"];
  235. if (screenHeight == 568)
  236. imageName = [imageName stringByAppendingString:@"-700-568h"];
  237. else if (screenHeight == 667)
  238. imageName = [imageName stringByAppendingString:@"-800-667h"];
  239. else if (screenHeight == 736)
  240. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  241. else if (screenHeight == 812)
  242. imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"];
  243. else if (screenHeight == 1024)
  244. imageName = [imageName stringByAppendingString:@"-Portrait"];
  245. image = [UIImage imageNamed:imageName];
  246. }
  247. if (image == nil)
  248. {
  249. return nil;
  250. }
  251. UIViewController* viewController = [[UIViewController alloc] init];
  252. UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  253. viewController.view = imageView;
  254. viewController.view.frame = screenBounds;
  255. return viewController;
  256. }
  257. + (void)showSplashScreenViewController:(UIViewController *)viewController
  258. {
  259. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  260. appDelegate.window.rootViewController = viewController;
  261. [appDelegate.window makeKeyAndVisible];
  262. }
  263. -(NSDictionary*)getAppStyle
  264. {
  265. return [NSDictionary dictionaryWithDictionary:self.globalAppStyle];
  266. }
  267. -(void)setAppStyle:(NSDictionary*)appStyle
  268. {
  269. self.globalAppStyle = [NSDictionary dictionaryWithDictionary:appStyle];
  270. [self applyAppStyle];
  271. }
  272. -(void)applyAppStyle {
  273. id backButtonImage = self.globalAppStyle[@"backButtonImage"];
  274. UIImage *image = backButtonImage ? [RCTConvert UIImage:backButtonImage] : nil;
  275. [[UINavigationBar appearance] setBackIndicatorImage:image];
  276. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  277. }
  278. #pragma mark - RCTBridgeDelegate methods
  279. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  280. {
  281. return self.bundleURL;
  282. }
  283. @end