Java Code Examples for com.facebook.litho.ComponentContext#getAndroidContext()

The following examples show how to use com.facebook.litho.ComponentContext#getAndroidContext() . 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: 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 2
Source File: RecyclerBinder.java    From litho with Apache License 2.0 6 votes vote down vote up
/** @param c The {@link ComponentContext} the RecyclerBinder will use. */
public RecyclerBinder build(ComponentContext c) {
  componentContext =
      new ComponentContext(
          c.getAndroidContext(), c.getLogTag(), c.getLogger(), null, c.getTreePropsCopy());

  // Incremental mount will not work if this ComponentTree is nested in a parent with it turned
  // off, so always disable it in that case
  incrementalMount = incrementalMount && ComponentContext.isIncrementalMountEnabled(c);
  visibilityProcessing =
      visibilityProcessing && ComponentContext.isVisibilityProcessingEnabled(c);

  if (recyclingMode == ComponentTree.RecyclingMode.DEFAULT) {
    // Only override from parent if recycling mode is not explicitly set.
    recyclingMode = c.getRecyclingMode();
  }

  if (layoutInfo == null) {
    layoutInfo = new LinearLayoutInfo(c.getAndroidContext(), VERTICAL, false);
  }

  return new RecyclerBinder(this);
}
 
Example 3
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 4
Source File: ComponentTestHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
/** Use {@link #triggerVisibilityEvent(LithoView, Class)} instead */
@Deprecated
private static LithoView dispatchVisibilityEvent(
    ComponentContext context,
    EventHandler eventHandler,
    Object eventInstance,
    Component component) {
  LithoView lithoView = new LithoView(context);
  FrameLayout parent = new FrameLayout(context.getAndroidContext());

  parent.addView(lithoView);

  mountComponent(context, lithoView, component, true, true, 100, 100);

  lithoView.notifyVisibleBoundsChanged();

  eventHandler.mHasEventDispatcher = component;

  try {
    Whitebox.invokeMethod(
        component.getEventDispatcher(), "dispatchOnEvent", eventHandler, eventInstance);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }

  return lithoView;
}
 
Example 5
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 6
Source File: ClockComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  final ClockView clockView = new ClockView(c.getAndroidContext());
  clockView.measure(
      MeasureUtils.getViewMeasureSpec(widthSpec), MeasureUtils.getViewMeasureSpec(heightSpec));
  size.width = clockView.getMeasuredWidth();
  size.height = clockView.getMeasuredHeight();
}
 
Example 7
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  mComponentContext = new ComponentContext(getApplicationContext());
  mRecyclerView = new TestRecyclerView(mComponentContext.getAndroidContext());

  mLayoutThreadShadowLooper =
      Shadows.shadowOf(
          (Looper) Whitebox.invokeMethod(ComponentTree.class, "getDefaultLayoutThreadLooper"));
}
 
Example 8
Source File: RecyclerSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  mComponentContext = new ComponentContext(getApplicationContext());
  mRecyclerView = new TestLithoRecyclerView(mComponentContext.getAndroidContext());
  mSectionsRecyclerView =
      new TestSectionsRecyclerView(mComponentContext.getAndroidContext(), mRecyclerView);
  mSectionsRecyclerView.setHasBeenDetachedFromWindow(true);
}
 
Example 9
Source File: RecyclerEventsControllerTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  mComponentContext = new ComponentContext(getApplicationContext());
  mRecyclerView = new RecyclerView(mComponentContext.getAndroidContext());
  mSectionsRecyclerView =
      new TestSectionsRecyclerView(mComponentContext.getAndroidContext(), mRecyclerView);
  mOnRecyclerUpdateListener = mock(RecyclerEventsController.OnRecyclerUpdateListener.class);
  mRecyclerEventsController = new RecyclerEventsController();
  mRecyclerEventsController.setSectionsRecyclerView(mSectionsRecyclerView);
}
 
Example 10
Source File: SectionContext.java    From litho with Apache License 2.0 5 votes vote down vote up
public SectionContext(ComponentContext context) {
  this(
      context.getAndroidContext(),
      context.getLogTag(),
      context.getLogger(),
      context.getTreePropsCopy());
}
 
Example 11
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 12
Source File: DemoListItemComponentSpec.java    From litho-picasso 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 13
Source File: TextInputSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop(optional = true, resType = ResType.STRING) CharSequence hint,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable inputBackground,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
    @Prop(optional = true, resType = ResType.COLOR) int shadowColor,
    @Prop(optional = true) ColorStateList textColorStateList,
    @Prop(optional = true) ColorStateList hintColorStateList,
    @Prop(optional = true, resType = ResType.COLOR) Integer highlightColor,
    @Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
    @Prop(optional = true) Typeface typeface,
    @Prop(optional = true) int textAlignment,
    @Prop(optional = true) int gravity,
    @Prop(optional = true) boolean editable,
    @Prop(optional = true) int inputType,
    @Prop(optional = true) int imeOptions,
    @Prop(optional = true, varArg = "inputFilter") List<InputFilter> inputFilters,
    @Prop(optional = true) boolean multiline,
    @Prop(optional = true) TextUtils.TruncateAt ellipsize,
    @Prop(optional = true) int minLines,
    @Prop(optional = true) int maxLines,
    @Prop(optional = true) int cursorDrawableRes,
    @Prop(optional = true, resType = ResType.STRING) CharSequence error,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable errorDrawable,
    @State AtomicReference<CharSequence> savedText,
    @State int measureSeqNumber) {

  // The height should be the measured height of EditText with relevant params
  final EditText forMeasure = new ForMeasureEditText(c.getAndroidContext());
  // If text contains Spans, we don't want it to be mutable for the measurement case
  CharSequence text = savedText.get();
  if (text instanceof Spannable) {
    text = text.toString();
  }
  setParams(
      forMeasure,
      hint,
      getBackgroundOrDefault(
          c, inputBackground == UNSET_DRAWABLE ? forMeasure.getBackground() : inputBackground),
      shadowRadius,
      shadowDx,
      shadowDy,
      shadowColor,
      textColorStateList,
      hintColorStateList,
      highlightColor,
      textSize,
      typeface,
      textAlignment,
      gravity,
      editable,
      inputType,
      imeOptions,
      inputFilters,
      multiline,
      ellipsize,
      minLines,
      maxLines,
      cursorDrawableRes,
      forMeasure.getMovementMethod(),
      // onMeasure happens:
      // 1. After initState before onMount: savedText = initText.
      // 2. After onMount before onUnmount: savedText preserved from underlying editText.
      text,
      error,
      errorDrawable);
  forMeasure.measure(
      MeasureUtils.getViewMeasureSpec(widthSpec), MeasureUtils.getViewMeasureSpec(heightSpec));

  size.height = forMeasure.getMeasuredHeight();

  // For width we always take all available space, or collapse to 0 if unspecified.
  if (SizeSpec.getMode(widthSpec) == SizeSpec.UNSPECIFIED) {
    size.width = 0;
  } else {
    size.width = Math.min(SizeSpec.getSize(widthSpec), forMeasure.getMeasuredWidth());
  }
}
 
Example 14
Source File: LinearLayoutInfo.java    From litho with Apache License 2.0 4 votes vote down vote up
public LinearLayoutInfo(ComponentContext context, int orientation, boolean reverseLayout) {
  this(context.getAndroidContext(), orientation, reverseLayout);
}
 
Example 15
Source File: RecyclerBinderCanRemeasureTest.java    From litho with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  mComponentContext = new ComponentContext(getApplicationContext());
  mRecyclerView = new TestRecyclerView(mComponentContext.getAndroidContext());
}