react-native-navigation的迁移库

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. UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:nil];
  183. if (storyboard == nil)
  184. {
  185. return nil;
  186. }
  187. return storyboard.instantiateInitialViewController;
  188. }
  189. + (UIViewController *)viewControllerFromLaunchNibForScreenBounds:(CGRect)screenBounds
  190. {
  191. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  192. if (launchStoryboardName == nil)
  193. {
  194. return nil;
  195. }
  196. @try {
  197. id nibContents = [[NSBundle mainBundle] loadNibNamed:launchStoryboardName owner:self options:nil];
  198. if (!nibContents || [nibContents count] == 0)
  199. {
  200. return nil;
  201. }
  202. id firstObject = [nibContents firstObject];
  203. if (![firstObject isKindOfClass:[UIView class]])
  204. {
  205. return nil;
  206. }
  207. UIViewController* viewController = [[UIViewController alloc] init];
  208. viewController.view = (UIView *)firstObject;
  209. viewController.view.frame = screenBounds;
  210. return viewController;
  211. }
  212. @catch(NSException *exception) {
  213. return nil;
  214. }
  215. }
  216. + (UIViewController *)viewControllerFromLaunchImageForScreenBounds:(CGRect)screenBounds
  217. {
  218. //load the splash from the default image or from LaunchImage in the xcassets
  219. CGFloat screenHeight = screenBounds.size.height;
  220. NSString* imageName = @"Default";
  221. if (screenHeight == 568)
  222. imageName = [imageName stringByAppendingString:@"-568h"];
  223. else if (screenHeight == 667)
  224. imageName = [imageName stringByAppendingString:@"-667h"];
  225. else if (screenHeight == 736)
  226. imageName = [imageName stringByAppendingString:@"-736h"];
  227. //xcassets LaunchImage files
  228. UIImage *image = [UIImage imageNamed:imageName];
  229. if (image == nil)
  230. {
  231. imageName = @"LaunchImage";
  232. if (screenHeight == 480)
  233. imageName = [imageName stringByAppendingString:@"-700"];
  234. if (screenHeight == 568)
  235. imageName = [imageName stringByAppendingString:@"-700-568h"];
  236. else if (screenHeight == 667)
  237. imageName = [imageName stringByAppendingString:@"-800-667h"];
  238. else if (screenHeight == 736)
  239. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  240. else if (screenHeight == 812)
  241. imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"];
  242. else if (screenHeight == 1024)
  243. imageName = [imageName stringByAppendingString:@"-Portrait"];
  244. image = [UIImage imageNamed:imageName];
  245. }
  246. if (image == nil)
  247. {
  248. return nil;
  249. }
  250. UIViewController* viewController = [[UIViewController alloc] init];
  251. UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  252. viewController.view = imageView;
  253. viewController.view.frame = screenBounds;
  254. return viewController;
  255. }
  256. + (void)showSplashScreenViewController:(UIViewController *)viewController
  257. {
  258. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  259. appDelegate.window.rootViewController = viewController;
  260. [appDelegate.window makeKeyAndVisible];
  261. }
  262. -(NSDictionary*)getAppStyle
  263. {
  264. return [NSDictionary dictionaryWithDictionary:self.globalAppStyle];
  265. }
  266. -(void)setAppStyle:(NSDictionary*)appStyle
  267. {
  268. self.globalAppStyle = [NSDictionary dictionaryWithDictionary:appStyle];
  269. [self applyAppStyle];
  270. }
  271. -(void)applyAppStyle {
  272. id backButtonImage = self.globalAppStyle[@"backButtonImage"];
  273. UIImage *image = backButtonImage ? [RCTConvert UIImage:backButtonImage] : nil;
  274. [[UINavigationBar appearance] setBackIndicatorImage:image];
  275. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  276. }
  277. #pragma mark - RCTBridgeDelegate methods
  278. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  279. {
  280. return self.bundleURL;
  281. }
  282. @end