Update Dimensions when device orientation changes

Reviewed By: nicklockwood

Differential Revision: D2939877

fb-gh-sync-id: ec6161448bff34c07b93f19e1ee953657675bad5
shipit-source-id: ec6161448bff34c07b93f19e1ee953657675bad5
This commit is contained in:
Pieter De Baets
2016-03-15 05:48:40 -07:00
committed by Facebook Github Bot 7
parent f6853b8eac
commit b653d43e2e
2 changed files with 85 additions and 51 deletions

View File

@@ -201,17 +201,14 @@ static UIViewAnimationOptions UIViewAnimationOptionsFromRCTAnimationType(RCTAnim
NSDictionary *_componentDataByName;
NSMutableSet<id<RCTComponent>> *_bridgeTransactionListeners;
UIInterfaceOrientation _currentInterfaceOrientation;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
/**
* Declared in RCTBridge.
*/
extern NSString *RCTBridgeModuleNameForClass(Class cls);
- (void)didReceiveNewContentSizeMultiplier
{
__weak RCTUIManager *weakSelf = self;
@@ -225,6 +222,23 @@ extern NSString *RCTBridgeModuleNameForClass(Class cls);
});
}
- (void)interfaceOrientationWillChange:(NSNotification *)notification
{
UIInterfaceOrientation nextOrientation =
[notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
// Update when we go from portrait to landscape, or landscape to portrait
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
!UIInterfaceOrientationIsPortrait(nextOrientation)) ||
(UIInterfaceOrientationIsLandscape(_currentInterfaceOrientation) &&
!UIInterfaceOrientationIsLandscape(nextOrientation))) {
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(YES)];
}
_currentInterfaceOrientation = nextOrientation;
}
- (void)invalidate
{
/**
@@ -298,6 +312,11 @@ extern NSString *RCTBridgeModuleNameForClass(Class cls);
selector:@selector(didReceiveNewContentSizeMultiplier)
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
object:_bridge.accessibilityManager];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationWillChange:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
}
- (dispatch_queue_t)methodQueue
@@ -1231,7 +1250,7 @@ RCT_EXPORT_METHOD(takeSnapshot:(id /* NSString or NSNumber */)target
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
// Get view
UIView *view;
@@ -1367,21 +1386,31 @@ RCT_EXPORT_METHOD(clearJSResponder)
allJSConstants[name] = constantsNamespace;
}];
_currentInterfaceOrientation = [RCTSharedApplication() statusBarOrientation];
[allJSConstants addEntriesFromDictionary:@{
@"customBubblingEventTypes": bubblingEvents,
@"customDirectEventTypes": directEvents,
@"Dimensions": @{
@"window": @{
@"width": @(RCTScreenSize().width),
@"height": @(RCTScreenSize().height),
@"scale": @(RCTScreenScale()),
},
},
@"Dimensions": RCTExportedDimensions(NO)
}];
return allJSConstants;
}
static NSDictionary *RCTExportedDimensions(BOOL rotateBounds)
{
RCTAssertMainThread();
// Don't use RCTScreenSize since it the interface orientation doesn't apply to it
CGRect screenSize = [[UIScreen mainScreen] bounds];
return @{
@"window": @{
@"width": @(rotateBounds ? screenSize.size.height : screenSize.size.width),
@"height": @(rotateBounds ? screenSize.size.width : screenSize.size.height),
@"scale": @(RCTScreenScale()),
},
};
}
RCT_EXPORT_METHOD(configureNextLayoutAnimation:(NSDictionary *)config
withCallback:(RCTResponseSenderBlock)callback
errorCallback:(__unused RCTResponseSenderBlock)errorCallback)