Skip to content

Commit

Permalink
xplat/js/react-native-github/packages/react-native/ReactAndroid/src/m…
Browse files Browse the repository at this point in the history
…ain/java/com/facebook/react/views/scroll/OnScrollDispatchHelper.java (#45676)

Summary:
Pull Request resolved: #45676

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D60233460

fbshipit-source-id: 7524fe461846466118e7037f01ac828535533d17
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Jul 26, 2024
1 parent 77dfa43 commit 95475ea
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 62 deletions.
8 changes: 4 additions & 4 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6646,11 +6646,11 @@ public abstract interface class com/facebook/react/views/scroll/FpsListener {
public abstract fun isEnabled ()Z
}

public class com/facebook/react/views/scroll/OnScrollDispatchHelper {
public final class com/facebook/react/views/scroll/OnScrollDispatchHelper {
public fun <init> ()V
public fun getXFlingVelocity ()F
public fun getYFlingVelocity ()F
public fun onScrollChanged (II)Z
public final fun getXFlingVelocity ()F
public final fun getYFlingVelocity ()F
public final fun onScrollChanged (II)Z
}

public class com/facebook/react/views/scroll/ReactHorizontalScrollContainerView : com/facebook/react/views/view/ReactViewGroup {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.scroll

import android.os.SystemClock

/**
* Android has a bug where onScrollChanged is called twice per frame with the same params during
* flings. We hack around that here by trying to detect that duplicate call and not dispatch it. See
* https://code.google.com/p/android/issues/detail?id=39473
*/
public class OnScrollDispatchHelper {

private var prevX = Int.MIN_VALUE
private var prevY = Int.MIN_VALUE
public var xFlingVelocity: Float = 0f
private set

public var yFlingVelocity: Float = 0f
private set

private var lastScrollEventTimeMs = -(MIN_EVENT_SEPARATION_MS + 1).toLong()

/**
* Call from a ScrollView in onScrollChanged, returns true if this onScrollChanged is legit (not a
* duplicate) and should be dispatched.
*/
public fun onScrollChanged(x: Int, y: Int): Boolean {
val eventTime = SystemClock.uptimeMillis()
val shouldDispatch =
eventTime - lastScrollEventTimeMs > MIN_EVENT_SEPARATION_MS || prevX != x || prevY != y
if (eventTime - lastScrollEventTimeMs != 0L) {
xFlingVelocity = (x - prevX).toFloat() / (eventTime - lastScrollEventTimeMs)
yFlingVelocity = (y - prevY).toFloat() / (eventTime - lastScrollEventTimeMs)
}
lastScrollEventTimeMs = eventTime
prevX = x
prevY = y
return shouldDispatch
}

private companion object {
private const val MIN_EVENT_SEPARATION_MS = 10
}
}

0 comments on commit 95475ea

Please sign in to comment.