From c07ca78ac66b9319a2b652cc34b9424429934d4d Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Sat, 27 Jul 2024 12:34:34 -0700 Subject: [PATCH] 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 --- .../react/views/scroll/VelocityHelper.java | 64 ------------------- .../react/views/scroll/VelocityHelper.kt | 51 +++++++++++++++ 2 files changed, 51 insertions(+), 64 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.java deleted file mode 100644 index 7887c0b4023..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.java +++ /dev/null @@ -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; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.kt new file mode 100644 index 00000000000..57d6c165f89 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/VelocityHelper.kt @@ -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 + } + } + } + } +}