Skip to content

Commit

Permalink
Convert ComponentsConfiguration to Kotlin
Browse files Browse the repository at this point in the history
Summary:
The goal of this diff is to make our `ComponentsConfiguration` into a kotlin data class.

The main changes are:
- `ComponentsConfiguration` is now a data class.
- `ComponentsConfiguration` has an `internal` constructor. This forces clients to only be able to generate one either by getting the `defaultInstance` or by overriding some values calling `copy` on the `defaultInstance`.
- There is still a `ComponentsConfiguration.create()` method for the Java clients to still have an easy access to overriding. However, as more code is converted to Kotlin we can actually remove it. (there are not really many usages of it).

Some examples of usage in Kotlin:

```
/* overriding the default */

ComponentsConfiguration.defaultInstance =
    ComponentsConfiguration.defaultInstance.copy(myParam = true)
```

Reviewed By: adityasharat

Differential Revision: D51109234

fbshipit-source-id: f566bae4b3902a6570f0e9d6819843530f2fdb17
  • Loading branch information
Fabio Carballo authored and facebook-github-bot committed Nov 13, 2023
1 parent a65a680 commit bab82b0
Show file tree
Hide file tree
Showing 36 changed files with 516 additions and 676 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public void notifyVisibleBoundsChanged() {
@Override
public void onRegisterForPremount(@Nullable Long frameTime) {
final @Nullable ComponentsConfiguration config = getConfiguration();
if (config != null && config.useIncrementalMountGapWorker()) {
if (config != null && config.useIncrementalMountGapWorker) {
final boolean isTracing = ComponentsSystrace.isTracing();
if (isTracing) {
ComponentsSystrace.beginSection("BaseMountingView::onRegisterForPremount");
Expand All @@ -660,7 +660,7 @@ public void onRegisterForPremount(@Nullable Long frameTime) {
@Override
public void onUnregisterForPremount() {
final @Nullable ComponentsConfiguration config = getConfiguration();
if (config != null && config.useIncrementalMountGapWorker()) {
if (config != null && config.useIncrementalMountGapWorker) {
final boolean isTracing = ComponentsSystrace.isTracing();
if (isTracing) {
ComponentsSystrace.beginSection("BaseMountingView::onUnregisterForPremount");
Expand Down Expand Up @@ -816,7 +816,7 @@ protected void setupMountExtensions() {
if (hasTree()) {
if (isIncrementalMountEnabled()) {
final @Nullable ComponentsConfiguration config = getConfiguration();
boolean useGapWorker = config != null && config.useIncrementalMountGapWorker();
boolean useGapWorker = config != null && config.useIncrementalMountGapWorker;
mLithoHostListenerCoordinator.enableIncrementalMount(useGapWorker);
} else {
mLithoHostListenerCoordinator.disableIncrementalMount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,12 @@ public ScopedComponentInfo getScopedComponentInfo() {
}

boolean shouldCacheLayouts() {
return isReconciliationEnabled() && mLithoConfiguration.componentsConfig.shouldCacheLayouts();
return isReconciliationEnabled()
&& mLithoConfiguration.componentsConfig.getShouldCacheLayouts();
}

public final boolean shouldUseNonRebindingEventHandlers() {
return mLithoConfiguration.componentsConfig.useNonRebindingEventHandlers();
return mLithoConfiguration.componentsConfig.getUseNonRebindingEventHandlers();
}

boolean isNestedTreeContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ public static LithoConfiguration buildDefaultLithoConfiguration(
@Nullable ComponentsLogger logger,
int treeID) {
ComponentsLogger loggerToUse =
logger != null ? logger : ComponentsConfiguration.sComponentsLogger;
logger != null ? logger : ComponentsConfiguration.componentsLogger;

String logTagToUse = logTag;
if (logTag == null && logger == null && loggerToUse != null) {
logTagToUse = "global-components-logger";
}

return new LithoConfiguration(
ComponentsConfiguration.getDefaultComponentsConfiguration(),
ComponentsConfiguration.defaultInstance,
AnimationsDebug.areTransitionsEnabled(context),
ComponentsConfiguration.overrideReconciliation != null
? ComponentsConfiguration.overrideReconciliation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ private void doResolve(
final boolean isInterruptible =
!LayoutState.isFromSyncLayout(source)
&& (mContext.mLithoConfiguration.componentsConfig.getUseInterruptibleResolution()
|| mContext.mLithoConfiguration.componentsConfig.getUseCancelableLayoutFutures());
|| mContext.mLithoConfiguration.componentsConfig.getUseCancellableLayoutFutures());

ResolveTreeFuture treeFuture =
new ResolveTreeFuture(
Expand Down Expand Up @@ -2230,7 +2230,7 @@ private void doResolve(
if (resolveResult == null) {
if (!isReleased()
&& isFromSyncLayout(source)
&& !mContext.mLithoConfiguration.componentsConfig.getUseCancelableLayoutFutures()) {
&& !mContext.mLithoConfiguration.componentsConfig.getUseCancellableLayoutFutures()) {
final String errorMessage =
"ResolveResult is null, but only async operations can return a null ResolveResult. Source: "
+ layoutSourceToString(source)
Expand Down Expand Up @@ -2427,7 +2427,7 @@ private void doLayout(
if (layoutState == null) {
if (!isReleased()
&& isSync
&& !mContext.mLithoConfiguration.componentsConfig.getUseCancelableLayoutFutures()) {
&& !mContext.mLithoConfiguration.componentsConfig.getUseCancellableLayoutFutures()) {
final String errorMessage =
"LayoutState is null, but only async operations can return a null LayoutState. Source: "
+ layoutSourceToString(source)
Expand Down Expand Up @@ -2834,7 +2834,7 @@ public void onErrorComponent(Component component) {
* after that because it's in an incomplete state, so it needs to be released.
*/
public void cancelLayoutAndReleaseTree() {
if (!mContext.mLithoConfiguration.componentsConfig.getUseCancelableLayoutFutures()) {
if (!mContext.mLithoConfiguration.componentsConfig.getUseCancellableLayoutFutures()) {
ComponentsReporter.emitMessage(
ComponentsReporter.LogLevel.ERROR,
TAG,
Expand Down
4 changes: 2 additions & 2 deletions litho-core/src/main/java/com/facebook/litho/LithoNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ open class LithoNode : Node<LithoRenderContext>, Cloneable {

private fun hasSelectedStateWhenDisablingDrawableOutputs(node: LithoNode): Boolean =
ComponentContext.getComponentsConfig(node.headComponentContext)
.isShouldAddHostViewForRootComponent &&
.shouldAddHostViewForRootComponent &&
!node.willMountView &&
node.nodeInfo != null &&
node.nodeInfo?.selectedState != NodeInfo.SELECTED_UNSET
Expand Down Expand Up @@ -1434,7 +1434,7 @@ open class LithoNode : Node<LithoRenderContext>, Cloneable {
// tree is mounted. Click handling is also considered accessibility content but
// this is already covered separately i.e. click handler is not null.
val hasBackgroundOrForeground =
ComponentContext.getComponentsConfig(c).shouldAddRootHostViewOrDisableBgFgOutputs() &&
ComponentContext.getComponentsConfig(c).shouldAddRootHostViewOrDisableBgFgOutputs &&
(node.background != null || node.foreground != null)
val hasAccessibilityContent =
(context?.isAccessibilityEnabled == true) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ object LithoNodeUtils {
flags = flags or LithoRenderUnit.LAYOUT_FLAG_MATCH_HOST_BOUNDS
}
if (ComponentContext.getComponentsConfig(node.headComponentContext)
.shouldAddRootHostViewOrDisableBgFgOutputs()) {
.shouldAddRootHostViewOrDisableBgFgOutputs) {
flags = flags or LithoRenderUnit.LAYOUT_FLAG_DRAWABLE_OUTPUTS_DISABLED
}
if (nodeInfo?.hasTouchEventHandlers() == true) {
Expand Down
8 changes: 4 additions & 4 deletions litho-core/src/main/java/com/facebook/litho/LithoReducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ object LithoReducer {

var parent: RenderTreeNode? = null
var hierarchy: DebugHierarchy.Node? = null
if (c.mLithoConfiguration.componentsConfig.isShouldAddHostViewForRootComponent) {
if (c.mLithoConfiguration.componentsConfig.shouldAddHostViewForRootComponent) {
hierarchy = if (root is LithoLayoutResult) root.node.getDebugHierarchy() else null
addRootHostRenderTreeNode(layoutState, root, hierarchy)
parent = layoutState.mMountableOutputs[0]
Expand Down Expand Up @@ -518,7 +518,7 @@ object LithoReducer {
}

// 2. Add background if defined.
if (!context.mLithoConfiguration.componentsConfig.shouldAddRootHostViewOrDisableBgFgOutputs()) {
if (!context.mLithoConfiguration.componentsConfig.shouldAddRootHostViewOrDisableBgFgOutputs) {
result.backgroundRenderUnit?.let { backgroundRenderUnit ->
val backgroundRenderTreeNode =
addDrawableRenderTreeNode(
Expand Down Expand Up @@ -607,7 +607,7 @@ object LithoReducer {
}

// 6. Add foreground if defined.
if (!context.mLithoConfiguration.componentsConfig.shouldAddRootHostViewOrDisableBgFgOutputs()) {
if (!context.mLithoConfiguration.componentsConfig.shouldAddRootHostViewOrDisableBgFgOutputs) {
result.foregroundRenderUnit?.let { foregroundRenderUnit ->
val foregroundRenderTreeNode: RenderTreeNode =
addDrawableRenderTreeNode(
Expand Down Expand Up @@ -923,7 +923,7 @@ object LithoReducer {
type,
unit.importantForAccessibility,
layoutState.resolveResult.context.lithoConfiguration.componentsConfig
.shouldAddRootHostViewOrDisableBgFgOutputs())
.shouldAddRootHostViewOrDisableBgFgOutputs)

if (attrs != null) {
layoutState.mRenderUnitsWithViewAttributes[id] = attrs
Expand Down
1 change: 0 additions & 1 deletion litho-core/src/main/java/com/facebook/litho/config/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ litho_android_library(
visibility = ["PUBLIC"],
deps = [
"//fbandroid/libraries/rendercore/rendercore-incremental-mount:rendercore-incremental-mount",
"//fbandroid/third-party/java/infer-annotations:infer-annotations",
],
)
Loading

0 comments on commit bab82b0

Please sign in to comment.