-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xplat/js/react-native-github/packages/react-native/ReactAndroid/src/m…
…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
1 parent
77dfa43
commit 95475ea
Showing
3 changed files
with
54 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
...ve/ReactAndroid/src/main/java/com/facebook/react/views/scroll/OnScrollDispatchHelper.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
...tive/ReactAndroid/src/main/java/com/facebook/react/views/scroll/OnScrollDispatchHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |