From 71b498b08259d183b22426b2425edc048b7520f5 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Wed, 29 Nov 2017 22:49:57 -0800 Subject: [PATCH] Introducing `[RCTUIManager _executeBlockWithShadowView:forTag:]` Summary: New super simple abstraction in RCTUIManager. Nothing really changed and RCTUIManager became shorter. Reviewed By: rsnara Differential Revision: D5990342 fbshipit-source-id: b38397b789a999168ac14625585065eda73d328f --- React/Modules/RCTUIManager.h | 2 +- React/Modules/RCTUIManager.m | 81 ++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/React/Modules/RCTUIManager.h b/React/Modules/RCTUIManager.h index a6c349f67..59547c220 100644 --- a/React/Modules/RCTUIManager.h +++ b/React/Modules/RCTUIManager.h @@ -86,7 +86,7 @@ RCT_EXTERN NSString *const RCTUIManagerWillUpdateViewsDueToContentSizeMultiplier * Use `UIViewNoIntrinsicMetric` to ignore a dimension. * The `size` must NOT include padding and border. */ -- (void)setIntrinsicContentSize:(CGSize)size forView:(UIView *)view; +- (void)setIntrinsicContentSize:(CGSize)intrinsicContentSize forView:(UIView *)view; /** * Update the background color of a view. The source of truth for diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 975f4df19..5574d270c 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -327,39 +327,46 @@ static NSDictionary *deviceOrientationEventBody(UIDeviceOrientation orientation) return _shadowViewRegistry[reactTag]; } -- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView +- (void)_executeBlockWithShadowView:(void (^)(RCTShadowView *shadowView))block forTag:(NSNumber *)tag { RCTAssertMainQueue(); - NSNumber *reactTag = rootView.reactTag; - RCTExecuteOnUIManagerQueue(^{ - RCTRootShadowView *shadowView = (RCTRootShadowView *)self->_shadowViewRegistry[reactTag]; - RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag); - RCTAssert([shadowView isKindOfClass:[RCTRootShadowView class]], @"Located shadow view (with tag #%@) is actually not root view.", reactTag); - if (CGSizeEqualToSize(availableSize, shadowView.availableSize)) { + RCTExecuteOnUIManagerQueue(^{ + RCTShadowView *shadowView = self->_shadowViewRegistry[tag]; + + if (shadowView == nil) { + RCTLogInfo(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", tag); return; } - shadowView.availableSize = availableSize; - [self setNeedsLayout]; + block(shadowView); }); } +- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView +{ + RCTAssertMainQueue(); + [self _executeBlockWithShadowView:^(RCTShadowView *shadowView) { + RCTAssert([shadowView isKindOfClass:[RCTRootShadowView class]], @"Located shadow view is actually not root view."); + + RCTRootShadowView *rootShadowView = (RCTRootShadowView *)shadowView; + + if (CGSizeEqualToSize(availableSize, rootShadowView.availableSize)) { + return; + } + + rootShadowView.availableSize = availableSize; + [self setNeedsLayout]; + } forTag:rootView.reactTag]; +} + - (void)setLocalData:(NSObject *)localData forView:(UIView *)view { RCTAssertMainQueue(); - NSNumber *tag = view.reactTag; - - RCTExecuteOnUIManagerQueue(^{ - RCTShadowView *shadowView = self->_shadowViewRegistry[tag]; - if (shadowView == nil) { - RCTLogWarn(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", tag); - return; - } - + [self _executeBlockWithShadowView:^(RCTShadowView *shadowView) { shadowView.localData = localData; [self setNeedsLayout]; - }); + } forTag:view.reactTag]; } /** @@ -392,56 +399,40 @@ static NSDictionary *deviceOrientationEventBody(UIDeviceOrientation orientation) - (void)setSize:(CGSize)size forView:(UIView *)view { RCTAssertMainQueue(); - - NSNumber *reactTag = view.reactTag; - RCTExecuteOnUIManagerQueue(^{ - RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag]; - RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag); - + [self _executeBlockWithShadowView:^(RCTShadowView *shadowView) { if (CGSizeEqualToSize(size, shadowView.size)) { return; } shadowView.size = size; [self setNeedsLayout]; - }); + } forTag:view.reactTag]; } -- (void)setIntrinsicContentSize:(CGSize)size forView:(UIView *)view +- (void)setIntrinsicContentSize:(CGSize)intrinsicContentSize forView:(UIView *)view { RCTAssertMainQueue(); - - NSNumber *reactTag = view.reactTag; - RCTExecuteOnUIManagerQueue(^{ - RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag]; - if (shadowView == nil) { - RCTLogWarn(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", reactTag); + [self _executeBlockWithShadowView:^(RCTShadowView *shadowView) { + if (CGSizeEqualToSize(shadowView.intrinsicContentSize, intrinsicContentSize)) { return; - } - - if (!CGSizeEqualToSize(shadowView.intrinsicContentSize, size)) { - shadowView.intrinsicContentSize = size; - [self setNeedsLayout]; } - }); + + shadowView.intrinsicContentSize = intrinsicContentSize; + } forTag:view.reactTag]; } - (void)setBackgroundColor:(UIColor *)color forView:(UIView *)view { RCTAssertMainQueue(); - - NSNumber *reactTag = view.reactTag; - RCTExecuteOnUIManagerQueue(^{ + [self _executeBlockWithShadowView:^(RCTShadowView *shadowView) { if (!self->_viewRegistry) { return; } - RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag]; - RCTAssert(shadowView != nil, @"Could not locate root view with tag #%@", reactTag); shadowView.backgroundColor = color; [self _amendPendingUIBlocksWithStylePropagationUpdateForShadowView:shadowView]; [self flushUIBlocks]; - }); + } forTag:view.reactTag]; } /**