react-native-navigation的迁移库

ReactNativeNavigation.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #import "ReactNativeNavigation.h"
  2. #import <React/RCTBridge.h>
  3. #import "RNNEventEmitter.h"
  4. #import "RNNSplashScreen.h"
  5. #import "RNNBridgeModule.h"
  6. #import "RNNRootViewCreator.h"
  7. #import "RNNReactRootViewCreator.h"
  8. @interface ReactNativeNavigation() <RCTBridgeDelegate>
  9. @end
  10. @implementation ReactNativeNavigation {
  11. NSURL* _jsCodeLocation;
  12. NSDictionary* _launchOptions;
  13. RNNStore* _store;
  14. RCTBridge* _bridge;
  15. }
  16. # pragma mark - public API
  17. +(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  18. [[ReactNativeNavigation sharedInstance] bootstrap:jsCodeLocation launchOptions:launchOptions];
  19. }
  20. # pragma mark - instance
  21. +(instancetype) sharedInstance {
  22. static ReactNativeNavigation *instance = nil;
  23. static dispatch_once_t onceToken = 0;
  24. dispatch_once(&onceToken,^{
  25. if (instance == nil) {
  26. instance = [[ReactNativeNavigation alloc] init];
  27. }
  28. });
  29. return instance;
  30. }
  31. -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  32. _jsCodeLocation = jsCodeLocation;
  33. _launchOptions = launchOptions;
  34. _store = [RNNStore new];
  35. [RNNSplashScreen show];
  36. [self registerForJsEvents];
  37. [self createBridgeLoadJsAndThenInitDependencyGraph];
  38. }
  39. # pragma mark - RCTBridgeDelegate
  40. -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  41. return _jsCodeLocation;
  42. }
  43. /**
  44. * here we initialize all of our dependency graph
  45. */
  46. -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  47. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  48. id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
  49. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter];
  50. RNNCommandsHandler *commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory];
  51. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
  52. return @[bridgeModule,eventEmitter];
  53. }
  54. # pragma mark - js events
  55. -(void)onJavaScriptWillLoad {
  56. [_store clean];
  57. [self resetRootViewControllerOnlyOnJSDevReload];
  58. }
  59. -(void)onJavaScriptLoaded {
  60. [_store setReadyToReceiveCommands:true];
  61. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  62. }
  63. # pragma mark - private
  64. -(void)createBridgeLoadJsAndThenInitDependencyGraph {
  65. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
  66. }
  67. -(void)registerForJsEvents {
  68. [[NSNotificationCenter defaultCenter] addObserver:self
  69. selector:@selector(onJavaScriptLoaded)
  70. name:RCTJavaScriptDidLoadNotification
  71. object:nil];
  72. [[NSNotificationCenter defaultCenter] addObserver:self
  73. selector:@selector(onJavaScriptWillLoad)
  74. name:RCTJavaScriptWillStartLoadingNotification
  75. object:nil];
  76. }
  77. -(void)resetRootViewControllerOnlyOnJSDevReload {
  78. if(![UIApplication.sharedApplication.delegate.window.rootViewController isKindOfClass:[RNNSplashScreen class]]) {
  79. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  80. }
  81. }
  82. @end