Browse Source

refactor RNNBridgeModule into commandsHandler

Daniel Zlotin 7 years ago
parent
commit
30a58c0363

+ 14
- 53
ios/RNNBridgeModule.m View File

1
 #import "RNNBridgeModule.h"
1
 #import "RNNBridgeModule.h"
2
 
2
 
3
-#import "RNN.h"
4
-#import "RNNControllerFactory.h"
5
-#import "RNNReactRootViewCreator.h"
6
-#import "RNNStore.h"
7
-#import "RNNModalManager.h"
8
-#import "RNNNavigationStackManager.h"
3
+#import "RNNCommandsHandler.h"
9
 
4
 
10
 @implementation RNNBridgeModule
5
 @implementation RNNBridgeModule
11
 
6
 
12
 RCT_EXPORT_MODULE();
7
 RCT_EXPORT_MODULE();
13
 
8
 
14
-- (dispatch_queue_t)methodQueue
15
-{
9
+- (dispatch_queue_t)methodQueue {
16
 	return dispatch_get_main_queue();
10
 	return dispatch_get_main_queue();
17
 }
11
 }
18
 
12
 
19
 #pragma mark - JS interface
13
 #pragma mark - JS interface
20
 
14
 
21
-RCT_EXPORT_METHOD(setRoot:(NSDictionary*)layout)
22
-{
23
-	[self assertReady];
24
-	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
25
-	UIViewController *vc = [factory createLayoutAndSaveToStore:layout];
26
-	
27
-	UIApplication.sharedApplication.delegate.window.rootViewController = vc;
28
-	[UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
15
+RCT_EXPORT_METHOD(setRoot:(NSDictionary*)layout) {
16
+	[[RNNCommandsHandler new] setRoot:layout];
29
 }
17
 }
30
 
18
 
31
-RCT_EXPORT_METHOD(push:(NSString*)containerId layout:(NSDictionary*)layout)
32
-{
33
-	[self assertReady];
34
-	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
35
-	UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
36
-	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
37
-
38
-	[[[RNNNavigationStackManager alloc] initWithStore:[RNN instance].store] push:newVc onTop:vc animated:YES];
39
-}
40
-
41
-RCT_EXPORT_METHOD(pop:(NSString*)containerId)
42
-{
43
-	[self assertReady];
44
-	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
45
-	[[[RNNNavigationStackManager alloc] initWithStore:[RNN instance].store] pop:vc animated:YES];
46
-	[[RNN instance].store removeContainer:containerId];
19
+RCT_EXPORT_METHOD(push:(NSString*)containerId layout:(NSDictionary*)layout) {
20
+	[[RNNCommandsHandler new] push:containerId layout:layout];
47
 }
21
 }
48
 
22
 
49
-RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout)
50
-{
51
-	[self assertReady];
52
-	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
53
-	UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
54
-	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] showModal:newVc];
23
+RCT_EXPORT_METHOD(pop:(NSString*)containerId) {
24
+	[[RNNCommandsHandler new] pop:containerId];
55
 }
25
 }
56
 
26
 
57
-RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId)
58
-{
59
-	[self assertReady];
60
-	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissModal:containerId];
27
+RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout) {
28
+	[[RNNCommandsHandler new] showModal:layout];
61
 }
29
 }
62
 
30
 
63
-RCT_EXPORT_METHOD(dismissAllModals)
64
-{
65
-	[self assertReady];
66
-	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissAllModals];
31
+RCT_EXPORT_METHOD(dismissModal:(NSString*)containerId) {
32
+	[[RNNCommandsHandler new] dismissModal:containerId];
67
 }
33
 }
68
 
34
 
69
-#pragma mark - private
70
-
71
-- (void)assertReady
72
-{
73
-	if (![RNN instance].isReadyToReceiveCommands) {
74
-		@throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
75
-	}
35
+RCT_EXPORT_METHOD(dismissAllModals) {
36
+	[[RNNCommandsHandler new] dismissAllModals];
76
 }
37
 }
77
 
38
 
78
 @end
39
 @end

+ 19
- 0
ios/RNNCommandsHandler.h View File

1
+
2
+
3
+#import <Foundation/Foundation.h>
4
+
5
+@interface RNNCommandsHandler : NSObject
6
+
7
+-(void) setRoot:(NSDictionary*)layout;
8
+
9
+-(void) push:(NSString*)containerId layout:(NSDictionary*)layout;
10
+
11
+-(void) pop:(NSString*)containerId;
12
+
13
+-(void) showModal:(NSDictionary*)layout;
14
+
15
+-(void) dismissModal:(NSString*)containerId;
16
+
17
+-(void) dismissAllModals;
18
+
19
+@end

+ 66
- 0
ios/RNNCommandsHandler.m View File

1
+
2
+#import "RNNCommandsHandler.h"
3
+
4
+
5
+#import "RNN.h"
6
+#import "RNNControllerFactory.h"
7
+#import "RNNReactRootViewCreator.h"
8
+#import "RNNStore.h"
9
+#import "RNNModalManager.h"
10
+#import "RNNNavigationStackManager.h"
11
+
12
+@implementation RNNCommandsHandler
13
+
14
+#pragma mark - public
15
+
16
+-(void) setRoot:(NSDictionary*)layout {
17
+	[self assertReady];
18
+	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
19
+	UIViewController *vc = [factory createLayoutAndSaveToStore:layout];
20
+	
21
+	UIApplication.sharedApplication.delegate.window.rootViewController = vc;
22
+	[UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
23
+}
24
+
25
+-(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
26
+	[self assertReady];
27
+	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
28
+	UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
29
+	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
30
+	
31
+	[[[RNNNavigationStackManager alloc] initWithStore:[RNN instance].store] push:newVc onTop:vc animated:YES];
32
+}
33
+
34
+-(void) pop:(NSString*)containerId {
35
+	[self assertReady];
36
+	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
37
+	[[[RNNNavigationStackManager alloc] initWithStore:[RNN instance].store] pop:vc animated:YES];
38
+	[[RNN instance].store removeContainer:containerId];
39
+}
40
+
41
+-(void) showModal:(NSDictionary*)layout {
42
+	[self assertReady];
43
+	RNNControllerFactory *factory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:[RNN instance].store];
44
+	UIViewController *newVc = [factory createLayoutAndSaveToStore:layout];
45
+	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] showModal:newVc];
46
+}
47
+
48
+-(void) dismissModal:(NSString*)containerId {
49
+	[self assertReady];
50
+	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissModal:containerId];
51
+}
52
+
53
+-(void) dismissAllModals {
54
+	[self assertReady];
55
+	[[[RNNModalManager alloc] initWithStore:[RNN instance].store] dismissAllModals];
56
+}
57
+
58
+#pragma mark - private
59
+
60
+- (void)assertReady {
61
+	if (![RNN instance].isReadyToReceiveCommands) {
62
+		@throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
63
+	}
64
+}
65
+
66
+@end

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

60
 		7B1126A61E2D2B6C00F9B03B /* RNNBridgeModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BBFE5421E25330E002A6182 /* RNNBridgeModule.h */; };
60
 		7B1126A61E2D2B6C00F9B03B /* RNNBridgeModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BBFE5421E25330E002A6182 /* RNNBridgeModule.h */; };
61
 		7B1126A71E2D2B6C00F9B03B /* RNNEventEmitter.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */; };
61
 		7B1126A71E2D2B6C00F9B03B /* RNNEventEmitter.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */; };
62
 		7B1126A91E2D2B6C00F9B03B /* RNNControllerFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC9346C1E26886E00EFA125 /* RNNControllerFactory.h */; };
62
 		7B1126A91E2D2B6C00F9B03B /* RNNControllerFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC9346C1E26886E00EFA125 /* RNNControllerFactory.h */; };
63
+		7B4928081E70415400555040 /* RNNCommandsHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B4928061E70415400555040 /* RNNCommandsHandler.h */; };
64
+		7B4928091E70415400555040 /* RNNCommandsHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B4928071E70415400555040 /* RNNCommandsHandler.m */; };
63
 		7BA500751E2544B9001B9E1B /* ReactNativeNavigation.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BA500741E2544B9001B9E1B /* ReactNativeNavigation.m */; };
65
 		7BA500751E2544B9001B9E1B /* ReactNativeNavigation.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BA500741E2544B9001B9E1B /* ReactNativeNavigation.m */; };
64
 		7BA500781E254908001B9E1B /* RNNSplashScreen.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BA500771E254908001B9E1B /* RNNSplashScreen.m */; };
66
 		7BA500781E254908001B9E1B /* RNNSplashScreen.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BA500771E254908001B9E1B /* RNNSplashScreen.m */; };
65
 		7BBFE5441E25330E002A6182 /* RNNBridgeModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BBFE5431E25330E002A6182 /* RNNBridgeModule.m */; };
67
 		7BBFE5441E25330E002A6182 /* RNNBridgeModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BBFE5431E25330E002A6182 /* RNNBridgeModule.m */; };
133
 		26916C971E4B9E7700D13680 /* RNNReactRootViewCreator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNReactRootViewCreator.m; sourceTree = "<group>"; };
135
 		26916C971E4B9E7700D13680 /* RNNReactRootViewCreator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNReactRootViewCreator.m; sourceTree = "<group>"; };
134
 		7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNEventEmitter.h; sourceTree = "<group>"; };
136
 		7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNEventEmitter.h; sourceTree = "<group>"; };
135
 		7B11269F1E2D263F00F9B03B /* RNNEventEmitter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNEventEmitter.m; sourceTree = "<group>"; };
137
 		7B11269F1E2D263F00F9B03B /* RNNEventEmitter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNEventEmitter.m; sourceTree = "<group>"; };
138
+		7B4928061E70415400555040 /* RNNCommandsHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNCommandsHandler.h; sourceTree = "<group>"; };
139
+		7B4928071E70415400555040 /* RNNCommandsHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNCommandsHandler.m; sourceTree = "<group>"; };
136
 		7BA500731E2544B9001B9E1B /* ReactNativeNavigation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReactNativeNavigation.h; sourceTree = "<group>"; };
140
 		7BA500731E2544B9001B9E1B /* ReactNativeNavigation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReactNativeNavigation.h; sourceTree = "<group>"; };
137
 		7BA500741E2544B9001B9E1B /* ReactNativeNavigation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativeNavigation.m; sourceTree = "<group>"; };
141
 		7BA500741E2544B9001B9E1B /* ReactNativeNavigation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativeNavigation.m; sourceTree = "<group>"; };
138
 		7BA500761E254908001B9E1B /* RNNSplashScreen.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNSplashScreen.h; sourceTree = "<group>"; };
142
 		7BA500761E254908001B9E1B /* RNNSplashScreen.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNSplashScreen.h; sourceTree = "<group>"; };
272
 				7BBFE5601E253F97002A6182 /* RNN.m */,
276
 				7BBFE5601E253F97002A6182 /* RNN.m */,
273
 				268692801E5054F800E2C612 /* RNNStore.h */,
277
 				268692801E5054F800E2C612 /* RNNStore.h */,
274
 				268692811E5054F800E2C612 /* RNNStore.m */,
278
 				268692811E5054F800E2C612 /* RNNStore.m */,
279
+				7B4928061E70415400555040 /* RNNCommandsHandler.h */,
280
+				7B4928071E70415400555040 /* RNNCommandsHandler.m */,
275
 				7BD721F31E2D3AA100724059 /* Bridge */,
281
 				7BD721F31E2D3AA100724059 /* Bridge */,
276
 				7B1E4C4B1E2D173700C3A525 /* Controllers */,
282
 				7B1E4C4B1E2D173700C3A525 /* Controllers */,
277
 				263905881E4C6F440023D7D3 /* RNNSideMenu */,
283
 				263905881E4C6F440023D7D3 /* RNNSideMenu */,
304
 				263905B71E4C6F440023D7D3 /* UIViewController+MMDrawerController.h in Headers */,
310
 				263905B71E4C6F440023D7D3 /* UIViewController+MMDrawerController.h in Headers */,
305
 				263905BB1E4C6F440023D7D3 /* RCCDrawerHelper.h in Headers */,
311
 				263905BB1E4C6F440023D7D3 /* RCCDrawerHelper.h in Headers */,
306
 				263905CC1E4C6F440023D7D3 /* SidebarWunderlistAnimation.h in Headers */,
312
 				263905CC1E4C6F440023D7D3 /* SidebarWunderlistAnimation.h in Headers */,
313
+				7B4928081E70415400555040 /* RNNCommandsHandler.h in Headers */,
307
 				263905AE1E4C6F440023D7D3 /* MMDrawerBarButtonItem.h in Headers */,
314
 				263905AE1E4C6F440023D7D3 /* MMDrawerBarButtonItem.h in Headers */,
308
 				263905BD1E4C6F440023D7D3 /* RCCDrawerProtocol.h in Headers */,
315
 				263905BD1E4C6F440023D7D3 /* RCCDrawerProtocol.h in Headers */,
309
 				263905C21E4C6F440023D7D3 /* SidebarAnimation.h in Headers */,
316
 				263905C21E4C6F440023D7D3 /* SidebarAnimation.h in Headers */,
414
 				263905CD1E4C6F440023D7D3 /* SidebarWunderlistAnimation.m in Sources */,
421
 				263905CD1E4C6F440023D7D3 /* SidebarWunderlistAnimation.m in Sources */,
415
 				263905CF1E4C6F440023D7D3 /* TheSidebarController.m in Sources */,
422
 				263905CF1E4C6F440023D7D3 /* TheSidebarController.m in Sources */,
416
 				263905AF1E4C6F440023D7D3 /* MMDrawerBarButtonItem.m in Sources */,
423
 				263905AF1E4C6F440023D7D3 /* MMDrawerBarButtonItem.m in Sources */,
424
+				7B4928091E70415400555040 /* RNNCommandsHandler.m in Sources */,
417
 				7BBFE5611E253F97002A6182 /* RNN.m in Sources */,
425
 				7BBFE5611E253F97002A6182 /* RNN.m in Sources */,
418
 				268692831E5054F800E2C612 /* RNNStore.m in Sources */,
426
 				268692831E5054F800E2C612 /* RNNStore.m in Sources */,
419
 				7BC9346E1E26886E00EFA125 /* RNNControllerFactory.m in Sources */,
427
 				7BC9346E1E26886E00EFA125 /* RNNControllerFactory.m in Sources */,