Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jwajgelt committed Dec 5, 2022
1 parent ff2ad1f commit f733def
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
29 changes: 21 additions & 8 deletions ios/LayoutReanimation/REAAnimationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,22 @@ - (void)maybeDropAncestors:(UIView *)child
}
}

- (BOOL)startAnimationsRecursive:(UIView *)view removingSubviewsWithoutAnimations:(BOOL)removeSubviewsWithoutAnimations;
- (BOOL)startAnimationsRecursive:(UIView *)view
shouldRemoveSubviewsWithoutAnimations:(BOOL)shouldRemoveSubviewsWithoutAnimations;
{
if (!view.reactTag) {
return NO;
}
BOOL hasExitAnimation = _hasAnimationForTag(view.reactTag, @"exiting") || [_exitingViews objectForKey:view.reactTag];
BOOL hasAnimatedChildren = NO;
removeSubviewsWithoutAnimations = removeSubviewsWithoutAnimations && !hasExitAnimation;
shouldRemoveSubviewsWithoutAnimations = shouldRemoveSubviewsWithoutAnimations && !hasExitAnimation;
NSMutableArray *toBeRemoved = [[NSMutableArray alloc] init];

for (UIView *subview in [view.reactSubviews copy]) {
if ([self startAnimationsRecursive:subview removingSubviewsWithoutAnimations:removeSubviewsWithoutAnimations]) {
if ([self startAnimationsRecursive:subview
shouldRemoveSubviewsWithoutAnimations:shouldRemoveSubviewsWithoutAnimations]) {
hasAnimatedChildren = YES;
} else if (removeSubviewsWithoutAnimations) {
} else if (shouldRemoveSubviewsWithoutAnimations) {
[toBeRemoved addObject:subview];
}
}
Expand Down Expand Up @@ -343,15 +345,26 @@ - (void)reattachAnimatedChildren:(NSArray<id<RCTComponent>> *)children
if (![container isKindOfClass:[UIView class]]) {
return;
}

// since we reattach only some of the views,
// we count the views we DIDN'T reattach
// and shift later views' indices by that number
// to make sure they appear at correct relative posisitons
// in the `subviews` array
int skippedViewsCount = 0;

for (int i = 0; i < children.count; i++) {
id<RCTComponent> child = [children objectAtIndex:i];
id<RCTComponent> child = children[i];
if (![child isKindOfClass:[UIView class]]) {
skippedViewsCount++;
continue;
}
UIView *childView = (UIView *)child;
NSNumber *originalIndex = [indices objectAtIndex:i];
if ([self startAnimationsRecursive:childView removingSubviewsWithoutAnimations:true]) {
[(UIView *)container insertSubview:childView atIndex:[originalIndex intValue]];
NSNumber *originalIndex = indices[i];
if ([self startAnimationsRecursive:childView shouldRemoveSubviewsWithoutAnimations:YES]) {
[(UIView *)container insertSubview:childView atIndex:[originalIndex intValue] - skippedViewsCount];
} else {
skippedViewsCount++;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions ios/LayoutReanimation/REAUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ - (void)_manageChildren:(NSNumber *)containerTag
registry:registry];

if (isLayoutAnimationEnabled) {
// we sort the (index, view) pairs to make sure we insert views back in order
NSMutableArray<NSArray<id> *> *removedViewsWithIndices = [NSMutableArray new];
for (int i = 0; i < removeAtIndices.count; i++) {
removedViewsWithIndices[i] = @[ removeAtIndices[i], permanentlyRemovedChildren[i] ];
}
[removedViewsWithIndices
sortUsingComparator:^NSComparisonResult(NSArray<id> *_Nonnull obj1, NSArray<id> *_Nonnull obj2) {
return [(NSNumber *)obj1[0] compare:(NSNumber *)obj2[0]];
}];

[_animationsManager reattachAnimatedChildren:permanentlyRemovedChildren
toContainer:container
atIndices:removeAtIndices];
Expand Down
3 changes: 0 additions & 3 deletions src/reanimated2/layoutReanimation/animationsManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { runOnUI } from '../core';
import { withStyleAnimation } from '../animation/styleAnimation';
import { LogBox } from 'react-native';
import { SharedValue } from '../commonTypes';
import { makeUIMutable } from '../mutables';
import {
LayoutAnimationFunction,
LayoutAnimationsValues,
} from './animationBuilder';

LogBox.ignoreLogs(['Overriding previous layout animation with']);

const TAG_OFFSET = 1e9;

function startObservingProgress(
Expand Down

0 comments on commit f733def

Please sign in to comment.