diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 6495a03e550..9f88aba31a7 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6921,14 +6921,21 @@ public class com/facebook/react/views/scroll/ReactScrollView : android/widget/Sc public fun updateClippingRect ()V } -public class com/facebook/react/views/scroll/ReactScrollViewCommandHelper { +public final class com/facebook/react/views/scroll/ReactScrollViewCommandHelper { public static final field COMMAND_FLASH_SCROLL_INDICATORS I public static final field COMMAND_SCROLL_TO I public static final field COMMAND_SCROLL_TO_END I + public static final field Companion Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$Companion; public fun ()V - public static fun getCommandsMap ()Ljava/util/Map; - public static fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;ILcom/facebook/react/bridge/ReadableArray;)V - public static fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V + public static final fun getCommandsMap ()Ljava/util/Map; + public static final fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;ILcom/facebook/react/bridge/ReadableArray;)V + public static final fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V +} + +public final class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$Companion { + public final fun getCommandsMap ()Ljava/util/Map; + public final fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;ILcom/facebook/react/bridge/ReadableArray;)V + public final fun receiveCommand (Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler;Ljava/lang/Object;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V } public abstract interface class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollCommandHandler { @@ -6937,14 +6944,16 @@ public abstract interface class com/facebook/react/views/scroll/ReactScrollViewC public abstract fun scrollToEnd (Ljava/lang/Object;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData;)V } -public class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToCommandData { +public final class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToCommandData { public final field mAnimated Z public final field mDestX I public final field mDestY I + public fun (IIZ)V } -public class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData { +public final class com/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData { public final field mAnimated Z + public fun (Z)V } public final class com/facebook/react/views/scroll/ReactScrollViewHelper { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java deleted file mode 100644 index f2e67e66770..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java +++ /dev/null @@ -1,141 +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 androidx.annotation.Nullable; -import com.facebook.infer.annotation.Assertions; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.common.MapBuilder; -import com.facebook.react.uimanager.PixelUtil; -import java.util.Map; - -/** - * Helper for view managers to handle commands like 'scrollTo'. Shared by {@link - * ReactScrollViewManager} and {@link ReactHorizontalScrollViewManager}. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class ReactScrollViewCommandHelper { - - public static final int COMMAND_SCROLL_TO = 1; - public static final int COMMAND_SCROLL_TO_END = 2; - public static final int COMMAND_FLASH_SCROLL_INDICATORS = 3; - - public interface ScrollCommandHandler { - void scrollTo(T scrollView, ScrollToCommandData data); - - void scrollToEnd(T scrollView, ScrollToEndCommandData data); - - void flashScrollIndicators(T scrollView); - } - - public static class ScrollToCommandData { - - public final int mDestX, mDestY; - public final boolean mAnimated; - - ScrollToCommandData(int destX, int destY, boolean animated) { - mDestX = destX; - mDestY = destY; - mAnimated = animated; - } - } - - public static class ScrollToEndCommandData { - - public final boolean mAnimated; - - ScrollToEndCommandData(boolean animated) { - mAnimated = animated; - } - } - - public static Map getCommandsMap() { - return MapBuilder.of( - "scrollTo", - COMMAND_SCROLL_TO, - "scrollToEnd", - COMMAND_SCROLL_TO_END, - "flashScrollIndicators", - COMMAND_FLASH_SCROLL_INDICATORS); - } - - public static void receiveCommand( - ScrollCommandHandler viewManager, - T scrollView, - int commandType, - @Nullable ReadableArray args) { - Assertions.assertNotNull(viewManager); - Assertions.assertNotNull(scrollView); - switch (commandType) { - case COMMAND_SCROLL_TO: - { - scrollTo(viewManager, scrollView, Assertions.assertNotNull(args)); - return; - } - case COMMAND_SCROLL_TO_END: - { - scrollToEnd(viewManager, scrollView, Assertions.assertNotNull(args)); - return; - } - case COMMAND_FLASH_SCROLL_INDICATORS: - viewManager.flashScrollIndicators(scrollView); - return; - - default: - throw new IllegalArgumentException( - String.format( - "Unsupported command %d received by %s.", - commandType, viewManager.getClass().getSimpleName())); - } - } - - public static void receiveCommand( - ScrollCommandHandler viewManager, - T scrollView, - String commandType, - @Nullable ReadableArray args) { - Assertions.assertNotNull(viewManager); - Assertions.assertNotNull(scrollView); - switch (commandType) { - case "scrollTo": - { - scrollTo(viewManager, scrollView, Assertions.assertNotNull(args)); - return; - } - case "scrollToEnd": - { - scrollToEnd(viewManager, scrollView, Assertions.assertNotNull(args)); - return; - } - case "flashScrollIndicators": - viewManager.flashScrollIndicators(scrollView); - return; - - default: - throw new IllegalArgumentException( - String.format( - "Unsupported command %s received by %s.", - commandType, viewManager.getClass().getSimpleName())); - } - } - - private static void scrollTo( - ScrollCommandHandler viewManager, T scrollView, ReadableArray args) { - int destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0))); - int destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1))); - boolean animated = args.getBoolean(2); - viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY, animated)); - } - - private static void scrollToEnd( - ScrollCommandHandler viewManager, T scrollView, ReadableArray args) { - boolean animated = args.getBoolean(0); - viewManager.scrollToEnd(scrollView, new ScrollToEndCommandData(animated)); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.kt new file mode 100644 index 00000000000..313b74beba2 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.kt @@ -0,0 +1,101 @@ +/* + * 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 com.facebook.react.bridge.ReadableArray +import com.facebook.react.uimanager.PixelUtil + +public class ReactScrollViewCommandHelper { + + public companion object { + public const val COMMAND_SCROLL_TO: Int = 1 + public const val COMMAND_SCROLL_TO_END: Int = 2 + public const val COMMAND_FLASH_SCROLL_INDICATORS: Int = 3 + + @JvmStatic + public fun getCommandsMap(): Map = + hashMapOf( + "scrollTo" to COMMAND_SCROLL_TO, + "scrollToEnd" to COMMAND_SCROLL_TO_END, + "flashScrollIndicators" to COMMAND_FLASH_SCROLL_INDICATORS) + + @JvmStatic + public fun receiveCommand( + viewManager: ScrollCommandHandler, + scrollView: T, + commandType: Int, + args: ReadableArray? + ) { + checkNotNull(viewManager) + checkNotNull(scrollView) + when (commandType) { + COMMAND_SCROLL_TO -> scrollTo(viewManager, scrollView, checkNotNull(args)) + COMMAND_SCROLL_TO_END -> scrollToEnd(viewManager, scrollView, checkNotNull(args)) + COMMAND_FLASH_SCROLL_INDICATORS -> viewManager.flashScrollIndicators(scrollView) + else -> + throw IllegalArgumentException( + "Unsupported command $commandType received by ${viewManager::class.java.simpleName}.") + } + } + + @JvmStatic + public fun receiveCommand( + viewManager: ScrollCommandHandler, + scrollView: T, + commandType: String, + args: ReadableArray? + ) { + checkNotNull(viewManager) + checkNotNull(scrollView) + when (commandType) { + "scrollTo" -> scrollTo(viewManager, scrollView, checkNotNull(args)) + "scrollToEnd" -> scrollToEnd(viewManager, scrollView, checkNotNull(args)) + "flashScrollIndicators" -> viewManager.flashScrollIndicators(scrollView) + else -> + throw IllegalArgumentException( + "Unsupported command $commandType received by ${viewManager::class.java.simpleName}.") + } + } + + private fun scrollTo( + viewManager: ScrollCommandHandler, + scrollView: T, + args: ReadableArray + ) { + val destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0))) + val destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1))) + val animated = args.getBoolean(2) + viewManager.scrollTo(scrollView, ScrollToCommandData(destX, destY, animated)) + } + + private fun scrollToEnd( + viewManager: ScrollCommandHandler, + scrollView: T, + args: ReadableArray + ) { + val animated = args.getBoolean(0) + viewManager.scrollToEnd(scrollView, ScrollToEndCommandData(animated)) + } + } + + public interface ScrollCommandHandler { + public fun scrollTo(scrollView: T, data: ScrollToCommandData) + + public fun scrollToEnd(scrollView: T, data: ScrollToEndCommandData) + + public fun flashScrollIndicators(scrollView: T) + } + + public class ScrollToCommandData( + @JvmField public val mDestX: Int, + @JvmField public val mDestY: Int, + @JvmField public val mAnimated: Boolean + ) + + public class ScrollToEndCommandData(@JvmField public val mAnimated: Boolean) +}