forked from walmartlabs/react-native-orientation-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCTOrientationListener.m
81 lines (63 loc) · 2.35 KB
/
RCTOrientationListener.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// RCTOrientationListener.m
//
// Created by Ken Wheeler on 9/9/15.
// Copyright (c) 2015 Facebook. All rights reserved.
//
#import "RCTOrientationListener.h"
#import "RCTBridge.h"
@implementation RCTOrientationListener
@synthesize bridge = _bridge;
- (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification
{
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *deviceStr = [currentDevice model];
// Add this "timeout" inside deviceOrientationDidChange, because the interface orientation isn't updated yet
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
NSString *orientationStr = [self getOrientationStr:orientation];
[_bridge.eventDispatcher sendDeviceEventWithName:@"orientationDidChange"
body:@{@"orientation": orientationStr,@"device": deviceStr}];
});
}
- (NSString *)getOrientationStr: (UIInterfaceOrientation)orientation {
NSString *orientationStr;
switch (orientation) {
case UIInterfaceOrientationPortrait:
orientationStr = @"PORTRAIT";
break;
case UIInterfaceOrientationPortraitUpsideDown:
orientationStr = @"PORTRAITUPSIDEDOWN";
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
orientationStr = @"LANDSCAPE";
break;
default:
orientationStr = @"UNKNOWN";
break;
}
return orientationStr;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(getOrientation:(RCTResponseSenderBlock)callback)
{
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *deviceStr = [currentDevice model];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
NSString *orientationStr = [self getOrientationStr:orientation];
NSArray *orientationArray = @[orientationStr, deviceStr];
callback(orientationArray);
}
@end