2017-03-17 23:47:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-03-17 23:47:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTDeviceInfo.h"
|
|
|
|
|
|
|
|
#import "RCTAccessibilityManager.h"
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2018-01-29 19:07:37 +00:00
|
|
|
#import "RCTUIUtils.h"
|
2017-03-17 23:47:51 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
@implementation RCTDeviceInfo {
|
|
|
|
#if !TARGET_OS_TV
|
|
|
|
UIInterfaceOrientation _currentInterfaceOrientation;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-08-07 13:45:24 +00:00
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:47:51 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
_bridge = bridge;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(didReceiveNewContentSizeMultiplier)
|
|
|
|
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
|
|
|
|
object:_bridge.accessibilityManager];
|
|
|
|
#if !TARGET_OS_TV
|
|
|
|
_currentInterfaceOrientation = [RCTSharedApplication() statusBarOrientation];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(interfaceOrientationDidChange)
|
|
|
|
name:UIApplicationDidChangeStatusBarOrientationNotification
|
|
|
|
object:nil];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-06 02:10:28 +00:00
|
|
|
static BOOL RCTIsIPhoneX() {
|
|
|
|
static BOOL isIPhoneX = NO;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
|
|
|
|
isIPhoneX = CGSizeEqualToSize(
|
|
|
|
[UIScreen mainScreen].nativeBounds.size,
|
|
|
|
CGSizeMake(1125, 2436)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return isIPhoneX;
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:47:51 +00:00
|
|
|
static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
|
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
|
2018-01-29 19:07:37 +00:00
|
|
|
RCTDimensions dimensions = RCTGetDimensions(bridge.accessibilityManager.multiplier);
|
|
|
|
typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.
|
|
|
|
NSDictionary<NSString *, NSNumber *> *dims = @{
|
|
|
|
@"width": @(window.width),
|
|
|
|
@"height": @(window.height),
|
|
|
|
@"scale": @(window.scale),
|
|
|
|
@"fontScale": @(window.fontScale)
|
|
|
|
};
|
2017-03-17 23:47:51 +00:00
|
|
|
return @{
|
2018-01-29 19:07:37 +00:00
|
|
|
@"window": dims,
|
|
|
|
@"screen": dims
|
|
|
|
};
|
2017-03-17 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 11:58:13 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:47:51 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
2017-06-21 01:42:46 +00:00
|
|
|
RCTExecuteOnMainQueue(^{
|
2017-03-17 23:47:51 +00:00
|
|
|
self->_bridge = nil;
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
|
|
|
{
|
2017-10-03 05:49:59 +00:00
|
|
|
return @{
|
|
|
|
@"Dimensions": RCTExportedDimensions(_bridge),
|
2018-07-06 02:10:28 +00:00
|
|
|
// Note:
|
2018-07-08 20:06:38 +00:00
|
|
|
// This prop is deprecated and will be removed in a future release.
|
2018-07-06 02:10:28 +00:00
|
|
|
// Please use this only for a quick and temporary solution.
|
|
|
|
// Use <SafeAreaView> instead.
|
|
|
|
@"isIPhoneX_deprecated": @(RCTIsIPhoneX()),
|
2017-10-03 05:49:59 +00:00
|
|
|
};
|
2017-03-17 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveNewContentSizeMultiplier
|
|
|
|
{
|
2017-06-21 01:42:46 +00:00
|
|
|
RCTBridge *bridge = _bridge;
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
// Report the event across the bridge.
|
2017-03-17 23:47:51 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2017-06-21 01:42:46 +00:00
|
|
|
[bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
|
|
|
|
body:RCTExportedDimensions(bridge)];
|
2017-03-17 23:47:51 +00:00
|
|
|
#pragma clang diagnostic pop
|
2017-06-21 01:42:46 +00:00
|
|
|
});
|
2017-03-17 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 22:04:28 +00:00
|
|
|
#if !TARGET_OS_TV
|
|
|
|
|
2017-03-17 23:47:51 +00:00
|
|
|
- (void)interfaceOrientationDidChange
|
|
|
|
{
|
2017-06-21 01:42:46 +00:00
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[weakSelf _interfaceOrientationDidChange];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)_interfaceOrientationDidChange
|
|
|
|
{
|
2017-03-17 23:47:51 +00:00
|
|
|
UIInterfaceOrientation nextOrientation = [RCTSharedApplication() statusBarOrientation];
|
|
|
|
|
|
|
|
// Update when we go from portrait to landscape, or landscape to portrait
|
|
|
|
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
|
|
|
|
!UIInterfaceOrientationIsPortrait(nextOrientation)) ||
|
|
|
|
(UIInterfaceOrientationIsLandscape(_currentInterfaceOrientation) &&
|
|
|
|
!UIInterfaceOrientationIsLandscape(nextOrientation))) {
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
|
|
|
|
body:RCTExportedDimensions(_bridge)];
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
}
|
|
|
|
|
|
|
|
_currentInterfaceOrientation = nextOrientation;
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:04:28 +00:00
|
|
|
#endif // TARGET_OS_TV
|
|
|
|
|
2017-03-17 23:47:51 +00:00
|
|
|
|
|
|
|
@end
|