com.facebook.litho.annotations.Prop Java Examples

The following examples show how to use com.facebook.litho.annotations.Prop. 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: VerySimpleGroupSectionSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateChildren
protected static Children onCreateChildren(
    SectionContext c, @State(canUpdateLazily = true) int extra, @Prop int numberOfDummy) {
  Children.Builder builder = Children.create();

  if (extra > 0) {
    builder.child(
        SingleComponentSection.create(c)
            .component(Image.create(c).drawable(new ColorDrawable()).build()));
  }

  for (int i = 0; i < numberOfDummy + extra; i++) {
    builder.child(
        SingleComponentSection.create(c)
            .component(Text.create(c).text("Lol hi " + i).build())
            .key("key" + i)
            .build());
  }
  return builder.build();
}
 
Example #2
Source File: ExpandableElementMeSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@Nullable
@OnCreateTransition
static Transition onCreateTransition(
    ComponentContext c,
    @State Boolean expanded,
    @Prop(optional = true) boolean forceAnimateOnAppear) {
  if (!forceAnimateOnAppear && expanded == null) {
    return null;
  }

  return Transition.parallel(
      Transition.allLayout(),
      Transition.create(Transition.TransitionKeyType.GLOBAL, TRANSITION_MSG_PARENT)
          .animate(AnimatedProperties.HEIGHT)
          .appearFrom(0),
      Transition.create(
              Transition.TransitionKeyType.GLOBAL, ExpandableElementUtil.TRANSITION_TOP_DETAIL)
          .animate(AnimatedProperties.HEIGHT)
          .appearFrom(0)
          .disappearTo(0),
      Transition.create(
              Transition.TransitionKeyType.GLOBAL, ExpandableElementUtil.TRANSITION_BOTTOM_DETAIL)
          .animate(AnimatedProperties.HEIGHT)
          .appearFrom(0)
          .disappearTo(0));
}
 
Example #3
Source File: StateResettingRootComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c, @Prop List<DataModel> dataModels, @Prop boolean showHeader) {

  final Component listComponent =
      RecyclerCollectionComponent.create(c)
          .disablePTR(true)
          .section(FavouriteGroupSection.create(new SectionContext(c)).dataModels(dataModels))
          .flexGrow(1)
          .build();

  return showHeader
      ? Column.create(c)
          .child(Text.create(c).text("Header").textSizeDip(30).build())
          .child(listComponent)
          .build()
      : listComponent;
}
 
Example #4
Source File: ListRowComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(ComponentContext c, @Prop ListRow row) {
  return Column.create(c)
      .paddingDip(YogaEdge.VERTICAL, 8)
      .paddingDip(YogaEdge.HORIZONTAL, 32)
      .child(
          Card.create(c)
              .content(
                  Column.create(c)
                      .marginDip(YogaEdge.ALL, 32)
                      .child(TitleComponent.create(c).title(row.title))
                      .child(PossiblyCrashingSubTitleComponent.create(c).subtitle(row.subtitle))
                      .build())
              .build())
      .build();
}
 
Example #5
Source File: TitleComponentSpec.java    From litho-glide with MIT License 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop(resType = STRING) String title) {
  return Text.create(c)
      .text(title)
      .textStyle(BOLD)
      .textSizeDip(24)
      .flexShrink(0)
      .backgroundColor(0xDDFFFFFF)
      .positionType(YogaPositionType.ABSOLUTE)
      .positionDip(YogaEdge.BOTTOM, 4)
      .positionDip(YogaEdge.LEFT, 4)
      .paddingDip(YogaEdge.HORIZONTAL, 6)
      .build();
}
 
Example #6
Source File: ExpandableElementOtherSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop String messageText,
    @Prop String timestamp,
    @Prop(optional = true) boolean seen,
    @State Boolean expanded) {
  final boolean isExpanded = expanded == null ? false : expanded;
  return Column.create(c)
      .paddingDip(YogaEdge.TOP, 8)
      .transitionKey(ExpandableElementUtil.TRANSITION_MSG_PARENT)
      .transitionKeyType(Transition.TransitionKeyType.GLOBAL)
      .clickHandler(ExpandableElementOther.onClick(c))
      .child(ExpandableElementUtil.maybeCreateTopDetailComponent(c, isExpanded, timestamp))
      .child(
          Column.create(c)
              .transitionKey(ExpandableElementUtil.TRANSITION_TEXT_MESSAGE_WITH_BOTTOM)
              .transitionKeyType(Transition.TransitionKeyType.GLOBAL)
              .child(
                  Row.create(c)
                      .paddingDip(YogaEdge.END, 5)
                      .child(createSenderTile(c))
                      .child(createMessageContent(c, messageText)))
              .child(ExpandableElementUtil.maybeCreateBottomDetailComponent(c, isExpanded, seen)))
      .build();
}
 
Example #7
Source File: StateUpdateTestLayoutSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop CountDownLatch awaitable,
    @Prop CountDownLatch countDownLatch,
    @Prop AtomicInteger createStateCount,
    @Prop AtomicInteger outStateValue) {

  if (countDownLatch != null) {
    countDownLatch.countDown();
  }

  if (awaitable != null) {
    try {
      awaitable.await();
    } catch (InterruptedException e) {
      throw new IllegalStateException("Received an InterruptedException " + e);
    }
  }

  return StateUpdateTestInnerLayout.create(c)
      .outStateValue(outStateValue)
      .createStateCount(createStateCount)
      .build();
}
 
Example #8
Source File: TestDataDiffSectionSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateChildren
static Children onCreateChildren(
    SectionContext c,
    @Prop List<String> data,
    @Prop(optional = true) Boolean alwaysDetectDuplicates,
    @Prop(optional = true) boolean skipCheckIsSameHandler) {
  if (skipCheckIsSameHandler) {
    return Children.create()
        .child(
            DataDiffSection.create(c)
                .data(data)
                .renderEventHandler(TestDataDiffSection.onRender(c))
                .alwaysDetectDuplicates(alwaysDetectDuplicates))
        .build();
  }
  return Children.create()
      .child(
          DataDiffSection.create(c)
              .data(data)
              .renderEventHandler(TestDataDiffSection.onRender(c))
              .onCheckIsSameItemEventHandler(TestDataDiffSection.onCheckIsSameItem(c))
              .alwaysDetectDuplicates(alwaysDetectDuplicates))
      .build();
}
 
Example #9
Source File: TestLayoutSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static <S extends View> Component onCreateLayout(
    ComponentContext context,
    @Prop @Nullable Object prop3,
    @Prop char[] prop4,
    @Prop EventHandler<ClickEvent> handler,
    @Prop Component child,
    @Prop(optional = true) boolean prop2,
    @Prop(resType = ResType.STRING, optional = true, varArg = "name") List<String> names,
    @State(canUpdateLazily = true) long state1,
    @State S state2,
    @State int state3,
    @TreeProp TestTreeProp treeProp,
    @CachedValue int cached) {
  return null;
}
 
Example #10
Source File: SpinnerSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @State String selection,
    @State boolean isShowingDropDown,
    @Prop(resType = ResType.DIMEN_TEXT, optional = true) float selectedTextSize,
    @Prop(resType = ResType.COLOR, optional = true) int selectedTextColor,
    @Prop(resType = ResType.DRAWABLE, optional = true) @Nullable Drawable caret) {
  caret = caret == null ? new CaretDrawable(c.getAndroidContext(), DEFAULT_CARET_COLOR) : caret;
  selectedTextSize =
      selectedTextSize == -1
          ? spToPx(c.getAndroidContext(), DEFAULT_TEXT_SIZE_SP)
          : selectedTextSize;

  return Row.create(c)
      .minHeightDip(SPINNER_HEIGHT)
      .justifyContent(YogaJustify.SPACE_BETWEEN)
      .paddingDip(START, MARGIN_SMALL)
      .backgroundAttr(android.R.attr.selectableItemBackground)
      .clickHandler(Spinner.onClick(c))
      .child(createSelectedItemText(c, selection, (int) selectedTextSize, selectedTextColor))
      .child(createCaret(c, caret, isShowingDropDown))
      .accessibilityRole(AccessibilityRole.DROP_DOWN_LIST)
      .build();
}
 
Example #11
Source File: PsiAnnotationProxyUtilsTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void proxyHashCode() {
  testHelper.getPsiClass(
      psiClasses -> {
        assertNotNull(psiClasses);
        PsiClass psiClass = psiClasses.get(0);
        PsiParameter[] parameters =
            PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters();

        Prop prop1 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class);
        Prop prop2 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[2], Prop.class);
        Set<Prop> props = new HashSet<>(1);
        props.add(prop1);
        props.add(prop2);
        // Calls proxy
        assertEquals(1, props.size());

        return true;
      },
      "WithAnnotationClass.java");
}
 
Example #12
Source File: TreePropSectionTestLeafLayoutSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    final ComponentContext c,
    @TreeProp TreePropNumberType propA,
    @TreeProp TreePropStringType propB,
    @Prop(optional = true) TreePropSectionTestLeafGroupSpec.Result resultPropA,
    @Prop TreePropSectionTestLeafGroupSpec.Result resultPropB) {

  if (resultPropA != null && propA != null) {
    resultPropA.mProp = new TreePropNumberType(propA.getValue() + 1);
  }

  if (resultPropB != null && propB != null) {
    resultPropB.mProp = new TreePropStringType(propB.getValue() + "_changed_again");
  }

  return Column.create(c).build();
}
 
Example #13
Source File: DelayedLoadingComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop List<DataModel> headerDataModels,
    @Prop List<DataModel> feedDataModels) {

  return RecyclerCollectionComponent.create(c)
      .disablePTR(true)
      .section(
          DelayedLoadingSection.create(new SectionContext(c))
              .headerDataModels(headerDataModels)
              .feedDataModels(feedDataModels)
              .build())
      .flexGrow(1)
      .build();
}
 
Example #14
Source File: FixedRowItemSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(ComponentContext c, @Prop boolean favourited) {
  return Row.create(c)
      .backgroundRes(favourited ? star_on : star_off)
      .widthDip(32)
      .heightDip(32)
      .clickHandler(RowItem.onClick(c))
      .build();
}
 
Example #15
Source File: TitleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(ComponentContext c, @Prop(resType = STRING) String title) {
  return Text.create(c)
      .text(title)
      .textStyle(BOLD)
      .textSizeDip(24)
      .backgroundColor(0xDDFFFFFF)
      .positionType(YogaPositionType.ABSOLUTE)
      .positionDip(YogaEdge.BOTTOM, 4)
      .positionDip(YogaEdge.LEFT, 4)
      .paddingDip(YogaEdge.HORIZONTAL, 6)
      .build();
}
 
Example #16
Source File: ComponentBodyGeneratorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void whenComponentIsGeneratedFromTest_shouldHavePublicProps() {
  final Elements elements = mCompilationRule.getElements();
  final Types types = mCompilationRule.getTypes();
  final TypeElement type =
      elements.getTypeElement(PropsTestingComponentSpec.class.getCanonicalName());
  final LayoutSpecModel model =
      mLayoutSpecModelFactory.create(
          elements, types, type, mMessager, RunMode.testing(), null, null);
  final TypeSpecDataHolder holder =
      ComponentBodyGenerator.generate(model, null, RunMode.testing());

  final Predicate<AnnotationSpec> matcher =
      annotation ->
          annotation.type.equals(ClassName.get(Prop.class))
              || annotation.type.equals(ClassName.get(TreeProp.class));

  final FieldSpec[] props =
      holder.getFieldSpecs().stream()
          .filter(field -> field.annotations.stream().anyMatch(matcher))
          .toArray(FieldSpec[]::new);
  final MethodSpec[] getters =
      holder.getMethodSpecs().stream()
          .filter(method -> method.annotations.stream().anyMatch(matcher))
          .toArray(MethodSpec[]::new);

  assertThat(getters.length)
      .describedAs("number of getters should be equal to the number of props")
      .isEqualTo(props.length);

  Arrays.stream(getters)
      .forEach(methodSpec -> assertThat(methodSpec.modifiers).contains(Modifier.PUBLIC));
}
 
Example #17
Source File: MountSpecTriggerTesterSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnTrigger(TestTriggerEvent.class)
static void triggerTestEvent(
    ComponentContext c,
    @Prop List<LifecycleStep.StepInfo> steps,
    @Prop AtomicReference<Object> triggerObjectRef,
    @FromTrigger Object triggerObject) {
  steps.add(new LifecycleStep.StepInfo(LifecycleStep.ON_TRIGGER));
  triggerObjectRef.set(triggerObject);
}
 
Example #18
Source File: HorizontalScrollScrollerComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ItemSelectedEvent.class)
static void onScrollToPositionSelected(
    ComponentContext c,
    @FromEvent String newSelection,
    @Prop RecyclerCollectionEventsController eventsController) {
  eventsController.requestScrollToPositionWithSnap(Integer.parseInt(newSelection));
}
 
Example #19
Source File: PreallocatedMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@UiThread
@OnMount
static void onMount(
    ComponentContext context, View view, @Prop List<LifecycleStep.StepInfo> steps) {
  // TODO: (T64290961) Remove the StaticContainer hack for tracing OnCreateMountContent callback.
  if (view == StaticContainer.sLastCreatedView) {
    steps.add(new StepInfo(LifecycleStep.ON_CREATE_MOUNT_CONTENT));
    StaticContainer.sLastCreatedView = null;
  }
  steps.add(new StepInfo(LifecycleStep.ON_MOUNT));
}
 
Example #20
Source File: BasicLayoutSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext context,
    @Prop String myStringProp,
    @Prop(resType = ResType.COLOR) int myRequiredColorProp,
    @Prop(resType = ResType.DIMEN_SIZE) float myDimenSizeProp,
    @Prop Component child) {
  return null;
}
 
Example #21
Source File: SimpleStateUpdateEmulatorSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop Caller caller,
    @Prop(optional = true) String prefix,
    @State int count) {
  caller.set(c);
  return Column.create(c)
      .child(Text.create(c).text((prefix != null ? prefix : "Text: ") + count))
      .build();
}
 
Example #22
Source File: TestErrorBoundarySpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c, @Prop Component child, @State Optional<String> error) {

  if (error.isPresent()) {
    return Text.create(c)
        .marginDip(YogaEdge.ALL, 8)
        .textSizeSp(24)
        .text(String.format("A WILD ERROR APPEARS:\n%s", error.get()))
        .build();
  }

  return child;
}
 
Example #23
Source File: SolidColorSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @Prop(resType = COLOR) int color,
    @Prop(optional = true, isCommonProp = true, overrideCommonPropBehavior = true) float alpha) {
  if (alpha >= 0f) {
    alpha = Math.min(1f, alpha);
    color = ColorUtils.setAlphaComponent(color, (int) (alpha * 255f));
  }
  return Image.create(c).scaleType(FIT_XY).drawable(new ColorDrawable(color)).build();
}
 
Example #24
Source File: ComponentBodyGeneratorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(Object.class)
public void testEventMethod(
    @Prop boolean arg0,
    @State int arg1,
    @Param Object arg2,
    @TreeProp long arg3,
    @Prop @Nullable Component arg4) {}
 
Example #25
Source File: ProgressSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUnmount
static void onUnmount(
    ComponentContext c,
    ProgressBar progressBar,
    @Prop(optional = true, resType = ResType.COLOR) int color,
    @FromPrepare Drawable resolvedIndeterminateDrawable) {

  // restore the color first, since it acts on the indeterminateDrawable
  if (color != Color.TRANSPARENT && progressBar.getIndeterminateDrawable() != null) {
    progressBar.getIndeterminateDrawable().mutate().clearColorFilter();
  }

  progressBar.setIndeterminateDrawable(null);
}
 
Example #26
Source File: TestMountSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void testLayoutEvent(
    ComponentContext c,
    @Prop Object prop3,
    @Prop char prop5,
    @FromEvent View view,
    @Param int param1,
    @State(canUpdateLazily = true) long state1,
    @CachedValue int cached) {}
 
Example #27
Source File: MountSpecWithShouldUpdateSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUnmount
static void onUnmount(
    ComponentContext c,
    View v,
    @Prop List<LifecycleStep> operationsOutput,
    @Prop Object objectForShouldUpdate) {
  operationsOutput.add(LifecycleStep.ON_UNMOUNT);
}
 
Example #28
Source File: HideableDataDiffSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
protected static RenderInfo onRenderEvent(
    SectionContext c,
    @FromEvent int index,
    @FromEvent Object model,
    @FromEvent Bundle loggingExtras,
    @Prop EventHandler<RenderWithHideItemHandlerEvent> renderWithHideItemHandler) {
  return HideableDataDiffSection.dispatchRenderWithHideItemHandlerEvent(
      renderWithHideItemHandler,
      index,
      model,
      HideableDataDiffSection.onHideItem(c),
      loggingExtras);
}
 
Example #29
Source File: StateGeneratorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateLayout
public void onCreateLayout(
    @Prop boolean arg0,
    @State int arg1,
    @Param Object arg2,
    @TreeProp long arg3,
    @State(canUpdateLazily = true) boolean arg4) {}
 
Example #30
Source File: LithographyRootComponentSpec.java    From litho-glide with MIT License 5 votes vote down vote up
@OnCreateLayout static Component onCreateLayout(ComponentContext c,
    @Prop final RecyclerBinder recyclerBinder) {

  return Recycler.create(c)
      .binder(recyclerBinder)
      .flexShrink(0)
      .paddingDip(YogaEdge.TOP, 8)
      .testKey(MAIN_SCREEN)
      .build();
}