Skip to content

Commit

Permalink
Convert BaseMountingView to Kotlin
Browse files Browse the repository at this point in the history
Summary: As per title

Reviewed By: pasqualeanatriello

Differential Revision: D67277554

fbshipit-source-id: cffda9114d1b708bd7facdc6c7bd2a631e2cacbb
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Jan 2, 2025
1 parent 3342a52 commit 0568a4f
Show file tree
Hide file tree
Showing 12 changed files with 1,334 additions and 1,410 deletions.
1,368 changes: 0 additions & 1,368 deletions litho-core/src/main/java/com/facebook/litho/BaseMountingView.java

This file was deleted.

1,268 changes: 1,268 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/BaseMountingView.kt

Large diffs are not rendered by default.

48 changes: 26 additions & 22 deletions litho-core/src/main/java/com/facebook/litho/LithoRenderTreeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LithoRenderTreeView @JvmOverloads constructor(context: Context, attrs: Att

// Set together
private var layoutState: LayoutState? = null
private var treeState: TreeState? = null
private var _treeState: TreeState? = null

private var currentLifecycleOwner: LifecycleOwner? = null

Expand Down Expand Up @@ -65,34 +65,38 @@ class LithoRenderTreeView @JvmOverloads constructor(context: Context, attrs: Att
}
}

override fun getConfiguration(): ComponentsConfiguration? {
return layoutState?.componentContext?.lithoConfiguration?.componentsConfig
}
override val configuration: ComponentsConfiguration?
get() = layoutState?.componentContext?.lithoConfiguration?.componentsConfig

override fun isIncrementalMountEnabled(): Boolean {
val componentContext = layoutState?.componentContext ?: return false
return ComponentContext.isIncrementalMountEnabled(componentContext)
}
override val isIncrementalMountEnabled: Boolean
get() {
val componentContext = layoutState?.componentContext ?: return false
return ComponentContext.isIncrementalMountEnabled(componentContext)
}

override fun getCurrentLayoutState(): LayoutState? = layoutState
override val currentLayoutState: LayoutState?
get() = layoutState

override fun isVisibilityProcessingEnabled(): Boolean {
val componentContext = layoutState?.componentContext ?: return false
return ComponentContext.isVisibilityProcessingEnabled(componentContext)
}
override val isVisibilityProcessingEnabled: Boolean
get() {
val componentContext = layoutState?.componentContext ?: return false
return ComponentContext.isVisibilityProcessingEnabled(componentContext)
}

override fun getTreeState(): TreeState? = treeState
override val treeState: TreeState?
get() = _treeState

override fun hasTree(): Boolean = layoutState != null
override val hasTree: Boolean
get() = layoutState != null

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// mAnimatedWidth/mAnimatedHeight >= 0 if something is driving a width/height animation.
val isAnimating = mAnimatedWidth != -1 || mAnimatedHeight != -1
val isAnimating = animatedWidth != -1 || animatedHeight != -1
// up to date view sizes, taking into account running animations
val upToDateWidth = if (mAnimatedWidth != -1) mAnimatedWidth else width
val upToDateHeight = if (mAnimatedHeight != -1) mAnimatedHeight else height
mAnimatedWidth = -1
mAnimatedHeight = -1
val upToDateWidth = if (animatedWidth != -1) animatedWidth else width
val upToDateHeight = if (animatedHeight != -1) animatedHeight else height
animatedWidth = -1
animatedHeight = -1
if (isAnimating) {
// If the mount state is dirty, we want to ignore the current animation and calculate the
// new LayoutState as normal below. That LayoutState has the opportunity to define its own
Expand Down Expand Up @@ -154,7 +158,7 @@ class LithoRenderTreeView @JvmOverloads constructor(context: Context, attrs: Att
onBeforeSettingNewTree()
}
this.layoutState = layoutState
this.treeState = treeState
this._treeState = treeState

if (this.layoutState == null) {
clearDebugOverlay(this)
Expand All @@ -171,7 +175,7 @@ class LithoRenderTreeView @JvmOverloads constructor(context: Context, attrs: Att
currentLifecycleOwner?.lifecycle?.removeObserver(this)
currentLifecycleOwner = null
layoutState = null
treeState = null
_treeState = null
hasNewTree = true
requestLayout()
}
Expand Down
31 changes: 23 additions & 8 deletions litho-core/src/main/java/com/facebook/litho/LithoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

private void onMeasureInternal(int widthMeasureSpec, int heightMeasureSpec) {
// mAnimatedWidth/mAnimatedHeight >= 0 if something is driving a width/height animation.
final boolean animating = mAnimatedWidth != SIZE_UNSET || mAnimatedHeight != SIZE_UNSET;
final boolean animating = animatedWidth != SIZE_UNSET || animatedHeight != SIZE_UNSET;
// up to date view sizes, taking into account running animations
final int upToDateWidth = (mAnimatedWidth != SIZE_UNSET) ? mAnimatedWidth : getWidth();
final int upToDateHeight = (mAnimatedHeight != SIZE_UNSET) ? mAnimatedHeight : getHeight();
mAnimatedWidth = SIZE_UNSET;
mAnimatedHeight = SIZE_UNSET;
final int upToDateWidth = (animatedWidth != SIZE_UNSET) ? animatedWidth : getWidth();
final int upToDateHeight = (animatedHeight != SIZE_UNSET) ? animatedHeight : getHeight();
animatedWidth = SIZE_UNSET;
animatedHeight = SIZE_UNSET;

if (animating) {
// If the mount state is dirty, we want to ignore the current animation and calculate the
Expand Down Expand Up @@ -572,7 +572,7 @@ protected void onDetached() {
}

@Override
Object onBeforeMount() {
public Object onBeforeMount() {
super.onBeforeMount();
final boolean loggedFirstMount =
LithoView.MountStartupLoggingInfo.maybeLogFirstMountStart(mMountStartupLoggingInfo);
Expand All @@ -582,7 +582,7 @@ Object onBeforeMount() {
}

@Override
void onAfterMount(@Nullable Object fromOnBeforeMount) {
public void onAfterMount(@Nullable Object fromOnBeforeMount) {
super.onAfterMount(fromOnBeforeMount);
if (fromOnBeforeMount == null) {
throw new IllegalStateException(
Expand Down Expand Up @@ -642,7 +642,7 @@ protected TreeState getTreeState() {
}

@Override
protected boolean hasTree() {
protected boolean getHasTree() {
return mComponentTree != null;
}

Expand Down Expand Up @@ -797,6 +797,21 @@ public void rerenderForAccessibility(boolean enabled) {
forceRelayout();
}

@Override
public void setVisibilityHintNonRecursive(boolean isVisible) {
super.setVisibilityHintNonRecursive(isVisible);
}

@Override
public void resetVisibilityHint() {
super.resetVisibilityHint();
}

@Override
public void performLayout(boolean changed, int l, int t, int r, int b) {
super.performLayout(changed, l, t, r, b);
}

/**
* LayoutParams that override the LayoutManager.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object ViewAttributesViewBinder : RenderUnit.Binder<ViewAttributesViewBinder.Mod
override fun bind(context: Context, content: View, model: Model, layoutData: Any?): Int {
val flags =
if (model.isRootHost) {
(content as BaseMountingView).mViewAttributeFlags
(content as BaseMountingView).viewAttributeFlags
} else {
LithoMountData.getViewAttributeFlags(content)
}
Expand Down
4 changes: 4 additions & 0 deletions litho-it/src/test/com/facebook/litho/LithoViewMountTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,9 @@ class LithoViewMountTest {
fun setMeasured(width: Int, height: Int) {
setMeasuredDimension(width, height)
}

public override fun performLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
super.performLayout(changed, l, t, r, b)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class LithoVisibilityEventsControllerTest {
@Test
fun lithoLifecycleProviderComponentTreeResetVisibilityFlags() {
// In the new implementation, `setVisibilityHintNonRecursive` is always called in
// `setLithoView`, so mHasVisibilityHint will be still true after set new Component Tree
// `setLithoView`, so hasVisibilityHint will be still true after set new Component Tree
val testLithoView =
lithoTestRule
.createTestLithoView(lithoView = lithoView, widthPx = 10, heightPx = 5)
Expand All @@ -156,19 +156,19 @@ class LithoVisibilityEventsControllerTest {
lithoLifecycleProviderDelegate.moveToVisibilityState(
LithoVisibilityEventsController.LithoVisibilityState.HINT_INVISIBLE)
var hasVisibilityHint: Boolean =
Whitebox.getInternalState<Boolean>(testLithoView.lithoView, "mHasVisibilityHint")
Whitebox.getInternalState<Boolean>(testLithoView.lithoView, "hasVisibilityHint")
var pauseMountingWhileVisibilityHintFalse: Boolean =
Whitebox.getInternalState<Boolean>(
testLithoView.lithoView, "mPauseMountingWhileVisibilityHintFalse")
testLithoView.lithoView, "pauseMountingWhileVisibilityHintFalse")
assertThat(hasVisibilityHint).isTrue
assertThat(pauseMountingWhileVisibilityHintFalse).isTrue

testLithoView.useComponentTree(ComponentTree.create(lithoTestRule.context).build())
hasVisibilityHint =
Whitebox.getInternalState<Boolean>(testLithoView.lithoView, "mHasVisibilityHint")
Whitebox.getInternalState<Boolean>(testLithoView.lithoView, "hasVisibilityHint")
pauseMountingWhileVisibilityHintFalse =
Whitebox.getInternalState<Boolean>(
testLithoView.lithoView, "mPauseMountingWhileVisibilityHintFalse")
testLithoView.lithoView, "pauseMountingWhileVisibilityHintFalse")
assertThat(hasVisibilityHint).isFalse
assertThat(pauseMountingWhileVisibilityHintFalse).isFalse
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,8 @@ class MountStateIncrementalMountTest {
previousIncrementalMountBounds.set(visibleRect)
}

override fun isIncrementalMountEnabled(): Boolean = true
override val isIncrementalMountEnabled: Boolean
get() = true
}

companion object {
Expand Down
4 changes: 2 additions & 2 deletions litho-it/src/test/com/facebook/litho/MountStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class MountStateTest {
val mountDelegate = lithoView.mountDelegateTarget.getMountDelegate()
var coordinator =
Whitebox.getInternalState<LithoHostListenerCoordinator>(
lithoView, "mLithoHostListenerCoordinator")
lithoView, "lithoHostListenerCoordinator")
assertThat(coordinator).isNotNull
assertThat(coordinator.visibilityExtensionState).isNotNull
assertThat(coordinator.incrementalMountExtensionState).isNotNull
Expand All @@ -201,7 +201,7 @@ class MountStateTest {

// Unmount the parent
testLithoView.lithoView.unmountAllItems()
coordinator = Whitebox.getInternalState(lithoView, "mLithoHostListenerCoordinator")
coordinator = Whitebox.getInternalState(lithoView, "lithoHostListenerCoordinator")
assertThat(coordinator).isNull()
assertThat(mountDelegate).isNotNull
assertThat(mountDelegate?.extensionStates).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ class VisibilityEventsIncrementalMountDisabledTest {
private fun getVisibilityIdToItemMap(lithoView: LithoView): Map<String?, VisibilityItem>? {
val lithoHostListenerCoordinator =
Whitebox.getInternalState<LithoHostListenerCoordinator>(
lithoView, "mLithoHostListenerCoordinator")
lithoView, "lithoHostListenerCoordinator")
val extensionState =
Whitebox.getInternalState<ExtensionState<VisibilityMountExtensionState>>(
lithoHostListenerCoordinator, "mVisibilityExtensionState")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ class VisibilityEventsTest {

private fun getCurrentVisibilityToItemMap(lithoView: LithoView): Map<String?, VisibilityItem?>? {
val lithoHostListenerCoordinator: LithoHostListenerCoordinator? =
Whitebox.getInternalState(lithoView, "mLithoHostListenerCoordinator")
Whitebox.getInternalState(lithoView, "lithoHostListenerCoordinator")
val extensionState: ExtensionState<VisibilityMountExtensionState>? =
Whitebox.getInternalState(lithoHostListenerCoordinator, "mVisibilityExtensionState")
return if (extensionState != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class VisibilityEventsWithVisibilityExtensionTest {
) {
val lithoHostListenerCoordinator =
Whitebox.getInternalState<LithoHostListenerCoordinator>(
lithoView, "mLithoHostListenerCoordinator")
lithoView, "lithoHostListenerCoordinator")
lithoHostListenerCoordinator.useVisibilityExtension(visibilityOutputsExtension)
}
}

0 comments on commit 0568a4f

Please sign in to comment.