Change Litho observer from on scroll changed listener to on draw listener

Summary:
There are some situatuins where a litho view can move  without a mount occuring in that view. One situation is a litho view in a recycler view. If a neighbouring view is deleted or changes its width/height this will shift the whole recycler view. Since each view in the recycler view is typically a separate comonent tree. The children of the recycler view are not aware of their siblings changes through mount. And these situations do not count as a scroll which was the previous method of detecting change.

This is a work around to listen to on draw which seems to be fired in all situations.

Reviewed By: lblasa

Differential Revision: D40430777

fbshipit-source-id: a9c8196f31a6bdfd4a2fed398cfcaed190972959
This commit is contained in:
Luke De Feo
2022-10-25 07:10:38 -07:00
committed by Facebook GitHub Bot
parent 1aacc51d12
commit a447712865

View File

@@ -45,8 +45,7 @@ class LithoViewTreeObserver(val context: Context) : TreeObserver<LithoView>() {
private val waitScope = CoroutineScope(Dispatchers.IO) private val waitScope = CoroutineScope(Dispatchers.IO)
private val mainScope = CoroutineScope(Dispatchers.Main) private val mainScope = CoroutineScope(Dispatchers.Main)
var nodeRef: LithoView? = null var nodeRef: LithoView? = null
var onScrollChangedListener: ViewTreeObserver.OnScrollChangedListener? = null private var preDrawListener: ViewTreeObserver.OnPreDrawListener? = null
override fun subscribe(node: Any) { override fun subscribe(node: Any) {
Log.d(LogTag, "Subscribing to litho view ${node.nodeId()}") Log.d(LogTag, "Subscribing to litho view ${node.nodeId()}")
@@ -62,8 +61,13 @@ class LithoViewTreeObserver(val context: Context) : TreeObserver<LithoView>() {
processUpdate(context, node) processUpdate(context, node)
} }
onScrollChangedListener = ViewTreeObserver.OnScrollChangedListener({ throttledUpdate(node) }) preDrawListener =
node.viewTreeObserver.addOnScrollChangedListener(onScrollChangedListener) ViewTreeObserver.OnPreDrawListener {
throttledUpdate(node)
true
}
node.viewTreeObserver.addOnPreDrawListener(preDrawListener)
// we have already missed the first mount so we trigger it manually on subscribe // we have already missed the first mount so we trigger it manually on subscribe
processUpdate(context, node) processUpdate(context, node)
@@ -71,7 +75,7 @@ class LithoViewTreeObserver(val context: Context) : TreeObserver<LithoView>() {
override fun unsubscribe() { override fun unsubscribe() {
Log.d(LogTag, "Unsubscribing from litho view ${nodeRef?.nodeId()}") Log.d(LogTag, "Unsubscribing from litho view ${nodeRef?.nodeId()}")
nodeRef?.viewTreeObserver?.removeOnScrollChangedListener(onScrollChangedListener) nodeRef?.viewTreeObserver?.removeOnPreDrawListener(preDrawListener)
nodeRef?.unregisterUIDebugger() nodeRef?.unregisterUIDebugger()
nodeRef = null nodeRef = null
} }