private LithoView getLithoView(ComponentTree componentTree) { LithoView lithoView = new LithoView(mContext); lithoView.setComponentTree(componentTree); lithoView.measure( View.MeasureSpec.makeMeasureSpec(HOST_WIDTH, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(HOST_HEIGHT, View.MeasureSpec.EXACTLY)); lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); return lithoView; } }
public void setContentComponent(Component component) { isMain = false; ExamplesLithoLabActivity.this.setContentView( LithoView.create(ExamplesLithoLabActivity.this /* context */, component)); } }
void mount(Component component, int width, int height) { if (mLithoView.getComponentTree() == null) { mLithoView.setComponentTree( ComponentTree.create(mLithoView.getComponentContext(), component) .incrementalMount(false) .build()); } else { mLithoView.setComponent(component); } mComponentWidth = width; mComponentHeight = height; }
public void hideStickyHeader() { mStickyHeader.unmountAllItems(); mStickyHeader.setVisibility(View.GONE); }
public void showStickyHeader() { mStickyHeader.setVisibility(View.VISIBLE); mStickyHeader.performIncrementalMount(); }
/** Sets or replaces the ComponentTree being rendered in this Activity. */ public void setComponentTree(ComponentTree componentTree) { final LithoView lithoView = new LithoView(this); lithoView.setComponentTree(componentTree); setContentView(lithoView); } }
private LithoView getLithoView(Component component) { LithoView lithoView = new LithoView(mContext); lithoView.setComponent(component); lithoView.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); return lithoView; }
@Test public void testMeasureComputesLayoutStateWhenSpecsAreNotExact() { mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent( TestDrawableComponent.create(mLithoView.getComponentContext()).heightPx(100).build()); mLithoView.measure(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, AT_MOST)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(100); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); assertThat(mLithoView.getComponentTree().getMainThreadLayoutState()).isNotNull(); }
public static LithoView createAndMeasureLithoView( ComponentContext c, Component component, int widthSpec, int heightSpec) { final ComponentTree componentTree = ComponentTree.create(c, component).build(); final LithoView lithoView = new LithoView(c); lithoView.setComponentTree(componentTree); lithoView.onAttachedToWindow(); lithoView.measure(widthSpec, heightSpec); // This is really lithoView#layout without the mount componentTree.measure(widthSpec, heightSpec, LAYOUT_SIZE_OUT, false); return lithoView; }
@Test public void testCorrectsDoubleMeasureBug() { mLithoView = setupLithoViewForDoubleMeasureTest(411, 2.625f, 1080); mLithoView.measure(makeMeasureSpec(1079, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(1080); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); }
@Test public void testComponentTreeReleaseClearsView() { Component component = TestDrawableComponent.create(mContext) .build(); ComponentTree componentTree = create( mContext, component) .build(); LithoView lithoView = new LithoView(mContext); lithoView.setComponentTree(componentTree); assertThat(componentTree).isEqualTo(lithoView.getComponentTree()); componentTree.release(); assertThat(lithoView.getComponentTree()).isNull(); }
@Override protected LithoView buildView(@NonNull ViewGroup parent) { return new LithoView(parent.getContext()); } }
/** * Change the root component measuring it on a background thread before updating the UI. * If this {@link LithoView} doesn't have a ComponentTree initialized, the root will be * computed synchronously. */ public void setComponentAsync(Component component) { if (mComponentTree == null) { setComponentTree(ComponentTree.create(getComponentContext(), component).build()); } else { mComponentTree.setRootAsync(component); } }
@Override public void onViewRecycled(BaseViewHolder holder) { if (holder.isLithoViewType) { final LithoView lithoView = (LithoView) holder.itemView; lithoView.unmountAllItems(); lithoView.setComponentTree(null); lithoView.setInvalidStateLogParamsList(null); } else { final ViewBinder viewBinder = holder.viewBinder; if (viewBinder != null) { viewBinder.unbind(holder.itemView); holder.viewBinder = null; } } }
@OnUnmount static void onUnmount(ComponentContext c, LithoView mountedView) { mountedView.setComponentTree(null); } }
@Test public void testDontThrowWhenLayoutStateIsNull() { final ComponentTree mockComponentTree = mock(ComponentTree.class); mLithoView.setComponentTree(mockComponentTree); mLithoView.requestLayout(); mLithoView.performIncrementalMount(); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (mStickyHeader.getVisibility() == View.GONE) { return; } final int stickyHeaderLeft = getPaddingLeft(); final int stickyHeaderTop = getPaddingTop(); mStickyHeader.layout( stickyHeaderLeft, stickyHeaderTop, stickyHeaderLeft + mStickyHeader.getMeasuredWidth(), stickyHeaderTop + mStickyHeader.getMeasuredHeight()); }
/** * Unmounts a component tree from a component view. * @param lithoView the view to unmount */ public static void unmountComponent(LithoView lithoView) { if (!lithoView.isIncrementalMountEnabled()) { throw new IllegalArgumentException( "In order to test unmounting a Component, it needs to be mounted with " + "incremental mount enabled. Please use a mountComponent() variation that " + "accepts an incrementalMountEnabled argument"); } // Unmounting the component by running incremental mount to a Rect that we certain won't // contain the component. Rect rect = new Rect(99999, 99999, 999999, 999999); lithoView.performIncrementalMount(rect, true); }
@Test public void measureBeforeBeingAttached() { mLithoView.measure( makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); mLithoView.layout( 0, 0, mLithoView.getMeasuredWidth(), mLithoView.getMeasuredHeight()); // View got measured. assertThat(mLithoView.getMeasuredWidth()).isGreaterThan(0); assertThat(mLithoView.getMeasuredHeight()).isGreaterThan(0); // Attaching will automatically mount since we already have a layout fitting our size. ShadowView shadow = shadowOf(mLithoView); shadow.callOnAttachedToWindow(); assertThat(getInternalMountItems(mLithoView)).hasSize(2); }
@Nullable public static DebugComponent getRootInstance(LithoView view) { return getRootInstance(view.getComponentTree()); }