react-native-navigation的迁移库

ReactNativeNavigation.m 3.1KB

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