Browse Source

refactoring a bunch of ios stuff

Daniel Zlotin 7 years ago
parent
commit
765754a26c

+ 12
- 1
lib/ios/RNNCommandsHandler.m View File

37
 
37
 
38
 -(void) setOptions:(NSString*)containerId options:(NSDictionary*)options {
38
 -(void) setOptions:(NSString*)containerId options:(NSDictionary*)options {
39
 	[self assertReady];
39
 	[self assertReady];
40
+	
40
 	UIViewController* vc = [_store findContainerForId:containerId];
41
 	UIViewController* vc = [_store findContainerForId:containerId];
41
 	if([vc isKindOfClass:[RNNRootViewController class]]) {
42
 	if([vc isKindOfClass:[RNNRootViewController class]]) {
42
 		RNNRootViewController* rootVc = (RNNRootViewController*)vc;
43
 		RNNRootViewController* rootVc = (RNNRootViewController*)vc;
47
 
48
 
48
 -(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
49
 -(void) push:(NSString*)containerId layout:(NSDictionary*)layout {
49
 	[self assertReady];
50
 	[self assertReady];
51
+	
50
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
52
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
51
 	[_navigationStackManager push:newVc onTop:containerId];
53
 	[_navigationStackManager push:newVc onTop:containerId];
52
 }
54
 }
53
 
55
 
54
 -(void) pop:(NSString*)containerId {
56
 -(void) pop:(NSString*)containerId {
55
 	[self assertReady];
57
 	[self assertReady];
58
+	
56
 	[_navigationStackManager pop:containerId];
59
 	[_navigationStackManager pop:containerId];
57
 }
60
 }
58
 
61
 
59
 -(void) popTo:(NSString*)containerId {
62
 -(void) popTo:(NSString*)containerId {
60
 	[self assertReady];
63
 	[self assertReady];
64
+	
61
 	[_navigationStackManager popTo:containerId];
65
 	[_navigationStackManager popTo:containerId];
62
 }
66
 }
63
 
67
 
64
 -(void) popToRoot:(NSString*)containerId {
68
 -(void) popToRoot:(NSString*)containerId {
65
 	[self assertReady];
69
 	[self assertReady];
70
+	
66
 	[_navigationStackManager popToRoot:containerId];
71
 	[_navigationStackManager popToRoot:containerId];
67
 }
72
 }
68
 
73
 
69
 -(void) showModal:(NSDictionary*)layout {
74
 -(void) showModal:(NSDictionary*)layout {
70
 	[self assertReady];
75
 	[self assertReady];
76
+	
71
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
77
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
72
 	[_modalManager showModal:newVc];
78
 	[_modalManager showModal:newVc];
73
 }
79
 }
74
 
80
 
75
 -(void) dismissModal:(NSString*)containerId {
81
 -(void) dismissModal:(NSString*)containerId {
76
 	[self assertReady];
82
 	[self assertReady];
83
+	
77
 	[_modalManager dismissModal:containerId];
84
 	[_modalManager dismissModal:containerId];
78
 }
85
 }
79
 
86
 
80
 -(void) dismissAllModals {
87
 -(void) dismissAllModals {
81
 	[self assertReady];
88
 	[self assertReady];
89
+	
82
 	[_modalManager dismissAllModals];
90
 	[_modalManager dismissAllModals];
83
 }
91
 }
84
 
92
 
86
 
94
 
87
 -(void) assertReady {
95
 -(void) assertReady {
88
 	if (!_store.isReadyToReceiveCommands) {
96
 	if (!_store.isReadyToReceiveCommands) {
89
-		@throw [NSException exceptionWithName:@"BridgeNotLoadedError" reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called." userInfo:nil];
97
+		[[NSException exceptionWithName:@"BridgeNotLoadedError"
98
+								reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
99
+							  userInfo:nil]
100
+		 raise];
90
 	}
101
 	}
91
 }
102
 }
92
 
103
 

+ 1
- 0
lib/ios/RNNNavigationOptions.h View File

8
 @property (nonatomic, strong) NSNumber* statusBarHidden;
8
 @property (nonatomic, strong) NSNumber* statusBarHidden;
9
 @property (nonatomic, strong) NSString* title;
9
 @property (nonatomic, strong) NSString* title;
10
 
10
 
11
+-(instancetype)init;
11
 -(instancetype)initWithDict:(NSDictionary *)navigationOptions;
12
 -(instancetype)initWithDict:(NSDictionary *)navigationOptions;
12
 
13
 
13
 -(void)applyOn:(UIViewController*)viewController;
14
 -(void)applyOn:(UIViewController*)viewController;

+ 9
- 5
lib/ios/RNNNavigationOptions.m View File

3
 
3
 
4
 @implementation RNNNavigationOptions
4
 @implementation RNNNavigationOptions
5
 
5
 
6
+-(instancetype)init {
7
+	return [self initWithDict:@{}];
8
+}
6
 
9
 
7
 -(instancetype)initWithDict:(NSDictionary *)navigationOptions {
10
 -(instancetype)initWithDict:(NSDictionary *)navigationOptions {
8
-	self = [super init];
9
-	self.topBarBackgroundColor = [navigationOptions objectForKey:@"topBarBackgroundColor"];
10
-	self.statusBarHidden = [navigationOptions objectForKey:@"statusBarHidden"];
11
-	self.title = [navigationOptions objectForKey:@"title"];
12
-	self.topBarTextColor = [navigationOptions objectForKey:@"topBarTextColor"];
11
+	if(self = [super init]) {
12
+		self.topBarBackgroundColor = [navigationOptions objectForKey:@"topBarBackgroundColor"];
13
+		self.statusBarHidden = [navigationOptions objectForKey:@"statusBarHidden"];
14
+		self.title = [navigationOptions objectForKey:@"title"];
15
+		self.topBarTextColor = [navigationOptions objectForKey:@"topBarTextColor"];
16
+	}
13
 	return self;
17
 	return self;
14
 }
18
 }
15
 
19
 

+ 10
- 5
lib/ios/RNNRootViewController.m View File

13
 
13
 
14
 @implementation RNNRootViewController
14
 @implementation RNNRootViewController
15
 
15
 
16
--(instancetype)initWithName:(NSString*)name withOptions:(RNNNavigationOptions*)options withContainerId:(NSString*)containerId rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter*)eventEmitter {
16
+-(instancetype)initWithName:(NSString*)name
17
+				withOptions:(RNNNavigationOptions*)options
18
+			withContainerId:(NSString*)containerId
19
+			rootViewCreator:(id<RNNRootViewCreator>)creator
20
+			   eventEmitter:(RNNEventEmitter*)eventEmitter {
17
 	self = [super init];
21
 	self = [super init];
18
 	self.containerId = containerId;
22
 	self.containerId = containerId;
19
 	self.containerName = name;
23
 	self.containerName = name;
30
 	return self;
34
 	return self;
31
 }
35
 }
32
 
36
 
37
+-(void)viewWillAppear:(BOOL)animated{
38
+	[super viewWillAppear:animated];
39
+	[self.navigationOptions applyOn:self];
40
+}
41
+
33
 - (BOOL)prefersStatusBarHidden {
42
 - (BOOL)prefersStatusBarHidden {
34
 	return [self.navigationOptions.statusBarHidden boolValue]; // || self.navigationController.isNavigationBarHidden;
43
 	return [self.navigationOptions.statusBarHidden boolValue]; // || self.navigationController.isNavigationBarHidden;
35
 }
44
 }
44
 	[self.eventEmitter sendContainerStop:self.containerId];
53
 	[self.eventEmitter sendContainerStop:self.containerId];
45
 }
54
 }
46
 
55
 
47
--(void)viewWillAppear:(BOOL)animated{
48
-	[super viewWillAppear:animated];
49
-	[self.navigationOptions applyOn:self];
50
-}
51
 /**
56
 /**
52
  *	fix for #877, #878
57
  *	fix for #877, #878
53
  */
58
  */

+ 27
- 37
lib/ios/ReactNativeNavigationTests/RNNCommandsHandlerTest.m View File

7
 
7
 
8
 @interface RNNCommandsHandlerTest : XCTestCase
8
 @interface RNNCommandsHandlerTest : XCTestCase
9
 
9
 
10
-@property (nonatomic, strong) id<RNNRootViewCreator> creator;
11
-@property (nonatomic, strong) NSString* pageName;
12
-@property (nonatomic, strong) NSString* containerId;
13
-@property (nonatomic, strong) id emitter;
14
 @property (nonatomic, strong) RNNStore* store;
10
 @property (nonatomic, strong) RNNStore* store;
15
-@property (nonatomic, strong) RNNNavigationOptions* options;
16
-@property (nonatomic, strong) RNNRootViewController* viewController;
17
-@property (nonatomic, strong) RNNCommandsHandler* cmdHandler;
11
+@property (nonatomic, strong) RNNCommandsHandler* uut;
18
 
12
 
19
 @end
13
 @end
20
 
14
 
22
 
16
 
23
 - (void)setUp {
17
 - (void)setUp {
24
 	[super setUp];
18
 	[super setUp];
25
-	self.creator = [[RNNTestRootViewCreator alloc] init];
26
-	self.pageName = @"somename";
27
-	self.containerId = @"cntId";
28
-	self.emitter = nil;
29
-	self.options = [[RNNNavigationOptions alloc] initWithDict:@{@"title" : @"static title"}];
30
-	self.store = [RNNStore new];
31
-	self.viewController = [[RNNRootViewController alloc] initWithName:self.pageName withOptions:self.options withContainerId:self.containerId rootViewCreator:self.creator eventEmitter:self.emitter];
32
-	[self.store setReadyToReceiveCommands:true];
33
-	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
34
-	[self.store setContainer:self.viewController containerId:self.containerId];
35
-	self.cmdHandler = [[RNNCommandsHandler alloc] initWithStore:self.store controllerFactory:nil];
19
+	self.store = [[RNNStore alloc] init];
20
+	self.uut = [[RNNCommandsHandler alloc] initWithStore:self.store controllerFactory:nil];
36
 }
21
 }
37
 
22
 
38
 
23
 
39
 - (void)testAssertReadyForEachMethodThrowsExceptoins {
24
 - (void)testAssertReadyForEachMethodThrowsExceptoins {
40
-	RNNStore *store = [RNNStore new];
41
-	[store setReadyToReceiveCommands:NO];
42
-	RNNCommandsHandler *uut = [[RNNCommandsHandler alloc] initWithStore:store controllerFactory:nil];
43
-	
44
-	NSArray* methods = [self getPublicMethodNamesForObject:uut];
25
+	NSArray* methods = [self getPublicMethodNamesForObject:self.uut];
45
 	
26
 	
46
 	for (NSString* methodName in methods) {
27
 	for (NSString* methodName in methods) {
28
+		
29
+		__strong id uut = self.uut;
47
 		SEL s = NSSelectorFromString(methodName);
30
 		SEL s = NSSelectorFromString(methodName);
48
 		IMP imp = [uut methodForSelector:s];
31
 		IMP imp = [uut methodForSelector:s];
49
 		void (*func)(id, SEL) = (void *)imp;
32
 		void (*func)(id, SEL) = (void *)imp;
33
+		
50
 		XCTAssertThrowsSpecificNamed(func(uut,s), NSException, @"BridgeNotLoadedError");
34
 		XCTAssertThrowsSpecificNamed(func(uut,s), NSException, @"BridgeNotLoadedError");
51
 	}
35
 	}
52
 }
36
 }
68
 		NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
52
 		NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
69
 		
53
 		
70
 		// filter skippedMethods
54
 		// filter skippedMethods
71
-		if (![skipMethods containsObject:methodName]) {
55
+		if (methodName && ![skipMethods containsObject:methodName]) {
72
 			[result addObject:methodName];
56
 			[result addObject:methodName];
73
 		}
57
 		}
74
 	}
58
 	}
76
 	return result;
60
 	return result;
77
 }
61
 }
78
 
62
 
79
--(void)testDynamicTopBarBackgroundColor_validColor {
80
-	NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
81
-	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
82
-	[self.cmdHandler setOptions:self.containerId options:dictFromJs];
83
-	XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
84
-}
85
-
86
 -(void)testDynamicStylesMergeWithStaticStyles {
63
 -(void)testDynamicStylesMergeWithStaticStyles {
64
+	RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] init];
65
+	[initialOptions setTitle:@"the title"];
66
+	RNNRootViewController* vc = [[RNNRootViewController alloc] initWithName:@"name"
67
+																withOptions:initialOptions
68
+															withContainerId:@"containerId"
69
+															rootViewCreator:[[RNNTestRootViewCreator alloc] init]
70
+															   eventEmitter:nil];
71
+	UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
72
+	[vc viewWillAppear:false];
73
+	XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
74
+	
75
+	[self.store setReadyToReceiveCommands:true];
76
+	[self.store setContainer:vc containerId:@"containerId"];
77
+	
87
 	NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
78
 	NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
88
 	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
79
 	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
89
-	[self.cmdHandler setOptions:self.containerId options:dictFromJs];
90
-	XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
91
-	XCTAssertTrue([self.viewController.navigationItem.title isEqual:@"static title"]);
80
+	
81
+	[self.uut setOptions:@"containerId" options:dictFromJs];
82
+	
83
+	XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
84
+	XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
92
 }
85
 }
93
 
86
 
94
-
95
-
96
-
97
 @end
87
 @end