Ran Greenberg 7 years ago
parent
commit
720e07f872

+ 22
- 18
ios/RNN.m View File

@@ -9,14 +9,16 @@
9 9
 
10 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 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 23
 +(instancetype) sharedInstance {
22 24
 	static RNN *instance = nil;
@@ -34,8 +36,8 @@
34 36
 # pragma mark public
35 37
 
36 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 42
 	UIApplication.sharedApplication.delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
41 43
 	UIApplication.sharedApplication.delegate.window.backgroundColor = [UIColor whiteColor];
@@ -45,40 +47,42 @@
45 47
 	[self registerForJsEvents];
46 48
 	
47 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 53
 # pragma mark - RCTBridgeDelegate
52 54
 
53 55
 -(NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
54
-	return self.jsCodeLocation;
56
+	return _jsCodeLocation;
55 57
 }
56 58
 
57 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 70
 # pragma mark - private
67 71
 
68 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 78
 -(void)onJavaScriptWillLoad {
75
-	self.store = [RNNStore new];
79
+	_store = [RNNStore new];
76 80
 	[self resetRootViewControllerOnlyOnJSDevReload];
77 81
 }
78 82
 
79 83
 -(void)onJavaScriptLoaded {
80
-	self.store.isReadyToReceiveCommands = true;
81
-	[self.eventEmitter sendOnAppLaunched];
84
+	_store.isReadyToReceiveCommands = true;
85
+	[_eventEmitter sendOnAppLaunched];
82 86
 }
83 87
 
84 88
 -(void)resetRootViewControllerOnlyOnJSDevReload {

+ 2
- 0
ios/RNNEventEmitter.h View File

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

+ 12
- 4
ios/RNNEventEmitter.m View File

@@ -1,8 +1,8 @@
1
-
2 1
 #import "RNNEventEmitter.h"
3
-#import "RNN.h"
4 2
 
5
-@implementation RNNEventEmitter
3
+@implementation RNNEventEmitter {
4
+	RCTBridge *_bridge;
5
+}
6 6
 
7 7
 RCT_EXPORT_MODULE();
8 8
 
@@ -10,6 +10,14 @@ static NSString* const onAppLaunched	= @"RNN.appLaunched";
10 10
 static NSString* const containerStart	= @"RNN.containerStart";
11 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 21
 -(NSArray<NSString *> *)supportedEvents
14 22
 {
15 23
 	return @[onAppLaunched, containerStart, containerStop];
@@ -36,7 +44,7 @@ static NSString* const containerStop	= @"RNN.containerStop";
36 44
 
37 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 50
 @end

+ 2
- 8
ios/RNNReactRootViewCreator.h View File

@@ -1,14 +1,8 @@
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 1
 #import <Foundation/Foundation.h>
10 2
 #import "RNNRootViewCreator.h"
11 3
 
4
+#import <React/RCTBridge.h>
5
+
12 6
 @interface RNNReactRootViewCreator : NSObject <RNNRootViewCreator>
13 7
 
14 8
 @end

+ 17
- 2
ios/RNNReactRootViewCreator.m View File

@@ -3,14 +3,29 @@
3 3
 #import "RNN.h"
4 4
 #import <React/RCTRootView.h>
5 5
 
6
+@interface RNNReactRootViewCreator ()
7
+
8
+@property RCTBridge *bridge;
9
+
10
+@end
11
+
6 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 24
 	if (!rootViewId) {
10 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 29
 										 moduleName:name
15 30
 								  initialProperties:@{@"id": rootViewId}];
16 31
 	return view;

+ 4
- 1
ios/RNNRootViewController.h View File

@@ -3,9 +3,12 @@
3 3
 #import <UIKit/UIKit.h>
4 4
 #import "RNNLayoutNode.h"
5 5
 #import "RNNRootViewCreator.h"
6
+#import "RNNEventEmitter.h"
6 7
 
7 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 14
 @end

+ 5
- 4
ios/RNNRootViewController.m View File

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