Skip to content

Commit

Permalink
fix: Fix RNScreens/RNSScreen.h import if react-native-screens is no…
Browse files Browse the repository at this point in the history
…t installed (#2377)


## Description

When not having react-native-screens installed, react-native-reanimated does not build in alpha.3 (or current master).

There is an import: 

```objc
#import <RNScreens/RNSScreen.h>
```

which does not work since RNScreens does not exist. This PR fixes this issue by conditionally importing RNScreens.

## Changes

- Conditionally import RNScreens depending on whether it's installed or not
- Conditionally use RNSScreen class depending on whether it's available or not
  • Loading branch information
mrousavy authored Sep 28, 2021
1 parent 7f558c8 commit a4f4c83
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions ios/LayoutReanimation/REAUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#import "RCTLayoutAnimationGroup.h"
#import "REAIOSScheduler.h"
#include "Scheduler.h"

#if __has_include(<RNScreens/RNSScreen.h>)
#import <RNScreens/RNSScreen.h>
#endif

@interface RCTUIManager(REA)
- (void)_manageChildren:(NSNumber *)containerTag
Expand Down Expand Up @@ -40,7 +43,7 @@ - (void)setBridge:(RCTBridge *)bridge
{
if(!_blockSetter) {
_blockSetter = true;

self.bridge = bridge;
[super setValue:bridge forKey:@"_bridge"];
[self setValue:[bridge.uiManager valueForKey:@"_shadowViewRegistry"] forKey:@"_shadowViewRegistry"];
Expand All @@ -52,7 +55,7 @@ - (void)setBridge:(RCTBridge *)bridge
[self setValue:[bridge.uiManager valueForKey:@"_rootViewTags"] forKey:@"_rootViewTags"];
[self setValue:[bridge.uiManager valueForKey:@"_observerCoordinator"] forKey:@"_observerCoordinator"];
[self setValue:[bridge.uiManager valueForKey:@"_componentDataByName"] forKey:@"_componentDataByName"];

_blockSetter = false;
}
}
Expand All @@ -68,12 +71,12 @@ - (void)_manageChildren:(NSNumber *)containerTag
BOOL isUIViewRegistry = ((id)registry == (id)[self valueForKey:@"_viewRegistry"]);
id<RCTComponent> container;
NSArray<id<RCTComponent>> *permanentlyRemovedChildren;
if(isUIViewRegistry) {
container = registry[containerTag];
if(isUIViewRegistry) {
container = registry[containerTag];
for(id<RCTComponent> toRemoveChild in _toBeRemovedRegister[containerTag]) {
[container removeReactSubview:toRemoveChild];
}

permanentlyRemovedChildren = [super _childrenToRemoveFromContainer:container atIndices:removeAtIndices];
if(permanentlyRemovedChildren != nil) {
for(id<RCTComponent> permanentlyRemovedChild in permanentlyRemovedChildren) {
Expand All @@ -84,15 +87,15 @@ - (void)_manageChildren:(NSNumber *)containerTag
}
}
}

[super _manageChildren:containerTag
moveFromIndices:moveFromIndices
moveToIndices:moveToIndices
addChildReactTags:addChildReactTags
addAtIndices:addAtIndices
removeAtIndices:removeAtIndices
registry:registry];

if(isUIViewRegistry) {
NSMutableDictionary<NSNumber *, id<RCTComponent>> *viewRegistry = [self valueForKey:@"_viewRegistry"];
for(id<RCTComponent> toRemoveChild in _toBeRemovedRegister[containerTag]) {
Expand All @@ -103,7 +106,7 @@ - (void)_manageChildren:(NSNumber *)containerTag
[container insertReactSubview:toRemoveChild atIndex:lastIndex];
viewRegistry[toRemoveChild.reactTag] = toRemoveChild;
}

for (UIView *removedChild in permanentlyRemovedChildren) {
[self callAnimationForTree: removedChild parentTag:containerTag];
}
Expand All @@ -115,15 +118,15 @@ - (void) callAnimationForTree:(UIView*) view parentTag:(NSNumber*) parentTag
REASnapshot* snapshot = [[REASnapshot alloc] init:view];
_parentMapper[view.reactTag] = parentTag;
[_animationsManager onViewRemoval:view before:snapshot];

for(UIView* subView in view.reactSubviews) {
[self callAnimationForTree:subView parentTag:view.reactTag];
}
}

// Overrided https://github.com/facebook/react-native/blob/v0.65.0/React/Modules/RCTUIManager.m#L530
- (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *)rootShadowView
{
{
NSHashTable<RCTShadowView *> *affectedShadowViews = [NSHashTable weakObjectsHashTable];
[rootShadowView layoutWithAffectedShadowViews:affectedShadowViews];

Expand Down Expand Up @@ -232,7 +235,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
if (view.isHidden != isHidden) {
view.hidden = isHidden;
}

REASnapshot* snapshotBefore = [[REASnapshot alloc] init:view];
if (creatingLayoutAnimation) {
// Animate view creation
Expand Down Expand Up @@ -278,7 +281,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
[view reactSetFrame:frame];
completion(YES);
}

if(isNew) {
REASnapshot* snapshot = [[REASnapshot alloc] init:view];
[_animationsManager onViewCreate:view after:snapshot];
Expand All @@ -287,7 +290,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
REASnapshot* snapshotAfter = [[REASnapshot alloc] init:view];
[_animationsManager onViewUpdate:view before:snapshotBefore after:snapshotAfter];
}

}

[_animationsManager removeLeftovers];
Expand Down Expand Up @@ -319,7 +322,7 @@ - (void)unregisterView:(id<RCTComponent>) view
if(tag == nil) {
return;
}

[_toBeRemovedRegister[tag] removeObject:view];
if(_toBeRemovedRegister[tag].count == 0) {
[_toBeRemovedRegister removeObjectForKey:tag];
Expand All @@ -328,9 +331,11 @@ - (void)unregisterView:(id<RCTComponent>) view
[view.reactSuperview removeReactSubview:view];
id<RCTComponent> parentView = viewRegistry[tag];
[parentView removeReactSubview:view];
#if __has_include(<RNScreens/RNSScreen.h>)
if ([view isKindOfClass:[RNSScreenView class]]) {
[parentView didUpdateReactSubviews];
}
#endif
[viewRegistry removeObjectForKey:view.reactTag];
}

Expand Down

0 comments on commit a4f4c83

Please sign in to comment.