-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathViewport.m
73 lines (56 loc) · 1.8 KB
/
Viewport.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
#import "Viewport.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTUtils.h"
static NSDictionary *RCTCurrentDimensions()
{
static NSDictionary *dimensions;
CGSize frameSize = [UIScreen mainScreen].applicationFrame.size;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
&& UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
frameSize = CGSizeMake(frameSize.height, frameSize.width);
}
dimensions = @{
@"width": @(frameSize.width),
@"height": @(frameSize.height)
};
return dimensions;
}
@implementation Viewport
{
NSDictionary *_lastKnownDimensions;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
#pragma mark - Lifecycle
- (instancetype)init
{
if ((self = [super init])) {
_lastKnownDimensions = RCTCurrentDimensions();
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Notification methods
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
_lastKnownDimensions = RCTCurrentDimensions();
[_bridge.eventDispatcher sendDeviceEventWithName:@"dimensionsDidChange" body:_lastKnownDimensions];
}
#pragma mark - Public API
/**
* Get the current dimensions of the viewport
*/
RCT_EXPORT_METHOD(getCurrentDimensions:(RCTResponseSenderBlock)callback)
{
_lastKnownDimensions = RCTCurrentDimensions();
callback(@[_lastKnownDimensions]);
}
@end