Browse Source

Refactor ReactNativeNavigation class into a developer-controllable class (#2480)

This allows developers more control over how the app is launched, in a backwards-compatible way.
Eli Perkins 6 years ago
parent
commit
09d78271f9

+ 10
- 0
lib/ios/RNNBridgeManager.h View File

@@ -0,0 +1,10 @@
1
+#import <Foundation/Foundation.h>
2
+#import <React/RCTBridge.h>
3
+
4
+@interface RNNBridgeManager : NSObject <RCTBridgeDelegate>
5
+
6
+- (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions;
7
+
8
+@property (readonly, nonatomic, strong) RCTBridge *bridge;
9
+
10
+@end

+ 85
- 0
lib/ios/RNNBridgeManager.m View File

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

+ 0
- 2
lib/ios/ReactNativeNavigation.h View File

@@ -1,6 +1,4 @@
1
-
2 1
 #import <Foundation/Foundation.h>
3
-#import <UIKit/UIKit.h>
4 2
 
5 3
 @interface ReactNativeNavigation : NSObject
6 4
 

+ 8
- 80
lib/ios/ReactNativeNavigation.m View File

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

+ 8
- 0
lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj View File

@@ -57,6 +57,8 @@
57 57
 		268692831E5054F800E2C612 /* RNNStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 268692811E5054F800E2C612 /* RNNStore.m */; };
58 58
 		26916C981E4B9E7700D13680 /* RNNReactRootViewCreator.h in Headers */ = {isa = PBXBuildFile; fileRef = 26916C961E4B9E7700D13680 /* RNNReactRootViewCreator.h */; };
59 59
 		26916C991E4B9E7700D13680 /* RNNReactRootViewCreator.m in Sources */ = {isa = PBXBuildFile; fileRef = 26916C971E4B9E7700D13680 /* RNNReactRootViewCreator.m */; };
60
+		2DCD9195200014A900EDC75D /* RNNBridgeManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DCD9193200014A900EDC75D /* RNNBridgeManager.h */; };
61
+		2DCD9196200014A900EDC75D /* RNNBridgeManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DCD9194200014A900EDC75D /* RNNBridgeManager.m */; };
60 62
 		504AFE641FFE53070076E904 /* RNNOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 504AFE621FFE53070076E904 /* RNNOptions.h */; };
61 63
 		504AFE651FFE53070076E904 /* RNNOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 504AFE631FFE53070076E904 /* RNNOptions.m */; };
62 64
 		504AFE741FFFF0540076E904 /* RNNTopTabsOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 504AFE721FFFF0540076E904 /* RNNTopTabsOptions.h */; };
@@ -221,6 +223,8 @@
221 223
 		26916C941E4B9CCC00D13680 /* RNNRootViewCreator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNRootViewCreator.h; sourceTree = "<group>"; };
222 224
 		26916C961E4B9E7700D13680 /* RNNReactRootViewCreator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNReactRootViewCreator.h; sourceTree = "<group>"; };
223 225
 		26916C971E4B9E7700D13680 /* RNNReactRootViewCreator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNReactRootViewCreator.m; sourceTree = "<group>"; };
226
+		2DCD9193200014A900EDC75D /* RNNBridgeManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNBridgeManager.h; sourceTree = "<group>"; };
227
+		2DCD9194200014A900EDC75D /* RNNBridgeManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNBridgeManager.m; sourceTree = "<group>"; };
224 228
 		504AFE621FFE53070076E904 /* RNNOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNOptions.h; sourceTree = "<group>"; };
225 229
 		504AFE631FFE53070076E904 /* RNNOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNOptions.m; sourceTree = "<group>"; };
226 230
 		504AFE721FFFF0540076E904 /* RNNTopTabsOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNTopTabsOptions.h; sourceTree = "<group>"; };
@@ -553,6 +557,8 @@
553 557
 				214545271F4DC7ED006E8DA1 /* Helpers */,
554 558
 				7BA500731E2544B9001B9E1B /* ReactNativeNavigation.h */,
555 559
 				7BA500741E2544B9001B9E1B /* ReactNativeNavigation.m */,
560
+				2DCD9193200014A900EDC75D /* RNNBridgeManager.h */,
561
+				2DCD9194200014A900EDC75D /* RNNBridgeManager.m */,
556 562
 				268692801E5054F800E2C612 /* RNNStore.h */,
557 563
 				268692811E5054F800E2C612 /* RNNStore.m */,
558 564
 				7B4928061E70415400555040 /* RNNCommandsHandler.h */,
@@ -676,6 +682,7 @@
676 682
 				E8A5CD621F49114F00E89D0D /* RNNElement.h in Headers */,
677 683
 				504AFE741FFFF0540076E904 /* RNNTopTabsOptions.h in Headers */,
678 684
 				E8E5182E1F83A48B000467AC /* RNNTransitionStateHolder.h in Headers */,
685
+				2DCD9195200014A900EDC75D /* RNNBridgeManager.h in Headers */,
679 686
 				7B1126A91E2D2B6C00F9B03B /* RNNControllerFactory.h in Headers */,
680 687
 				263905D61E4C94970023D7D3 /* RNNSideMenuController.h in Headers */,
681 688
 				263905C81E4C6F440023D7D3 /* SidebarFlipboardAnimation.h in Headers */,
@@ -822,6 +829,7 @@
822 829
 				50F5DFC21F407A8C001A00BC /* RNNTabBarController.m in Sources */,
823 830
 				263905BC1E4C6F440023D7D3 /* RCCDrawerHelper.m in Sources */,
824 831
 				2145452A1F4DC85F006E8DA1 /* RCTHelpers.m in Sources */,
832
+				2DCD9196200014A900EDC75D /* RNNBridgeManager.m in Sources */,
825 833
 				263905C11E4C6F440023D7D3 /* SidebarAirbnbAnimation.m in Sources */,
826 834
 				263905D71E4C94970023D7D3 /* RNNSideMenuController.m in Sources */,
827 835
 				507F43CA1FF4F9CC00D9425B /* RNNTopTabOptions.m in Sources */,