Skip to content

Commit

Permalink
Adding animationsOnInitialMount
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Jain committed Jun 25, 2015
1 parent eb9c941 commit db86526
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ComponentKit/Core/CKComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ - (void)childrenDidMount

#pragma mark - Animation

- (std::vector<CKComponentAnimation>)animationsOnInitialMount
{
return {};
}

- (std::vector<CKComponentAnimation>)animationsFromPreviousComponent:(CKComponent *)previousComponent
{
return {};
Expand Down
18 changes: 18 additions & 0 deletions ComponentKit/Core/CKComponentController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ @implementation CKComponentController
{
CKComponentControllerState _state;
BOOL _updatingComponent;
BOOL _performedInitialMount;
CKComponent *_previousComponent;
std::vector<CKPendingComponentAnimation> _pendingAnimationsOnInitialMount;
std::vector<CKAppliedComponentAnimation> _appliedAnimationsOnInitialMount;
std::vector<CKPendingComponentAnimation> _pendingAnimations;
std::vector<CKAppliedComponentAnimation> _appliedAnimations;
}
Expand Down Expand Up @@ -87,6 +90,12 @@ - (void)componentWillMount:(CKComponent *)component
case CKComponentControllerStateUnmounted:
_state = CKComponentControllerStateMounting;
[self willMount];
if (!_performedInitialMount) {
_performedInitialMount = YES;
for (const auto &animation : [component animationsOnInitialMount]) {
_pendingAnimationsOnInitialMount.push_back({animation, animation.willRemount()});
}
}
break;
case CKComponentControllerStateMounted:
_state = CKComponentControllerStateRemounting;
Expand All @@ -108,6 +117,11 @@ - (void)componentDidMount:(CKComponent *)component
case CKComponentControllerStateMounting:
_state = CKComponentControllerStateMounted;
[self didMount];
for (const auto &pendingAnimation : _pendingAnimationsOnInitialMount) {
const CKComponentAnimation &anim = pendingAnimation.animation;
_appliedAnimationsOnInitialMount.push_back({anim, anim.didRemount(pendingAnimation.context)});
}
_pendingAnimationsOnInitialMount.clear();
break;
case CKComponentControllerStateRemounting:
_state = CKComponentControllerStateMounted;
Expand Down Expand Up @@ -176,6 +190,10 @@ - (void)_relinquishView

- (void)_cleanupAppliedAnimations
{
for (const auto &appliedAnimation : _appliedAnimationsOnInitialMount) {
appliedAnimation.animation.cleanup(appliedAnimation.context);
}
_appliedAnimationsOnInitialMount.clear();
for (const auto &appliedAnimation : _appliedAnimations) {
appliedAnimation.animation.cleanup(appliedAnimation.context);
}
Expand Down
8 changes: 8 additions & 0 deletions ComponentKit/Core/CKComponentSubclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ extern CGSize const kCKComponentParentSizeUndefined;
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;

/**
Override to return a list of animations that will be applied to the component when it is first mounted.
@warning If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to
identify equivalent components between trees.
*/
- (std::vector<CKComponentAnimation>)animationsOnInitialMount;

/**
Override to return a list of animations from the previous version of the same component.
Expand Down
6 changes: 4 additions & 2 deletions ComponentKit/Core/Scope/CKComponentScopeHandle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ static Class controllerClassForComponentClass(Class componentClass)
if (it == cache->end()) {
Class c = NSClassFromString([NSStringFromClass(componentClass) stringByAppendingString:@"Controller"]);

// If you override animationsFromPreviousComponent: then we need a controller.
if (c == nil && CKSubclassOverridesSelector([CKComponent class], componentClass, @selector(animationsFromPreviousComponent:))) {
// If you override animationsFromPreviousComponent: or animationsOnInitialMount then we need a controller.
if (c == nil &&
(CKSubclassOverridesSelector([CKComponent class], componentClass, @selector(animationsFromPreviousComponent:)) ||
CKSubclassOverridesSelector([CKComponent class], componentClass, @selector(animationsOnInitialMount)))) {
c = [CKComponentController class];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @interface CKMonkeyComponentWithAnimations : CKComponent

@implementation CKMonkeyComponentWithAnimations
- (std::vector<CKComponentAnimation>)animationsFromPreviousComponent:(CKComponent *)previousComponent { return {}; }
- (std::vector<CKComponentAnimation>)animationsOnInitialMount { return {}; }
@end

#pragma mark - Tests
Expand Down

0 comments on commit db86526

Please sign in to comment.