Browse Source

refactored RNN

Daniel Zlotin 7 years ago
parent
commit
226fb39f6a
6 changed files with 72 additions and 45 deletions
  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 View File

5
 #import "RNNEventEmitter.h"
5
 #import "RNNEventEmitter.h"
6
 #import "RNNSplashScreen.h"
6
 #import "RNNSplashScreen.h"
7
 #import "RNNBridgeModule.h"
7
 #import "RNNBridgeModule.h"
8
+#import "RNNRootViewCreator.h"
8
 #import "RNNReactRootViewCreator.h"
9
 #import "RNNReactRootViewCreator.h"
9
 
10
 
10
 @interface RNN() <RCTBridgeDelegate>
11
 @interface RNN() <RCTBridgeDelegate>
12
 @end
13
 @end
13
 
14
 
14
 @implementation RNN {
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
 +(instancetype) sharedInstance {
22
 +(instancetype) sharedInstance {
34
 
36
 
35
 -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
37
 -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
36
 	_jsCodeLocation = jsCodeLocation;
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
 	[RNNSplashScreen show];
42
 	[RNNSplashScreen show];
42
 	
43
 	
43
 	[self registerForJsEvents];
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
 # pragma mark - RCTBridgeDelegate
49
 # pragma mark - RCTBridgeDelegate
52
 	return _jsCodeLocation;
52
 	return _jsCodeLocation;
53
 }
53
 }
54
 
54
 
55
--(void)onJavaScriptWillLoad {
56
-	_store = [RNNStore new];
57
-	[self resetRootViewControllerOnlyOnJSDevReload];
58
-}
59
-
60
 -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
55
 -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
61
 	RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] init];
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
 	RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
61
 	RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
67
 	
62
 	
68
 	return @[bridgeModule,eventEmitter];
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
 -(void)onJavaScriptLoaded {
73
 -(void)onJavaScriptLoaded {
72
-	_store.isReadyToReceiveCommands = YES;
74
+	[_store setReadyToReceiveCommands:true];
73
 	[[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
75
 	[[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
74
 }
76
 }
75
 
77
 
76
 # pragma mark - private
78
 # pragma mark - private
77
 
79
 
80
+-(void)createBridgeLoadJsThenInitDependencyGraph {
81
+	_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];
82
+}
83
+
78
 -(void)registerForJsEvents {
84
 -(void)registerForJsEvents {
79
 	[[NSNotificationCenter defaultCenter] addObserver:self
85
 	[[NSNotificationCenter defaultCenter] addObserver:self
80
 											 selector:@selector(onJavaScriptLoaded)
86
 											 selector:@selector(onJavaScriptLoaded)
92
 	}
98
 	}
93
 }
99
 }
94
 
100
 
95
-
96
 @end
101
 @end

+ 4
- 4
ios/RNNModalManager.m View File

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

+ 10
- 12
ios/RNNSplashScreen.m View File

4
 
4
 
5
 @implementation RNNSplashScreen
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
 	CGRect screenBounds = [UIScreen mainScreen].bounds;
12
 	CGRect screenBounds = [UIScreen mainScreen].bounds;
10
 	UIView *splashView = nil;
13
 	UIView *splashView = nil;
11
 	
14
 	
12
 	NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
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
 		@try
17
 		@try
16
 		{
18
 		{
17
 			splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
19
 			splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
25
 			splashView = nil;
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
 		CGFloat screenHeight = screenBounds.size.height;
31
 		CGFloat screenHeight = screenBounds.size.height;
31
 		
32
 		
32
 		NSString* imageName = @"Default";
33
 		NSString* imageName = @"Default";
39
 		
40
 		
40
 		//xcassets LaunchImage files
41
 		//xcassets LaunchImage files
41
 		UIImage *image = [UIImage imageNamed:imageName];
42
 		UIImage *image = [UIImage imageNamed:imageName];
42
-		if (image == nil)
43
-		{
43
+		if (image == nil) {
44
 			imageName = @"LaunchImage";
44
 			imageName = @"LaunchImage";
45
 			
45
 			
46
 			if (screenHeight == 480)
46
 			if (screenHeight == 480)
55
 			image = [UIImage imageNamed:imageName];
55
 			image = [UIImage imageNamed:imageName];
56
 		}
56
 		}
57
 		
57
 		
58
-		if (image != nil)
59
-		{
58
+		if (image != nil) {
60
 			splashView = [[UIImageView alloc] initWithImage:image];
59
 			splashView = [[UIImageView alloc] initWithImage:image];
61
 		}
60
 		}
62
 	}
61
 	}
63
 	
62
 	
64
-	if (splashView != nil)
65
-	{
63
+	if (splashView != nil) {
66
 		RNNSplashScreen *splashVC = [[RNNSplashScreen alloc] init];
64
 		RNNSplashScreen *splashVC = [[RNNSplashScreen alloc] init];
67
 		splashVC.view = splashView;
65
 		splashVC.view = splashView;
68
 		
66
 		

+ 9
- 5
ios/RNNStore.h View File

4
 
4
 
5
 @interface RNNStore : NSObject
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
 @end
18
 @end

+ 21
- 1
ios/RNNStore.m View File

7
 
7
 
8
 @implementation RNNStore {
8
 @implementation RNNStore {
9
 	NSMapTable* _containerStore;
9
 	NSMapTable* _containerStore;
10
+	NSMutableArray* _pendingModalIdsToDismiss;
11
+	BOOL _isReadyToReceiveCommands;
10
 }
12
 }
11
 
13
 
12
 -(instancetype)init {
14
 -(instancetype)init {
13
 	self = [super init];
15
 	self = [super init];
16
+	_isReadyToReceiveCommands = false;
14
 	_containerStore = [NSMapTable strongToWeakObjectsMapTable];
17
 	_containerStore = [NSMapTable strongToWeakObjectsMapTable];
15
-	self.modalsToDismissArray = [NSMutableArray new];
18
+	_pendingModalIdsToDismiss = [NSMutableArray new];
16
 	return self;
19
 	return self;
17
 }
20
 }
18
 
21
 
33
 	[_containerStore removeObjectForKey:containerId];
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
 @end
57
 @end

+ 4
- 4
playground/ios/playgroundTests/RNNModalManagerTest.m View File

9
 
9
 
10
 - (void)testDismissAllModalsCleansPendingModalsToDismiss {
10
 - (void)testDismissAllModalsCleansPendingModalsToDismiss {
11
 	RNNStore *store = [RNNStore new];
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
 	RNNModalManager *modalManager = [[RNNModalManager alloc] initWithStore:store];
16
 	RNNModalManager *modalManager = [[RNNModalManager alloc] initWithStore:store];
17
 	[modalManager dismissAllModals];
17
 	[modalManager dismissAllModals];
18
 	
18
 	
19
-	XCTAssertTrue([store.modalsToDismissArray count] == 0);
19
+	XCTAssertTrue([[store pendingModalIdsToDismiss] count] == 0);
20
 	
20
 	
21
 }
21
 }
22
 
22