Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnLayoutCalculated component lifecycle method #786

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.litho.annotations;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface OnLayoutCalculated {}
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ protected void onBind(ComponentContext c, Object mountedContent) {
*/
protected void onBoundsDefined(ComponentContext c, ComponentLayout layout) {}

/**
* Called after the layout calculation is finished.
*
* @param c The {@link Context} used by this component.
*/
protected void onLayoutCalculated(ComponentContext c) {}

/**
* Generate a tree of {@link ComponentLayout} representing the layout structure of the {@link
* Component} and its sub-components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ && isLayoutDirectionRTL(c.getAndroidContext())) {

mYogaNode.calculateLayout(width, height);

for (Component component : mComponents) {
component.onLayoutCalculated(mComponentContext);
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum LifecycleStep {
ON_PREPARE,
ON_MEASURE,
ON_BOUNDS_DEFINED,
ON_LAYOUT_CALCULATED,
ON_CREATE_MOUNT_CONTENT,
ON_MOUNT,
ON_UNMOUNT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.facebook.litho.annotations.OnCreateTreeProp;
import com.facebook.litho.annotations.OnDetached;
import com.facebook.litho.annotations.OnEvent;
import com.facebook.litho.annotations.OnLayoutCalculated;
import com.facebook.litho.annotations.OnUpdateState;
import com.facebook.litho.annotations.OnUpdateStateWithTransition;
import com.facebook.litho.annotations.Param;
Expand Down Expand Up @@ -113,6 +114,11 @@ static void onDetached(ComponentContext c, @Prop List<LifecycleStep.StepInfo> st
steps.add(new StepInfo(LifecycleStep.ON_DETACHED));
}

@OnLayoutCalculated
static void onLayoutCalculated(ComponentContext c, @Prop List<LifecycleStep.StepInfo> steps) {
steps.add(new StepInfo(LifecycleStep.ON_LAYOUT_CALCULATED));
}

@OnUpdateState
static void updateState(@Param List<LifecycleStep.StepInfo> stepsAsParam) {
stepsAsParam.add(new StepInfo(LifecycleStep.ON_UPDATE_STATE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void lifecycle_onSetRootWithLayout_shouldCallLifecycleMethods() {
LifecycleStep.ON_CALCULATE_CACHED_VALUE,
LifecycleStep.ON_CREATE_LAYOUT,
LifecycleStep.ON_CREATE_TRANSITION,
LifecycleStep.ON_LAYOUT_CALCULATED,
LifecycleStep.ON_ATTACHED);
ComponentsConfiguration.isAnimationDisabled = true;
}
Expand Down Expand Up @@ -108,7 +109,8 @@ public void lifecycle_subsequentSetRoot_shouldCallLifecycleMethod() {
.containsExactly(
LifecycleStep.ON_CREATE_TREE_PROP,
LifecycleStep.ON_CALCULATE_CACHED_VALUE,
LifecycleStep.ON_CREATE_LAYOUT);
LifecycleStep.ON_CREATE_LAYOUT,
LifecycleStep.ON_LAYOUT_CALCULATED);
}

@Test
Expand All @@ -132,7 +134,8 @@ public void lifecycle_updateState_shouldCallLifecycleMethod() {
LifecycleStep.ON_UPDATE_STATE,
LifecycleStep.ON_CREATE_TREE_PROP,
LifecycleStep.ON_CALCULATE_CACHED_VALUE,
LifecycleStep.ON_CREATE_LAYOUT);
LifecycleStep.ON_CREATE_LAYOUT,
LifecycleStep.ON_LAYOUT_CALCULATED);
ComponentsConfiguration.isReconciliationEnabled = true;
}

Expand All @@ -158,7 +161,8 @@ public void lifecycle_updateStateWithTransition_shouldCallLifecycleMethod() {
LifecycleStep.ON_UPDATE_STATE_WITH_TRANSITION,
LifecycleStep.ON_CREATE_TREE_PROP,
LifecycleStep.ON_CALCULATE_CACHED_VALUE,
LifecycleStep.ON_CREATE_LAYOUT);
LifecycleStep.ON_CREATE_LAYOUT,
LifecycleStep.ON_LAYOUT_CALCULATED);
ComponentsConfiguration.isReconciliationEnabled = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.facebook.litho.annotations.OnCreateMountContentPool;
import com.facebook.litho.annotations.OnCreateTransition;
import com.facebook.litho.annotations.OnDetached;
import com.facebook.litho.annotations.OnLayoutCalculated;
import com.facebook.litho.annotations.OnError;
import com.facebook.litho.annotations.OnLoadStyle;
import com.facebook.litho.annotations.OnMeasure;
Expand Down Expand Up @@ -162,6 +163,18 @@ public final class DelegateMethodDescriptions {
.build()))
.build();

public static final DelegateMethodDescription ON_LAYOUT_CALCULATED =
DelegateMethodDescription.newBuilder()
.annotations(ImmutableList.of(AnnotationSpec.builder(Override.class).build()))
.accessType(Modifier.PROTECTED)
.returnType(TypeName.VOID)
.name("onLayoutCalculated")
.definedParameterTypes(
ImmutableList.<TypeName>of(ClassNames.COMPONENT_CONTEXT))
.optionalParameterTypes(
ImmutableList.of(PROP, TREE_PROP, STATE, INJECT_PROP, CACHED_VALUE))
.build();

public static final DelegateMethodDescription ON_CREATE_INITIAL_STATE =
DelegateMethodDescription.newBuilder()
.annotations(ImmutableList.of(AnnotationSpec.builder(Override.class).build()))
Expand Down Expand Up @@ -521,6 +534,7 @@ public int compare(Class<? extends Annotation> lhs, Class<? extends Annotation>
layoutSpecDelegateMethodsMap.put(ShouldUpdate.class, SHOULD_UPDATE);
layoutSpecDelegateMethodsMap.put(OnAttached.class, ON_ATTACHED);
layoutSpecDelegateMethodsMap.put(OnDetached.class, ON_DETACHED);
layoutSpecDelegateMethodsMap.put(OnLayoutCalculated.class, ON_LAYOUT_CALCULATED);
LAYOUT_SPEC_DELEGATE_METHODS_MAP = Collections.unmodifiableMap(layoutSpecDelegateMethodsMap);

Map<Class<? extends Annotation>, DelegateMethodDescription> mountSpecDelegateMethodsMap =
Expand Down