react-native-navigation的迁移库

RNN.m 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #import "RNN.h"
  2. #import "RNNEventEmitter.h"
  3. #import <React/RCTBridge.h>
  4. #import "RNNSplashScreen.h"
  5. #import "RNNBridgeModule.h"
  6. #import "RNNReactRootViewCreator.h"
  7. @interface RNN() <RCTBridgeDelegate>
  8. @property NSURL* jsCodeLocation;
  9. @property (readwrite) RCTBridge* bridge;
  10. @property (readwrite) RNNEventEmitter* eventEmitter;
  11. @property (readwrite) RNNStore *store;
  12. @end
  13. @implementation RNN
  14. # pragma mark public
  15. +(instancetype)instance
  16. {
  17. static RNN *sharedInstance = nil;
  18. static dispatch_once_t onceToken = 0;
  19. dispatch_once(&onceToken,^{
  20. if (sharedInstance == nil)
  21. {
  22. sharedInstance = [[RNN alloc] init];
  23. }
  24. });
  25. return sharedInstance;
  26. }
  27. -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
  28. self.jsCodeLocation = jsCodeLocation;
  29. self.eventEmitter = [RNNEventEmitter new];
  30. UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  31. UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
  32. [RNNSplashScreen show];
  33. [self registerForJsEvents];
  34. // this will load the JS bundle
  35. self.bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  36. }
  37. # pragma mark - RCTBridgeDelegate
  38. -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  39. return self.jsCodeLocation;
  40. }
  41. -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  42. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:self.store];
  43. RNNCommandsHandler* commandsHandler = [[RNNCommandsHandler alloc]initWithStore:[RNN instance].store controllerFactory:controllerFactory];
  44. RNNBridgeModule* bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
  45. return @[bridgeModule];
  46. }
  47. # pragma mark - private
  48. -(void)registerForJsEvents {
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptLoaded) name:RCTJavaScriptDidLoadNotification object:self.bridge];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptWillLoad) name:RCTJavaScriptWillStartLoadingNotification object:self.bridge];
  51. }
  52. -(void)onJavaScriptWillLoad {
  53. self.store = [RNNStore new];
  54. [self resetRootViewControllerOnlyOnJSDevReload];
  55. }
  56. -(void)onJavaScriptLoaded {
  57. self.store.isReadyToReceiveCommands = true;
  58. [self.eventEmitter sendOnAppLaunched];
  59. }
  60. -(void)resetRootViewControllerOnlyOnJSDevReload {
  61. if(![UIApplication.sharedApplication.delegate.window.rootViewController isKindOfClass:[RNNSplashScreen class]]) {
  62. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  63. }
  64. }
  65. @end