Skip to content

Commit

Permalink
Set initial visibility state for AOSPLithoVisibilityEventsController
Browse files Browse the repository at this point in the history
Summary: Set initial visibility state for AOSPLithoVisibilityEventsController

Reviewed By: pentiumao

Differential Revision: D68955307

fbshipit-source-id: 0430692f1e683663b3af9589ad07bec23697ae59
  • Loading branch information
jettbow authored and facebook-github-bot committed Jan 31, 2025
1 parent a47418a commit f2eadd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.facebook.litho.LithoVisibilityEventsController.LithoVisibilityState
import com.facebook.litho.config.ComponentsConfiguration

/**
* This LithoVisibilityEventsController implementation dispatches to the registered observers the
Expand All @@ -32,7 +33,15 @@ open class AOSPLithoVisibilityEventsController(final override val lifecycleOwner
LithoVisibilityEventsController, LifecycleEventObserver, AOSPLifecycleOwnerProvider {

private val delegate: LithoVisibilityEventsControllerDelegate =
LithoVisibilityEventsControllerDelegate()
if (ComponentsConfiguration.defaultInstance
.enableInitStateForAOSPLithoVisibilityEventsController)
LithoVisibilityEventsControllerDelegate(
if (lifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED)
LithoVisibilityState.HINT_VISIBLE
else LithoVisibilityState.HINT_INVISIBLE)
else {
LithoVisibilityEventsControllerDelegate()
}

init {
lifecycleOwner.lifecycle.addObserver(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import java.util.ArrayList
* lifecycle state but delegate to this to handle the effects of the state change. See an example of
* how this facilitates a custom lifecycle implementation in [AOSPLithoVisibilityEventsController].
*/
class LithoVisibilityEventsControllerDelegate : LithoVisibilityEventsController {
class LithoVisibilityEventsControllerDelegate(
private var _visibilityState: LithoVisibilityState = LithoVisibilityState.HINT_VISIBLE
) : LithoVisibilityEventsController {

private val lithoVisibilityEventsListeners: MutableSet<LithoVisibilityEventsListener> = HashSet()
override var visibilityState: LithoVisibilityState = LithoVisibilityState.HINT_VISIBLE
private set

override val visibilityState: LithoVisibilityState
get() = _visibilityState

@IntDef(
LifecycleTransitionStatus.VALID,
Expand All @@ -50,20 +53,20 @@ class LithoVisibilityEventsControllerDelegate : LithoVisibilityEventsController
override fun moveToVisibilityState(newVisibilityState: LithoVisibilityState) {
ThreadUtils.assertMainThread()
if (newVisibilityState === LithoVisibilityState.DESTROYED &&
visibilityState === LithoVisibilityState.HINT_VISIBLE) {
_visibilityState === LithoVisibilityState.HINT_VISIBLE) {
moveToVisibilityState(LithoVisibilityState.HINT_INVISIBLE)
}
@LifecycleTransitionStatus
val transitionStatus = getTransitionState(visibilityState, newVisibilityState)
val transitionStatus = getTransitionState(_visibilityState, newVisibilityState)
if (transitionStatus == LifecycleTransitionStatus.INVALID) {
ComponentsReporter.emitMessage(
ComponentsReporter.LogLevel.WARNING,
"LithoVisibilityEventsController",
"Cannot move from state $visibilityState to state $newVisibilityState")
"Cannot move from state $_visibilityState to state $newVisibilityState")
return
}
if (transitionStatus == LifecycleTransitionStatus.VALID) {
visibilityState = newVisibilityState
_visibilityState = newVisibilityState
when (newVisibilityState) {
LithoVisibilityState.HINT_VISIBLE -> {
notifyOnResumeVisible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ internal constructor(
*/
@JvmField val debugEventListener: ComponentTreeDebugEventListener? = null,
@JvmField var enablePreAllocationSameThreadCheck: Boolean = false,

/** Lifecycle Owner in BaseMountingView and TreeProp */
@JvmField val enableSetLifecycleOwnerTreePropViaDefaultLifecycleOwner: Boolean = false,
@JvmField val enableDefaultLifecycleOwnerAsFragmentOrActivity: Boolean = false,

/** Visibility Events related */
@JvmField val enableCheckVisibilityAggregated: Boolean = false,
@JvmField val enableInitStateForAOSPLithoVisibilityEventsController: Boolean = false,
/**
* This is a temporary config param for debugging state list animator crashes during layout of a
* [ComponentHost]
Expand Down

0 comments on commit f2eadd9

Please sign in to comment.