Ran Greenberg 7 years ago
parent
commit
720e07f872

+ 22
- 18
ios/RNN.m View File

9
 
9
 
10
 @interface RNN() <RCTBridgeDelegate>
10
 @interface RNN() <RCTBridgeDelegate>
11
 
11
 
12
-@property NSURL* jsCodeLocation;
13
-@property RCTBridge* bridge;
14
-@property RNNEventEmitter* eventEmitter;
15
-@property RNNStore *store;
12
+
16
 
13
 
17
 @end
14
 @end
18
 
15
 
19
-@implementation RNN
16
+@implementation RNN {
17
+	NSURL *_jsCodeLocation;
18
+	RCTBridge *_bridge;
19
+	RNNEventEmitter *_eventEmitter;
20
+	RNNStore *_store;
21
+}
20
 
22
 
21
 +(instancetype) sharedInstance {
23
 +(instancetype) sharedInstance {
22
 	static RNN *instance = nil;
24
 	static RNN *instance = nil;
34
 # pragma mark public
36
 # pragma mark public
35
 
37
 
36
 -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
38
 -(void)bootstrap:(NSURL *)jsCodeLocation launchOptions:(NSDictionary *)launchOptions {
37
-	self.jsCodeLocation = jsCodeLocation;
38
-	self.eventEmitter = [RNNEventEmitter new];
39
+	_jsCodeLocation = jsCodeLocation;
40
+	_eventEmitter = [RNNEventEmitter new];
39
 	
41
 	
40
 	UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
42
 	UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
41
 	UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
43
 	UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
45
 	[self registerForJsEvents];
47
 	[self registerForJsEvents];
46
 	
48
 	
47
 	// this will load the JS bundle
49
 	// this will load the JS bundle
48
-	self.bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
50
+	_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
49
 }
51
 }
50
 
52
 
51
 # pragma mark - RCTBridgeDelegate
53
 # pragma mark - RCTBridgeDelegate
52
 
54
 
53
 -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
55
 -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
54
-	return self.jsCodeLocation;
56
+	return _jsCodeLocation;
55
 }
57
 }
56
 
58
 
57
 -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
59
 -(NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
58
 	
60
 	
59
-	RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:self.store];
60
-	RNNCommandsHandler* commandsHandler = [[RNNCommandsHandler alloc]initWithStore:self.store controllerFactory:controllerFactory];
61
+	RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:[RNNReactRootViewCreator new] store:_store];
62
+	RNNCommandsHandler *commandsHandler = [[RNNCommandsHandler alloc]initWithStore:_store controllerFactory:controllerFactory];
63
+	
64
+	RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
65
+	RNNEventEmitter *eventEmitter = [[RNNEventEmitter alloc] initWithBridge:bridge];
61
 	
66
 	
62
-	RNNBridgeModule* bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:commandsHandler];
63
-	return @[bridgeModule];
67
+	return @[bridgeModule,eventEmitter];
64
 }
68
 }
65
 
69
 
66
 # pragma mark - private
70
 # pragma mark - private
67
 
71
 
68
 -(void)registerForJsEvents {
72
 -(void)registerForJsEvents {
69
-	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptLoaded) name:RCTJavaScriptDidLoadNotification object:self.bridge];
70
-	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptWillLoad) name:RCTJavaScriptWillStartLoadingNotification	object:self.bridge];
73
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptLoaded) name:RCTJavaScriptDidLoadNotification object:_bridge];
74
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onJavaScriptWillLoad) name:RCTJavaScriptWillStartLoadingNotification	object:_bridge];
71
 }
75
 }
72
 
76
 
73
 
77
 
74
 -(void)onJavaScriptWillLoad {
78
 -(void)onJavaScriptWillLoad {
75
-	self.store = [RNNStore new];
79
+	_store = [RNNStore new];
76
 	[self resetRootViewControllerOnlyOnJSDevReload];
80
 	[self resetRootViewControllerOnlyOnJSDevReload];
77
 }
81
 }
78
 
82
 
79
 -(void)onJavaScriptLoaded {
83
 -(void)onJavaScriptLoaded {
80
-	self.store.isReadyToReceiveCommands = true;
81
-	[self.eventEmitter sendOnAppLaunched];
84
+	_store.isReadyToReceiveCommands = true;
85
+	[_eventEmitter sendOnAppLaunched];
82
 }
86
 }
83
 
87
 
84
 -(void)resetRootViewControllerOnlyOnJSDevReload {
88
 -(void)resetRootViewControllerOnlyOnJSDevReload {

+ 2
- 0
ios/RNNEventEmitter.h View File

6
 
6
 
7
 @interface RNNEventEmitter : RCTEventEmitter <RCTBridgeModule>
7
 @interface RNNEventEmitter : RCTEventEmitter <RCTBridgeModule>
8
 
8
 
9
+-(instancetype)initWithBridge:(RCTBridge*)bridge;
10
+
9
 -(void)sendOnAppLaunched;
11
 -(void)sendOnAppLaunched;
10
 
12
 
11
 -(void)sendContainerStart:(NSString*)containerId;
13
 -(void)sendContainerStart:(NSString*)containerId;

+ 12
- 4
ios/RNNEventEmitter.m View File

1
-
2
 #import "RNNEventEmitter.h"
1
 #import "RNNEventEmitter.h"
3
-#import "RNN.h"
4
 
2
 
5
-@implementation RNNEventEmitter
3
+@implementation RNNEventEmitter {
4
+	RCTBridge *_bridge;
5
+}
6
 
6
 
7
 RCT_EXPORT_MODULE();
7
 RCT_EXPORT_MODULE();
8
 
8
 
10
 static NSString* const containerStart	= @"RNN.containerStart";
10
 static NSString* const containerStart	= @"RNN.containerStart";
11
 static NSString* const containerStop	= @"RNN.containerStop";
11
 static NSString* const containerStop	= @"RNN.containerStop";
12
 
12
 
13
+-(instancetype)initWithBridge:(RCTBridge*)bridge {
14
+	self = [super init];
15
+	
16
+	_bridge = bridge;
17
+	
18
+	return self;
19
+}
20
+
13
 -(NSArray<NSString *> *)supportedEvents
21
 -(NSArray<NSString *> *)supportedEvents
14
 {
22
 {
15
 	return @[onAppLaunched, containerStart, containerStop];
23
 	return @[onAppLaunched, containerStart, containerStop];
36
 
44
 
37
 -(void)send:(NSString *)eventName body:(id)body
45
 -(void)send:(NSString *)eventName body:(id)body
38
 {
46
 {
39
-	[[[RNN instance].bridge moduleForClass:[RNNEventEmitter class]] sendEventWithName:eventName body:body];
47
+	[[_bridge moduleForClass:[RNNEventEmitter class]] sendEventWithName:eventName body:body];
40
 }
48
 }
41
 
49
 
42
 @end
50
 @end

+ 2
- 8
ios/RNNReactRootViewCreator.h View File

1
-//
2
-//  RNNReactRootViewCreator.h
3
-//  ReactNativeNavigation
4
-//
5
-//  Created by Ran Greenberg on 08/02/2017.
6
-//  Copyright © 2017 Wix. All rights reserved.
7
-//
8
-
9
 #import <Foundation/Foundation.h>
1
 #import <Foundation/Foundation.h>
10
 #import "RNNRootViewCreator.h"
2
 #import "RNNRootViewCreator.h"
11
 
3
 
4
+#import <React/RCTBridge.h>
5
+
12
 @interface RNNReactRootViewCreator : NSObject <RNNRootViewCreator>
6
 @interface RNNReactRootViewCreator : NSObject <RNNRootViewCreator>
13
 
7
 
14
 @end
8
 @end

+ 17
- 2
ios/RNNReactRootViewCreator.m View File

3
 #import "RNN.h"
3
 #import "RNN.h"
4
 #import <React/RCTRootView.h>
4
 #import <React/RCTRootView.h>
5
 
5
 
6
+@interface RNNReactRootViewCreator ()
7
+
8
+@property RCTBridge *bridge;
9
+
10
+@end
11
+
6
 @implementation RNNReactRootViewCreator
12
 @implementation RNNReactRootViewCreator
7
 
13
 
8
-- (UIView*)createRootView:(NSString*)name rootViewId:(NSString*)rootViewId{
14
+-(instancetype)initWithBridge:(RCTBridge*)bridge {
15
+	self = [super init];
16
+	
17
+	self.bridge = bridge;
18
+	
19
+	return self;
20
+	
21
+}
22
+
23
+- (UIView*)createRootView:(NSString*)name rootViewId:(NSString*)rootViewId {
9
 	if (!rootViewId) {
24
 	if (!rootViewId) {
10
 		@throw [NSException exceptionWithName:@"MissingViewId" reason:@"Missing view id" userInfo:nil];
25
 		@throw [NSException exceptionWithName:@"MissingViewId" reason:@"Missing view id" userInfo:nil];
11
 	}
26
 	}
12
 	
27
 	
13
-	UIView *view = [[RCTRootView alloc] initWithBridge:[RNN instance].bridge
28
+	UIView *view = [[RCTRootView alloc] initWithBridge:self.bridge
14
 										 moduleName:name
29
 										 moduleName:name
15
 								  initialProperties:@{@"id": rootViewId}];
30
 								  initialProperties:@{@"id": rootViewId}];
16
 	return view;
31
 	return view;

+ 4
- 1
ios/RNNRootViewController.h View File

3
 #import <UIKit/UIKit.h>
3
 #import <UIKit/UIKit.h>
4
 #import "RNNLayoutNode.h"
4
 #import "RNNLayoutNode.h"
5
 #import "RNNRootViewCreator.h"
5
 #import "RNNRootViewCreator.h"
6
+#import "RNNEventEmitter.h"
6
 
7
 
7
 @interface RNNRootViewController : UIViewController
8
 @interface RNNRootViewController : UIViewController
8
 
9
 
9
--(instancetype)initWithNode:(RNNLayoutNode*)node rootViewCreator:(id<RNNRootViewCreator>)creator;
10
+-(instancetype)initWithNode:(RNNLayoutNode*)node
11
+			rootViewCreator:(id<RNNRootViewCreator>)creator
12
+			   eventEmitter:(RNNEventEmitter*)eventEmitter;
10
 
13
 
11
 @end
14
 @end

+ 5
- 4
ios/RNNRootViewController.m View File

6
 @interface RNNRootViewController()
6
 @interface RNNRootViewController()
7
 @property NSString* containerId;
7
 @property NSString* containerId;
8
 @property NSString* containerName;
8
 @property NSString* containerName;
9
+@property RNNEventEmitter *eventEmitter;
9
 @end
10
 @end
10
 
11
 
11
 @implementation RNNRootViewController
12
 @implementation RNNRootViewController
12
 
13
 
13
--(instancetype)initWithNode:(RNNLayoutNode*)node rootViewCreator:(id<RNNRootViewCreator>)creator
14
-{
14
+-(instancetype)initWithNode:(RNNLayoutNode*)node rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter*)eventEmitter {
15
 	self = [super init];
15
 	self = [super init];
16
 	self.containerId = node.nodeId;
16
 	self.containerId = node.nodeId;
17
 	self.containerName = node.data[@"name"];
17
 	self.containerName = node.data[@"name"];
18
+	self.eventEmitter = eventEmitter;
18
 	
19
 	
19
 	self.view = [creator createRootView:self.containerName rootViewId:self.containerId];
20
 	self.view = [creator createRootView:self.containerName rootViewId:self.containerId];
20
 	return self;
21
 	return self;
23
 -(void)viewDidAppear:(BOOL)animated
24
 -(void)viewDidAppear:(BOOL)animated
24
 {
25
 {
25
 	[super viewDidAppear:animated];
26
 	[super viewDidAppear:animated];
26
-	[[RNN instance].eventEmitter sendContainerStart:self.containerId];
27
+	[self.eventEmitter sendContainerStart:self.containerId];
27
 }
28
 }
28
 
29
 
29
 -(void)viewDidDisappear:(BOOL)animated
30
 -(void)viewDidDisappear:(BOOL)animated
30
 {
31
 {
31
 	[super viewDidDisappear:animated];
32
 	[super viewDidDisappear:animated];
32
-	[[RNN instance].eventEmitter sendContainerStop:self.containerId];
33
+	[self.eventEmitter sendContainerStop:self.containerId];
33
 }
34
 }
34
 
35
 
35
 
36