Java Code Examples for com.facebook.litho.Component#Builder

The following examples show how to use com.facebook.litho.Component#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpandableElementUtil.java    From litho with Apache License 2.0 6 votes vote down vote up
@Nullable
static Component.Builder maybeCreateBottomDetailComponent(
    ComponentContext c, boolean expanded, boolean seen) {
  if (!expanded) {
    return null;
  }

  return Text.create(c)
      .textSizeDip(14)
      .textColor(Color.GRAY)
      .alignSelf(YogaAlign.FLEX_END)
      .paddingDip(YogaEdge.RIGHT, 10)
      .transitionKey(TRANSITION_BOTTOM_DETAIL)
      .transitionKeyType(Transition.TransitionKeyType.GLOBAL)
      .text(seen ? "Seen" : "Sent");
}
 
Example 2
Source File: TextInputSpecTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnConnectionEventHandler() {
  InputConnection inputConnection = mock(InputConnection.class);
  EventHandler<InputConnectionEvent> inputConnectionEventHandler =
      EventHandlerTestHelper.createMockEventHandler(
          InputConnectionEvent.class,
          new EventHandlerTestHelper.MockEventHandler<InputConnectionEvent, Object>() {
            @Override
            public Object handleEvent(InputConnectionEvent event) {
              return inputConnection;
            }
          });
  Component.Builder component =
      TextInput.create(mContext).inputConnectionEventHandler(inputConnectionEventHandler);
  final android.widget.EditText editText = getEditText(component);
  InputConnection editTextInputConnection =
      editText.onCreateInputConnection(mock(EditorInfo.class));
  assertThat(editTextInputConnection).isEqualTo(inputConnection);
}
 
Example 3
Source File: FastScrollHandleComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
private static Component.Builder buildDragHandle(ComponentContext c) {
  final int radiusDip = HANDLE_SIZE_DP / 2;
  return Text.create(c)
      .widthDip(HANDLE_SIZE_DP)
      .heightDip(HANDLE_SIZE_DP)
      .background(buildCircleDrawable(c, 0xFFDDDDDD, radiusDip))
      .border(
          Border.create(c)
              .color(YogaEdge.ALL, Color.BLACK)
              .widthDip(YogaEdge.ALL, 1)
              .radiusDip(radiusDip)
              .build())
      .verticalGravity(VerticalGravity.CENTER)
      .textAlignment(Layout.Alignment.ALIGN_CENTER)
      .textSizeDip(16)
      .typeface(Typeface.DEFAULT_BOLD)
      .text("DRAG");
}
 
Example 4
Source File: TestSizeDependentComponent.java    From litho with Apache License 2.0 6 votes vote down vote up
@Override
protected Component onCreateLayoutWithSizeSpec(
    ComponentContext c, int widthSpec, int heightSpec) {

  final Component.Builder builder1 =
      TestDrawableComponent.create(c, false, true, true, false)
          .flexShrink(0)
          .backgroundColor(0xFFFF0000);
  final Component.Builder builder2 =
      TestViewComponent.create(c, false, true, true, false)
          .flexShrink(0)
          .marginPx(YogaEdge.ALL, 3);

  if (hasFixedSizes) {
    builder1.widthPx(50).heightPx(50);
    builder2.heightPx(20);
  }

  if (isDelegate) {
    return builder1.build();
  }

  return Column.create(c).paddingPx(YogaEdge.ALL, 5).child(builder1).child(builder2).build();
}
 
Example 5
Source File: SizeTreePropComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(ComponentContext c, @TreeProp Size size) {
  Component.Builder<?> component;
  if (SizeSpec.getSize(size.width) < DEFAULT_SIZE
      && SizeSpec.getSize(size.height) < DEFAULT_SIZE) {
    throw new IllegalStateException("Available width and height needs to be > " + DEFAULT_SIZE);
  } else {
    component = Row.create(c).widthPx(DEFAULT_SIZE);
  }
  return component.build();
}
 
Example 6
Source File: ExpandableElementMeSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
static Component.Builder createMessageContent(ComponentContext c, String messageText) {
  return Row.create(c)
      .paddingDip(YogaEdge.ALL, 8)
      .marginDip(YogaEdge.ALL, 8)
      .background(ExpandableElementUtil.getMessageBackground(c, 0xFF0084FF))
      .child(Text.create(c).textSizeDip(18).textColor(Color.WHITE).text(messageText));
}
 
Example 7
Source File: ExpandableElementOtherSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
static Component.Builder createMessageContent(ComponentContext c, String messageText) {
  return Row.create(c)
      .paddingDip(YogaEdge.ALL, 8)
      .marginDip(YogaEdge.ALL, 8)
      .background(ExpandableElementUtil.getMessageBackground(c, 0xFFEAEAEA))
      .child(Text.create(c).textSizeDip(18).textColor(Color.BLACK).text(messageText));
}
 
Example 8
Source File: TextInputSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testCursorDrawableResSet() throws IllegalAccessException, NoSuchFieldException {
  int drawableRes = 10;
  Component.Builder component = TextInput.create(mContext).cursorDrawableRes(drawableRes);
  final android.widget.EditText editText = getEditText(component);
  Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
  f.setAccessible(true);
  f.get(editText);

  assertThat(f.get(editText)).isEqualTo(drawableRes);
}
 
Example 9
Source File: CardSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
private static Component.Builder makeTransparencyEnabledCardClip(
    ComponentContext c, int clippingColor, float cornerRadius) {
  return TransparencyEnabledCardClip.create(c)
      .cardBackgroundColor(clippingColor)
      .cornerRadiusPx(cornerRadius)
      .positionType(ABSOLUTE)
      .positionPx(ALL, 0);
}
 
Example 10
Source File: TextInputSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testTextInputWithText() {
  String text = "Dummy text";
  int textSize = 10;
  Component.Builder component = TextInput.create(mContext).textSizePx(textSize).initialText(text);
  final android.widget.EditText editText = getEditText(component);

  assertThat(editText.getText().toString()).isEqualTo(text);
  assertThat(editText.getTextSize()).isEqualTo(textSize);
}
 
Example 11
Source File: TextInputSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorState() {
  String errorMessage = "Error message";
  Component.Builder component = TextInput.create(mContext).error(errorMessage);
  final android.widget.EditText editText = getEditText(component);
  assertThat(editText.getError()).isEqualTo(errorMessage);
  component = TextInput.create(mContext).error(null);
  assertThat(getEditText(component).getError()).isNullOrEmpty();
}
 
Example 12
Source File: ComponentTestHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Mount a component into a component view.
 *
 * @param component The component builder to mount
 * @param incrementalMountEnabled States whether incremental mount is enabled
 * @return A LithoView with the component mounted in it.
 */
public static LithoView mountComponent(
    Component.Builder component,
    boolean incrementalMountEnabled,
    boolean visibilityProcessingEnabled) {
  ComponentContext context = getContext(component);
  return mountComponent(
      context,
      new LithoView(context),
      component.build(),
      incrementalMountEnabled,
      visibilityProcessingEnabled,
      100,
      100);
}
 
Example 13
Source File: ComponentAssert.java    From litho with Apache License 2.0 4 votes vote down vote up
public static ComponentAssert assertThat(Component.Builder<?> builder) {
  // mContext is freed up during build() so we need to get a reference to it before.
  final ComponentContext context = Whitebox.getInternalState(builder, "mContext");
  return new ComponentAssert(context, builder.build());
}
 
Example 14
Source File: LithoViewRule.java    From litho with Apache License 2.0 4 votes vote down vote up
public LithoViewRule setRoot(Component.Builder builder) {
  getComponentTree().setRoot(builder.build());
  return this;
}
 
Example 15
Source File: FullGroupSection.java    From litho with Apache License 2.0 4 votes vote down vote up
public Builder<T> prop3(Component.Builder<?> prop3Builder) {
  this.mFullGroupSection.prop3 = prop3Builder == null ? null : prop3Builder.build();
  mRequired.set(1);
  return this;
}
 
Example 16
Source File: FeedImageComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
private static Component.Builder createImageComponent(ComponentContext c, String image) {
  final DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(image).build();

  return FrescoImage.create(c).controller(controller).imageAspectRatio(2f);
}
 
Example 17
Source File: CommonDynamicPropAnimationsComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(
    ComponentContext c,
    @Prop DynamicValue<Float> dynamicAlpha,
    @Prop DynamicValue<Float> dynamicScale,
    @Prop DynamicValue<Float> dynamicTranslation,
    @Prop DynamicValue<Integer> dynamicBgColor,
    @Prop DynamicValue<Float> dynamicRotation,
    @Prop DynamicValue<Float> dynamicElevation,
    @FromEvent CommonDynamicPropsExample model) {
  Component.Builder builder = Column.create(c).widthDip(100).heightDip(100);

  boolean shouldApplyBgColor = true;

  switch (model) {
    case ALPHA:
      builder.alpha(dynamicAlpha);
      break;
    case SCALE:
      builder.scaleX(dynamicScale).scaleY(dynamicScale);
      break;
    case TRANSLATION:
      builder.translationX(dynamicTranslation).translationY(dynamicTranslation);
      break;
    case BACKGROUND_COLOR:
      builder.backgroundColor(dynamicBgColor);
      shouldApplyBgColor = false;
      break;
    case ROTATION:
      builder.rotation(dynamicRotation);
      break;
    case ELEVATION:
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // By default the shadow is cast by the View's background
        // We need to override this behaviour as the LithoView's background is unset and drawn
        // by a child component.
        builder.outlineProvider(ViewOutlineProvider.PADDED_BOUNDS);
      }
      builder.shadowElevation(dynamicElevation);
      break;
  }

  if (shouldApplyBgColor) {
    builder.backgroundColor(Color.BLUE);
  }

  return ComponentRenderInfo.create()
      .component(
          Column.create(c)
              .alignItems(YogaAlign.CENTER)
              .paddingDip(YogaEdge.ALL, 20)
              .child(builder))
      .build();
}
 
Example 18
Source File: FeedItemComponentSpec.java    From litho-picasso with MIT License 4 votes vote down vote up
private static Component.Builder getRecyclerComponent(ComponentContext c,
    RecyclerBinder binder) {
  return Recycler.create(c).binder(binder).flexShrink(0).aspectRatio(2);
}
 
Example 19
Source File: TestLayout.java    From litho with Apache License 2.0 4 votes vote down vote up
public Builder<S> child(Component.Builder<?> childBuilder) {
  this.mTestLayout.child = childBuilder == null ? null : childBuilder.build();
  mRequired.set(1);
  return this;
}
 
Example 20
Source File: ComponentTestHelper.java    From litho with Apache License 2.0 2 votes vote down vote up
/**
 * Get the subcomponents of a component
 *
 * @param component The component builder which to get the subcomponents of
 * @return The subcomponents of the given component
 * @deprecated Use ComponentAssert#extractingSubComponents() instead.
 */
@Deprecated
public static List<SubComponent> getSubComponents(Component.Builder component) {
  return getSubComponents(getContext(component), component.build());
}