Migrate VelocityHelper.java- to Kotlin (#45715)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45715

# Changelog:
[Internal] -

As in the title.

Reviewed By: tdn120

Differential Revision: D60283416

fbshipit-source-id: 62241bc3a7bcb0a4eb95b660a01233c269296621
This commit is contained in:
Ruslan Shestopalyuk
2024-07-27 12:34:34 -07:00
committed by Facebook GitHub Bot
parent e69b46c472
commit c07ca78ac6
2 changed files with 51 additions and 64 deletions
@@ -1,64 +0,0 @@
/*
* 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.view.MotionEvent;
import android.view.VelocityTracker;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
/**
* This Class helps to calculate the velocity for all ScrollView. The x and y velocity will later on
* send to ReactScrollViewHelper for further use.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
class VelocityHelper {
private @Nullable VelocityTracker mVelocityTracker;
private float mXVelocity;
private float mYVelocity;
/**
* Call from a ScrollView in onTouchEvent. Calculating the velocity for END_DRAG movement and send
* them back to react ScrollResponder.js
*/
public void calculateVelocity(MotionEvent ev) {
int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
{
// Calculate velocity on END_DRAG
mVelocityTracker.computeCurrentVelocity(1); // points/millisecond
mXVelocity = mVelocityTracker.getXVelocity();
mYVelocity = mVelocityTracker.getYVelocity();
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
}
}
/* Needs to call ACTION_UP/CANCEL to update the mXVelocity */
public float getXVelocity() {
return mXVelocity;
}
/* Needs to call ACTION_UP/CANCEL to update the mYVelocity */
public float getYVelocity() {
return mYVelocity;
}
}
@@ -0,0 +1,51 @@
/*
* 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.view.MotionEvent
import android.view.VelocityTracker
/**
* This Class helps to calculate the velocity for all ScrollView. The x and y velocity will later on
* send to ReactScrollViewHelper for further use.
*/
internal class VelocityHelper {
private var velocityTracker: VelocityTracker? = null
/* Needs to call ACTION_UP/CANCEL to update the xVelocity/yVelocity */
public var xVelocity = 0f
private set
public var yVelocity = 0f
private set
/**
* Call from a ScrollView in onTouchEvent. Calculating the velocity for END_DRAG movement and send
* them back to react ScrollResponder.js
*/
public fun calculateVelocity(ev: MotionEvent): Unit {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain()
}
velocityTracker?.let { tracker ->
tracker.addMovement(ev)
when (ev.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL -> {
// Calculate velocity on END_DRAG
tracker.computeCurrentVelocity(1) // points/millisecond
xVelocity = tracker.xVelocity
yVelocity = tracker.yVelocity
tracker.recycle()
velocityTracker = null
}
}
}
}
}