react-native-navigation的迁移库

RNNBridgeManager.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #import "RNNBridgeManager.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 "RNNComponentViewCreator.h"
  8. #import "RNNReactRootViewCreator.h"
  9. #import "RNNReactComponentRegistry.h"
  10. @interface RNNBridgeManager() <RCTBridgeDelegate>
  11. @property (nonatomic, strong, readwrite) RCTBridge *bridge;
  12. @property (nonatomic, strong, readwrite) RNNExternalComponentStore *store;
  13. @property (nonatomic, strong, readwrite) RNNReactComponentRegistry *componentRegistry;
  14. @property (nonatomic, strong, readonly) RNNOverlayManager *overlayManager;
  15. @property (nonatomic, strong, readonly) RNNModalManager *modalManager;
  16. @end
  17. @implementation RNNBridgeManager {
  18. NSURL* _jsCodeLocation;
  19. NSDictionary* _launchOptions;
  20. id<RCTBridgeDelegate> _delegate;
  21. RCTBridge* _bridge;
  22. UIWindow* _mainWindow;
  23. RNNExternalComponentStore* _store;
  24. RNNCommandsHandler* _commandsHandler;
  25. }
  26. - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions bridgeManagerDelegate:(id<RCTBridgeDelegate>)delegate mainWindow:(UIWindow *)mainWindow {
  27. if (self = [super init]) {
  28. _mainWindow = mainWindow;
  29. _jsCodeLocation = jsCodeLocation;
  30. _launchOptions = launchOptions;
  31. _delegate = delegate;
  32. _overlayManager = [RNNOverlayManager new];
  33. _store = [RNNExternalComponentStore new];
  34. _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(onJavaScriptLoaded)
  37. name:RCTJavaScriptDidLoadNotification
  38. object:nil];
  39. [[NSNotificationCenter defaultCenter] addObserver:self
  40. selector:@selector(onJavaScriptWillLoad)
  41. name:RCTJavaScriptWillStartLoadingNotification
  42. object:nil];
  43. [[NSNotificationCenter defaultCenter] addObserver:self
  44. selector:@selector(onBridgeWillReload)
  45. name:RCTBridgeWillReloadNotification
  46. object:nil];
  47. }
  48. return self;
  49. }
  50. - (void)setJSCodeLocation:(NSURL *)jsCodeLocation {
  51. _jsCodeLocation = jsCodeLocation;
  52. }
  53. - (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
  54. [_store registerExternalComponent:name callback:callback];
  55. }
  56. - (NSArray *)extraModulesFromDelegate {
  57. if ([_delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
  58. return [_delegate extraModulesForBridge:_bridge];
  59. }
  60. return nil;
  61. }
  62. # pragma mark - RCTBridgeDelegate
  63. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  64. if ([_delegate respondsToSelector:@selector(sourceURLForBridge:)]) {
  65. return [_delegate sourceURLForBridge:bridge];
  66. } else {
  67. return _jsCodeLocation;
  68. }
  69. }
  70. - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
  71. RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
  72. _modalManager = [[RNNModalManager alloc] initWithBridge:bridge];
  73. id<RNNComponentViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge eventEmitter:eventEmitter];
  74. _componentRegistry = [[RNNReactComponentRegistry alloc] initWithCreator:rootViewCreator];
  75. RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator eventEmitter:eventEmitter store:_store componentRegistry:_componentRegistry andBridge:bridge bottomTabsAttachModeFactory:[BottomTabsAttachModeFactory new]];
  76. _commandsHandler = [[RNNCommandsHandler alloc] initWithControllerFactory:controllerFactory eventEmitter:eventEmitter modalManager:_modalManager overlayManager:_overlayManager mainWindow:_mainWindow];
  77. RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];
  78. return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
  79. }
  80. # pragma mark - JavaScript & Bridge Notifications
  81. - (void)onJavaScriptWillLoad {
  82. [_componentRegistry clear];
  83. }
  84. - (void)onJavaScriptLoaded {
  85. [_commandsHandler setReadyToReceiveCommands:true];
  86. [[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
  87. }
  88. - (void)onBridgeWillReload {
  89. [_overlayManager dismissAllOverlays];
  90. [_modalManager dismissAllModalsSynchronosly];
  91. [_componentRegistry clear];
  92. UIApplication.sharedApplication.delegate.window.rootViewController = nil;
  93. }
  94. @end