Skip to content

Commit

Permalink
Add a ComponentContext constructor that takes LithoConfig/TreeProps
Browse files Browse the repository at this point in the history
Summary:
In this diff we add a `ComponentContext` constructor that takes `LithoConfig/TreeProps`. This follows our vision according what is an "ideal" constructor.

In the process I replace other constructors usages with this new one.

Reviewed By: pentiumao

Differential Revision: D51158621

fbshipit-source-id: 5c8931e524a439855ce635b89a3479a47e112d90
  • Loading branch information
Fabio Carballo authored and facebook-github-bot committed Nov 11, 2023
1 parent f171409 commit bbd211b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
23 changes: 9 additions & 14 deletions litho-core/src/main/java/com/facebook/litho/ComponentContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,19 @@ public class ComponentContext {

private final ThreadLocal<CalculationContext> mCalculationStateContextThreadLocal;

/**
* This constructors takes a {@link Context}. This should only be used to create {@link
* ComponentTree}
*/
public ComponentContext(Context context) {
this(context, "", null);
this(context, null, null);
}

public ComponentContext(Context context, @Nullable LithoConfiguration lithoConfiguration) {
this(context, null, lithoConfiguration, null, null, null, null, null);
}
/**
* Constructor that can be used to receive log data from components. Check {@link
* ComponentsLogger} for the type of events you can listen for.
*
* @param context Android context.
* @param logTag a log tag to be used with the logger.
* @param logger a lifecycle logger to be used.
*/
public ComponentContext(
Context context, @Nullable String logTag, @Nullable ComponentsLogger logger) {
this(context, logTag, logger, null);
Context context,
@Nullable LithoConfiguration lithoConfiguration,
@Nullable TreeProps treeProps) {
this(context, treeProps, lithoConfiguration, null, null, null, null, null);
}

public ComponentContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class ComponentTreeBuilderTest {
looper = mock()
componentsLogger = mock()
context =
ComponentContext(ApplicationProvider.getApplicationContext(), LOG_TAG, componentsLogger)
ComponentContext(
ApplicationProvider.getApplicationContext(), LOG_TAG, componentsLogger, null)
root = TestLayoutComponent.create(context).build()
componentTreeBuilder = ComponentTree.create(context, root)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class LithoMetadataExceptionWrapperTest {
fun onCreateLayout_withLogTag_showsLogTagInStack() {
expectedException.expect(LithoMetadataExceptionWrapper::class.java)
expectedException.expectMessage("log_tag: myLogTag")
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null)
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null, null)
legacyLithoViewRule
.useComponentTree(ComponentTree.create(c).build())
.setRoot(TestCrasherOnCreateLayout.create(c))
Expand All @@ -173,7 +173,7 @@ class LithoMetadataExceptionWrapperTest {
fun onMount_withLogTag_showsLogTagInStack() {
expectedException.expect(LithoMetadataExceptionWrapper::class.java)
expectedException.expectMessage("log_tag: myLogTag")
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null)
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null, null)
legacyLithoViewRule
.useComponentTree(ComponentTree.create(c).build())
.setSizePx(100, 100)
Expand All @@ -188,7 +188,7 @@ class LithoMetadataExceptionWrapperTest {
expectedException.expect(LithoMetadataExceptionWrapper::class.java)
expectedException.expectMessage("log_tag: myLogTag")
expectedException.expectMessage("<cls>com.facebook.litho.widget.OnClickCallbackComponent</cls>")
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null)
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null, null)
val component: Component =
Column.create(c)
.child(
Expand All @@ -210,7 +210,7 @@ class LithoMetadataExceptionWrapperTest {
expectedException.expect(LithoMetadataExceptionWrapper::class.java)
expectedException.expectMessage("log_tag: myLogTag")
expectedException.expectMessage("<cls>com.facebook.litho.widget.TriggerCallbackComponent</cls>")
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null)
val c = ComponentContext(ApplicationProvider.getApplicationContext(), "myLogTag", null, null)
val handle = Handle()
val component: Component =
Column.create(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LogTreePopulatorTest {

@Before
fun setup() {
context = ComponentContext(getApplicationContext(), "test", null)
context = ComponentContext(getApplicationContext(), "test", null, null)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentContextUtils;
import com.facebook.litho.ComponentsLogger;
import com.facebook.litho.EventDispatchInfo;
import com.facebook.litho.EventHandler;
Expand Down Expand Up @@ -59,8 +60,10 @@ public SectionContext(
@Nullable String logTag,
@Nullable ComponentsLogger logger,
@Nullable TreeProps treeProps) {
super(context, logTag, logger);
super.setTreeProps(treeProps);
super(
context,
ComponentContextUtils.buildDefaultLithoConfiguration(context, null, logTag, logger, -1),
treeProps);
mKeyHandler = new KeyHandler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.facebook.samples.litho.kotlin.logging
import android.os.Bundle
import android.util.Log
import com.facebook.litho.ComponentContext
import com.facebook.litho.ComponentContextUtils
import com.facebook.litho.ComponentTree
import com.facebook.litho.ComponentTreeDebugEventListener
import com.facebook.litho.LithoView
Expand All @@ -33,7 +34,13 @@ class LoggingActivity : NavigatableDemoActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val c = ComponentContext(this, "LITHOSAMPLE", SampleComponentsLogger())
val c =
ComponentContext(
this,
ComponentContextUtils.buildDefaultLithoConfiguration(
this, null, "LITHOSAMPLE", SampleComponentsLogger(), -1),
null)

val lithoView =
LithoView.create(
c,
Expand Down

0 comments on commit bbd211b

Please sign in to comment.