浏览代码

refactored RNN

Daniel Zlotin 7 年前
父节点
当前提交
226fb39f6a
共有 6 个文件被更改,包括 72 次插入45 次删除
  1. 24
    19
      ios/RNN.m
  2. 4
    4
      ios/RNNModalManager.m
  3. 10
    12
      ios/RNNSplashScreen.m
  4. 9
    5
      ios/RNNStore.h
  5. 21
    1
      ios/RNNStore.m
  6. 4
    4
      playground/ios/playgroundTests/RNNModalManagerTest.m

+ 24
- 19
ios/RNN.m 查看文件

@@ -5,6 +5,7 @@
5 5
 #import "RNNEventEmitter.h"
6 6
 #import "RNNSplashScreen.h"
7 7
 #import "RNNBridgeModule.h"
8
+#import "RNNRootViewCreator.h"
8 9
 #import "RNNReactRootViewCreator.h"
9 10
 
10 11
 @interface RNN() <RCTBridgeDelegate>
@@ -12,9 +13,10 @@
12 13
 @end
13 14
 
14 15
 @implementation RNN {
15
-	NSURL *_jsCodeLocation;
16
-	RCTBridge *_bridge;
17
-	RNNStore *_store;
16
+	NSURL* _jsCodeLocation;
17
+	NSDictionary* _launchOptions;
18
+	RNNStore* _store;
19
+	RCTBridge* _bridge;
18 20
 }
19 21
 
20 22
 +(instancetype) sharedInstance {
@@ -34,16 +36,14 @@
34 36
 
35 37
 -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
36 38
 	_jsCodeLocation = jsCodeLocation;
37
-	
38
-	UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
39
-	UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
40
-	
39
+	_launchOptions = launchOptions;
40
+	_store = [RNNStore new];
41
+
41 42
 	[RNNSplashScreen show];
42 43
 	
43 44
 	[self registerForJsEvents];
44 45
 	
45
-	// this will load the JS bundle
46
-	_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
46
+	[self createBridgeLoadJsThenInitDependencyGraph];
47 47
 }
48 48
 
49 49
 # pragma mark - RCTBridgeDelegate
@@ -52,29 +52,35 @@
52 52
 	return _jsCodeLocation;
53 53
 }
54 54
 
55
--(void)onJavaScriptWillLoad {
56
-	_store = [RNNStore new];
57
-	[self resetRootViewControllerOnlyOnJSDevReload];
58
-}
59
-
60 55
 -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
61 56
 	RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
62 57
 	
63
-	RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:[[RNNReactRootViewCreator alloc]initWithBridge:bridge] store:_store eventEmitter:eventEmitter];
64
-	RNNCommandsHandler *commandsHandler = [[RNNCommandsHandler alloc]initWithStore:_store controllerFactory:controllerFactory];
65
-	
58
+	id<RNNRootViewCreator> rootViewCreator = [[RNNReactRootViewCreator alloc] initWithBridge:bridge];
59
+	RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator store:_store eventEmitter:eventEmitter];
60
+	RNNCommandsHandler *commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory];
66 61
 	RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
67 62
 	
68 63
 	return @[bridgeModule,eventEmitter];
69 64
 }
70 65
 
66
+# pragma mark - js events
67
+
68
+-(void)onJavaScriptWillLoad {
69
+	[_store clean];
70
+	[self resetRootViewControllerOnlyOnJSDevReload];
71
+}
72
+
71 73
 -(void)onJavaScriptLoaded {
72
-	_store.isReadyToReceiveCommands = YES;
74
+	[_store setReadyToReceiveCommands:true];
73 75
 	[[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
74 76
 }
75 77
 
76 78
 # pragma mark - private
77 79
 
80
+-(void)createBridgeLoadJsThenInitDependencyGraph {
81
+	_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
82
+}
83
+
78 84
 -(void)registerForJsEvents {
79 85
 	[[NSNotificationCenter defaultCenter] addObserver:self
80 86
 											 selector:@selector(onJavaScriptLoaded)
@@ -92,5 +98,4 @@
92 98
 	}
93 99
 }
94 100
 
95
-
96 101
 @end

+ 4
- 4
ios/RNNModalManager.m 查看文件

@@ -17,14 +17,14 @@
17 17
 }
18 18
 
19 19
 -(void)dismissModal:(NSString *)containerId {
20
-	[_store.modalsToDismissArray addObject:containerId];
20
+	[[_store pendingModalIdsToDismiss] addObject:containerId];
21 21
 	[self removePendingNextModalIfOnTop];
22 22
 }
23 23
 
24 24
 -(void)dismissAllModals {
25 25
 	UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
26 26
 	[root dismissViewControllerAnimated:YES completion:nil];
27
-	[_store.modalsToDismissArray removeAllObjects];
27
+	[[_store pendingModalIdsToDismiss] removeAllObjects];
28 28
 }
29 29
 
30 30
 
@@ -32,7 +32,7 @@
32 32
 
33 33
 
34 34
 -(void)removePendingNextModalIfOnTop {
35
-	NSString *containerId = [_store.modalsToDismissArray lastObject];
35
+	NSString *containerId = [[_store pendingModalIdsToDismiss] lastObject];
36 36
 	
37 37
 	UIViewController *modalToDismiss = [_store findContainerForId:containerId];
38 38
 	
@@ -42,7 +42,7 @@
42 42
 	
43 43
 	if (modalToDismiss == [self topPresentedVC]) {
44 44
 		[modalToDismiss dismissViewControllerAnimated:YES completion:^{
45
-			[_store.modalsToDismissArray removeObject:containerId];
45
+			[[_store pendingModalIdsToDismiss] removeObject:containerId];
46 46
 			[self removePendingNextModalIfOnTop];
47 47
 		}];
48 48
 	}

+ 10
- 12
ios/RNNSplashScreen.m 查看文件

@@ -4,14 +4,16 @@
4 4
 
5 5
 @implementation RNNSplashScreen
6 6
 
7
-+(void)show
8
-{
7
++(void)show {
8
+	
9
+	UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
10
+	UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
11
+	
9 12
 	CGRect screenBounds = [UIScreen mainScreen].bounds;
10 13
 	UIView *splashView = nil;
11 14
 	
12 15
 	NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
13
-	if (launchStoryBoard != nil)
14
-	{//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
16
+	if (launchStoryBoard != nil) {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
15 17
 		@try
16 18
 		{
17 19
 			splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
@@ -25,8 +27,7 @@
25 27
 			splashView = nil;
26 28
 		}
27 29
 	}
28
-	else
29
-	{//load the splash from the DEfault image or from LaunchImage in the xcassets
30
+	else {//load the splash from the DEfault image or from LaunchImage in the xcassets
30 31
 		CGFloat screenHeight = screenBounds.size.height;
31 32
 		
32 33
 		NSString* imageName = @"Default";
@@ -39,8 +40,7 @@
39 40
 		
40 41
 		//xcassets LaunchImage files
41 42
 		UIImage *image = [UIImage imageNamed:imageName];
42
-		if (image == nil)
43
-		{
43
+		if (image == nil) {
44 44
 			imageName = @"LaunchImage";
45 45
 			
46 46
 			if (screenHeight == 480)
@@ -55,14 +55,12 @@
55 55
 			image = [UIImage imageNamed:imageName];
56 56
 		}
57 57
 		
58
-		if (image != nil)
59
-		{
58
+		if (image != nil) {
60 59
 			splashView = [[UIImageView alloc] initWithImage:image];
61 60
 		}
62 61
 	}
63 62
 	
64
-	if (splashView != nil)
65
-	{
63
+	if (splashView != nil) {
66 64
 		RNNSplashScreen *splashVC = [[RNNSplashScreen alloc] init];
67 65
 		splashVC.view = splashView;
68 66
 		

+ 9
- 5
ios/RNNStore.h 查看文件

@@ -4,11 +4,15 @@
4 4
 
5 5
 @interface RNNStore : NSObject
6 6
 
7
-@property (nonatomic, strong) NSMutableArray *modalsToDismissArray;
8
-@property (atomic) BOOL isReadyToReceiveCommands;
7
+-(UIViewController*) findContainerForId:(NSString*)containerId;
8
+-(void) setContainer:(UIViewController*)viewController containerId:(NSString*)containerId;
9
+-(void) removeContainer:(NSString*)containerId;
9 10
 
10
-- (UIViewController*)findContainerForId:(NSString*)containerId;
11
-- (void)setContainer:(UIViewController*)viewController containerId:(NSString*)containerId;
12
-- (void)removeContainer:(NSString*)containerId;
11
+-(void) setReadyToReceiveCommands:(BOOL)isReady;
12
+-(BOOL) isReadyToReceiveCommands;
13
+
14
+-(NSMutableArray*) pendingModalIdsToDismiss;
15
+
16
+-(void) clean;
13 17
 
14 18
 @end

+ 21
- 1
ios/RNNStore.m 查看文件

@@ -7,12 +7,15 @@
7 7
 
8 8
 @implementation RNNStore {
9 9
 	NSMapTable* _containerStore;
10
+	NSMutableArray* _pendingModalIdsToDismiss;
11
+	BOOL _isReadyToReceiveCommands;
10 12
 }
11 13
 
12 14
 -(instancetype)init {
13 15
 	self = [super init];
16
+	_isReadyToReceiveCommands = false;
14 17
 	_containerStore = [NSMapTable strongToWeakObjectsMapTable];
15
-	self.modalsToDismissArray = [NSMutableArray new];
18
+	_pendingModalIdsToDismiss = [NSMutableArray new];
16 19
 	return self;
17 20
 }
18 21
 
@@ -33,5 +36,22 @@
33 36
 	[_containerStore removeObjectForKey:containerId];
34 37
 }
35 38
 
39
+-(void)setReadyToReceiveCommands:(BOOL)isReady {
40
+	_isReadyToReceiveCommands = isReady;
41
+}
42
+
43
+-(BOOL)isReadyToReceiveCommands {
44
+	return _isReadyToReceiveCommands;
45
+}
46
+
47
+-(NSMutableArray *)pendingModalIdsToDismiss {
48
+	return _pendingModalIdsToDismiss;
49
+}
50
+
51
+-(void)clean {
52
+	_isReadyToReceiveCommands = false;
53
+	[_pendingModalIdsToDismiss removeAllObjects];
54
+	[_containerStore removeAllObjects];
55
+}
36 56
 
37 57
 @end

+ 4
- 4
playground/ios/playgroundTests/RNNModalManagerTest.m 查看文件

@@ -9,14 +9,14 @@
9 9
 
10 10
 - (void)testDismissAllModalsCleansPendingModalsToDismiss {
11 11
 	RNNStore *store = [RNNStore new];
12
-	[store.modalsToDismissArray addObject:@"modal_id_1"];
13
-	[store.modalsToDismissArray addObject:@"modal_id_2"];
14
-	[store.modalsToDismissArray addObject:@"modal_id_3"];
12
+	[[store pendingModalIdsToDismiss] addObject:@"modal_id_1"];
13
+	[[store pendingModalIdsToDismiss] addObject:@"modal_id_2"];
14
+	[[store pendingModalIdsToDismiss] addObject:@"modal_id_3"];
15 15
 	
16 16
 	RNNModalManager *modalManager = [[RNNModalManager alloc] initWithStore:store];
17 17
 	[modalManager dismissAllModals];
18 18
 	
19
-	XCTAssertTrue([store.modalsToDismissArray count] == 0);
19
+	XCTAssertTrue([[store pendingModalIdsToDismiss] count] == 0);
20 20
 	
21 21
 }
22 22