com.facebook.litho.annotations.OnEvent Java Examples

The following examples show how to use com.facebook.litho.annotations.OnEvent. 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: SharedElementsComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClickEvent(
    ComponentContext c, @FromEvent View view, @Param int color, @State boolean landLitho) {
  Activity activity = (Activity) c.getAndroidContext();
  Intent intent = new Intent(c.getAndroidContext(), DetailActivity.class);
  intent.putExtra(INTENT_COLOR_KEY, color);
  intent.putExtra(INTENT_LAND_LITHO, landLitho);
  ActivityOptionsCompat options =
      ActivityOptionsCompat.makeSceneTransitionAnimation(
          activity,
          new Pair<View, String>(view, SQUARE_TRANSITION_NAME),
          new Pair<View, String>(
              activity.getWindow().getDecorView().findViewWithTag(TITLE_TRANSITION_NAME),
              TITLE_TRANSITION_NAME));
  activity.startActivity(intent, options.toBundle());
}
 
Example #2
Source File: RecyclerCollectionComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnEvent(PTRRefreshEvent.class)
protected static boolean onRefresh(
    ComponentContext c,
    @Param SectionTree sectionTree,
    @Prop(optional = true) boolean ignoreLoadingUpdates) {
  EventHandler<PTRRefreshEvent> ptrEventHandler =
      RecyclerCollectionComponent.getPTRRefreshEventHandler(c);

  if (!ignoreLoadingUpdates || ptrEventHandler == null) {
    sectionTree.refresh();
    return true;
  }

  final boolean isHandled = RecyclerCollectionComponent.dispatchPTRRefreshEvent(ptrEventHandler);
  if (!isHandled) {
    sectionTree.refresh();
  }

  return true;
}
 
Example #3
Source File: BorderEffectsComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(ComponentContext c, @FromEvent Class<? extends Component> model) {

  try {
    Method createMethod = model.getMethod("create", ComponentContext.class);
    Component.Builder componentBuilder = (Component.Builder) createMethod.invoke(null, c);
    return ComponentRenderInfo.create().component(componentBuilder.build()).build();
  } catch (NoSuchMethodException
      | IllegalAccessException
      | IllegalArgumentException
      | InvocationTargetException ex) {
    return ComponentRenderInfo.create()
        .component(Text.create(c).textSizeDip(32).text(ex.getLocalizedMessage()).build())
        .build();
  }
}
 
Example #4
Source File: ListSectionSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(final SectionContext c, @FromEvent Integer model) {
  if (model.intValue() == 1) {
    return ViewRenderInfo.create()
        .viewBinder(
            new SimpleViewBinder<TextView>() {
              @Override
              public void bind(TextView textView) {
                textView.setText("I'm a view in a Litho world");
              }
            })
        .viewCreator(VIEW_CREATOR)
        .build();
  }

  return ComponentRenderInfo.create()
      .component(
          ListItem.create(c)
              .color(model % 2 == 0 ? Color.WHITE : Color.LTGRAY)
              .title(model + ". Hello, world!")
              .subtitle("Litho tutorial")
              .build())
      .build();
}
 
Example #5
Source File: ComposedAnimationsComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(ComponentContext c, @FromEvent int index) {
  final int numDemos = 5;
  Component component;
  // Keep alternating between demos
  switch (index % numDemos) {
    case 0:
      component = StoryFooterComponent.create(c).key("footer").build();
      break;
    case 1:
      component = UpDownBlocksComponent.create(c).build();
      break;
    case 2:
      component = LeftRightBlocksComponent.create(c).build();
      break;
    case 3:
      component = OneByOneLeftRightBlocksComponent.create(c).build();
      break;
    case 4:
      component = LeftRightBlocksSequenceComponent.create(c).build();
      break;
    default:
      throw new RuntimeException("Bad index: " + index);
  }
  return ComponentRenderInfo.create().component(component).build();
}
 
Example #6
Source File: ClassAnnotationsGeneratorTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateAnnotations() {
  final ImmutableList<AnnotationSpec> annotations =
      ImmutableList.of(
          AnnotationSpec.builder(Deprecated.class).build(),
          AnnotationSpec.builder(OnEvent.class).build());
  final SpecModel specModel =
      SpecModelImpl.newBuilder()
          .qualifiedSpecClassName("com.example.MyComponentSpec")
          .delegateMethods(ImmutableList.<SpecMethodModel<DelegateMethod, Void>>of())
          .representedObject(new Object())
          .classAnnotations(annotations)
          .build();

  final TypeSpecDataHolder dataHolder = ClassAnnotationsGenerator.generate(specModel);

  assertThat(dataHolder.getAnnotationSpecs())
      .hasSize(2)
      .contains(AnnotationSpec.builder(Deprecated.class).build())
      .contains(AnnotationSpec.builder(OnEvent.class).build());
}
 
Example #7
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 #8
Source File: SelectedItemRootComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(
    ComponentContext c,
    @Prop int selectedItem,
    @FromEvent DataModel model,
    @FromEvent int index) {
  return ComponentRenderInfo.create()
      .component(
          Row.create(c)
              .child(Text.create(c).text(model.getData()).textSizeDip(30))
              .child(FixedRowItem.create(c).favourited(selectedItem == index))
              .build())
      .build();
}
 
Example #9
Source File: DelayedLoadingSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(SectionContext c, @FromEvent DataModel model) {
  return ComponentRenderInfo.create()
      .component(
          Row.create(c)
              .child(Text.create(c).text(model.getData()).textSizeDip(30))
              .child(RowItem.create(c))
              .build())
      .build();
}
 
Example #10
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 #11
Source File: FavouriteGroupSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(SectionContext c, @FromEvent DataModel model) {
  return ComponentRenderInfo.create()
      .component(
          Row.create(c)
              .child(Text.create(c).text(model.getData()).textSizeDip(30))
              .child(RowItem.create(c))
              .build())
      .build();
}
 
Example #12
Source File: HorizontalScrollScrollerComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(
    ComponentContext c,
    @Prop RecyclerCollectionEventsController eventsController,
    @Param boolean forward) {
  if (forward) {
    eventsController.requestScrollToNextPosition(true);
  } else {
    eventsController.requestScrollToPreviousPosition(true);
  }
}
 
Example #13
Source File: DemoListComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Called during DataDiffSection's diffing to determine if two objects contain the same data. This
 * is used to detect of contents of an item have changed. See {@link
 * androidx.recyclerview.widget.DiffUtil.Callback#areContentsTheSame(int, int)} for more info.
 *
 * @return true if the two objects contain the same data.
 */
@OnEvent(OnCheckIsSameContentEvent.class)
static boolean isSameContent(
    ComponentContext c,
    @FromEvent DemoListActivity.DemoListDataModel previousItem,
    @FromEvent DemoListActivity.DemoListDataModel nextItem) {
  // We're only displaying the name so checking if that's equal here is enough for our use case.
  return previousItem == null
      ? nextItem == null
      : previousItem.name == null
          ? nextItem.name == null
          : nextItem.name.equals(previousItem.name);
}
 
Example #14
Source File: LifecycleDelegateComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClickStateUpdate(ComponentContext c, @State Random random, @Param boolean isSync) {
  if (isSync) {
    LifecycleDelegateComponent.updateBricksSync(c, random);
  } else {
    LifecycleDelegateComponent.updateBricksAsync(c, random);
  }
}
 
Example #15
Source File: TestGroupSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(OnCheckIsSameContentEvent.class)
protected static boolean onCheckIsSameContent(
    SectionContext c,
    @FromEvent Object previousItem,
    @FromEvent Object nextItem,
    @Prop(optional = true) Comparator isSameContentComparator) {
  return isSameContentComparator.compare(previousItem, nextItem) == 0;
}
 
Example #16
Source File: DemoListItemComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(
    ComponentContext c,
    @FromEvent View view,
    @Prop final DemoListActivity.DemoListDataModel model,
    @Prop final int[] currentIndices) {
  final Intent intent =
      new Intent(
          c.getAndroidContext(), model.datamodels == null ? model.klass : DemoListActivity.class);
  intent.putExtra(DemoListActivity.INDICES, currentIndices);
  c.getAndroidContext().startActivity(intent);
}
 
Example #17
Source File: TestGroupSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
protected static RenderInfo onRender(
    SectionContext c, @FromEvent Object model, @Param ComponentContext context) {
  return ComponentRenderInfo.create()
      .customAttribute("model", model)
      .component(Text.create(context).text(model.toString()).build())
      .build();
}
 
Example #18
Source File: InefficientFavouriteGroupSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(SectionContext c, @FromEvent DataModel model) {
  return ComponentRenderInfo.create()
      .component(
          Row.create(c)
              .child(Text.create(c).text(model.getData()).textSizeDip(30))
              .child(RowItem.create(c))
              .build())
      .build();
}
 
Example #19
Source File: ExpandingPickerComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClickEvent(
    ComponentContext c,
    @State AtomicReference<Animator> animatorRef,
    @State AtomicReference<Boolean> isExpanded,
    @State DynamicValue<Float> expandedAmount,
    @State DynamicValue<Float> contractButtonRotation,
    @State DynamicValue<Float> contractButtonScale,
    @State DynamicValue<Float> contractButtonAlpha,
    @State DynamicValue<Float> expandButtonScale,
    @State DynamicValue<Float> expandButtonAlpha,
    @State DynamicValue<Float> menuButtonAlpha) {

  Animator oldAnimator = animatorRef.get();
  if (oldAnimator != null) {
    oldAnimator.cancel();
  }

  Animator newAnimator =
      createExpandCollapseAnimator(
          !Boolean.TRUE.equals(isExpanded.get()),
          expandedAmount,
          contractButtonRotation,
          contractButtonScale,
          contractButtonAlpha,
          expandButtonScale,
          expandButtonAlpha,
          menuButtonAlpha);
  isExpanded.set(!Boolean.TRUE.equals(isExpanded.get()));
  animatorRef.set(newAnimator);
  newAnimator.start();
}
 
Example #20
Source File: HideableDataDiffSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(HideItemEvent.class)
public static void onHideItem(
    SectionContext c,
    @FromEvent Object model,
    @Prop EventHandler<GetUniqueIdentifierEvent> getUniqueIdentifierHandler) {
  HideableDataDiffSection.onBlacklistUpdateSync(c, model, getUniqueIdentifierHandler);
}
 
Example #21
Source File: BounceExampleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(
    ComponentContext c,
    @State AtomicReference<Animator> animatorRef,
    @State DynamicValue<Float> translationY) {
  Animator oldAnimator = animatorRef.get();
  if (oldAnimator != null) {
    oldAnimator.cancel();
  }

  final Animator newAnimator = createBounceAnimator(c, translationY);
  animatorRef.set(newAnimator);
  newAnimator.start();
}
 
Example #22
Source File: DemoListItemComponentSpec.java    From litho-glide with MIT License 5 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(
    ComponentContext c,
    @FromEvent View view,
    @Prop final String name) {
  final Intent intent = new Intent(c.getAndroidContext(), DemoActivity.class);
  intent.putExtra("demoName", name);
  c.getAndroidContext().startActivity(intent);
}
 
Example #23
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 #24
Source File: BlocksSameTransitionKeyComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(TransitionEndEvent.class)
static void onTransitionEndEvent(
    ComponentContext c,
    @State Set<String> runningAnimations,
    @FromEvent String transitionKey,
    @FromEvent AnimatedProperty property) {
  if (property == AnimatedProperties.X) {
    BlocksSameTransitionKeyComponent.updateRunningAnimations(c, ANIM_X);
    BlocksSameTransitionKeyComponent.updateStateSync(c, false);
  } else {
    BlocksSameTransitionKeyComponent.updateRunningAnimations(c, ANIM_ALPHA);
  }
}
 
Example #25
Source File: TTIMarkerSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(RenderEvent.class)
static RenderInfo onRender(final SectionContext c, @FromEvent String model) {
  return ComponentRenderInfo.create()
      .component(Text.create(c).text(model).textSizeSp(14).build())
      .renderCompleteHandler(TTIMarkerSection.onRenderComplete(c, RENDER_MARKER))
      .build();
}
 
Example #26
Source File: EventMethodExtractorTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnEvent(Object.class)
public <T extends Integer> void testMethod(
    @Prop boolean testProp,
    @State int testState,
    @Param Object testPermittedAnnotation,
    @Event Object testNotPermittedAnnotation,
    T testTypeVariable) {
  // Don't do anything.
}
 
Example #27
Source File: DemoListComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Called during DataDiffSection's diffing to determine if two objects represent the same item.
 * See {@link androidx.recyclerview.widget.DiffUtil.Callback#areItemsTheSame(int, int)} for more
 * info.
 *
 * @return true if the two objects in the event represent the same item.
 */
@OnEvent(OnCheckIsSameItemEvent.class)
static boolean isSameItem(
    ComponentContext c,
    @FromEvent DemoListActivity.DemoListDataModel previousItem,
    @FromEvent DemoListActivity.DemoListDataModel nextItem) {
  return previousItem == nextItem;
}
 
Example #28
Source File: PsiEventMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
static ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> getOnEventMethods(
    PsiClass psiClass, List<Class<? extends Annotation>> permittedInterStageInputAnnotations) {
  final List<SpecMethodModel<EventMethod, EventDeclarationModel>> delegateMethods =
      new ArrayList<>();

  for (PsiMethod psiMethod : psiClass.getMethods()) {
    final PsiAnnotation onEventAnnotation =
        AnnotationUtil.findAnnotation(psiMethod, OnEvent.class.getName());
    if (onEventAnnotation == null) {
      continue;
    }

    PsiClassObjectAccessExpression accessExpression =
        (PsiClassObjectAccessExpression) onEventAnnotation.findAttributeValue("value");
    final List<MethodParamModel> methodParams =
        getMethodParams(
            psiMethod,
            EventMethodExtractor.getPermittedMethodParamAnnotations(
                permittedInterStageInputAnnotations),
            permittedInterStageInputAnnotations,
            ImmutableList.of());

    final SpecMethodModel<EventMethod, EventDeclarationModel> eventMethod =
        new SpecMethodModel<>(
            ImmutableList.of(),
            PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()),
            psiMethod.getName(),
            PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()),
            ImmutableList.copyOf(getTypeVariables(psiMethod)),
            ImmutableList.copyOf(methodParams),
            psiMethod,
            PsiEventDeclarationsExtractor.getEventDeclarationModel(accessExpression));
    delegateMethods.add(eventMethod);
  }

  return ImmutableList.copyOf(delegateMethods);
}
 
Example #29
Source File: StoryFooterComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(ComponentContext c) {
  StoryFooterComponent.updateStateSync(c);
}
 
Example #30
Source File: OneByOneLeftRightBlocksComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnEvent(ClickEvent.class)
static void onClick(ComponentContext c) {
  OneByOneLeftRightBlocksComponent.updateStateSync(c);
}