//xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast:toastAndroid (#44100)

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

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D56138594

fbshipit-source-id: a3010e4d99e5ba9948c7214d546461cbbf959851
This commit is contained in:
Andrew Datsenko
2024-04-15 14:09:52 -07:00
committed by Facebook GitHub Bot
parent e7154bdd9b
commit d53a2bd4a2
3 changed files with 86 additions and 99 deletions
@@ -3544,7 +3544,9 @@ public class com/facebook/react/modules/systeminfo/ReactNativeVersion {
public fun <init> ()V
}
public class com/facebook/react/modules/toast/ToastModule : com/facebook/fbreact/specs/NativeToastAndroidSpec {
public final class com/facebook/react/modules/toast/ToastModule : com/facebook/fbreact/specs/NativeToastAndroidSpec {
public static final field Companion Lcom/facebook/react/modules/toast/ToastModule$Companion;
public static final field NAME Ljava/lang/String;
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun getTypedExportedConstants ()Ljava/util/Map;
public fun show (Ljava/lang/String;D)V
@@ -3552,6 +3554,9 @@ public class com/facebook/react/modules/toast/ToastModule : com/facebook/fbreact
public fun showWithGravityAndOffset (Ljava/lang/String;DDDD)V
}
public final class com/facebook/react/modules/toast/ToastModule$Companion {
}
public final class com/facebook/react/modules/vibration/VibrationModule : com/facebook/fbreact/specs/NativeVibrationSpec {
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun cancel ()V
@@ -1,98 +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.modules.toast;
import android.view.Gravity;
import android.widget.Toast;
import com.facebook.fbreact.specs.NativeToastAndroidSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import java.util.Map;
/** {@link NativeModule} that allows JS to show an Android Toast. */
@ReactModule(name = NativeToastAndroidSpec.NAME)
public class ToastModule extends NativeToastAndroidSpec {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
private static final String GRAVITY_TOP_KEY = "TOP";
private static final String GRAVITY_BOTTOM_KEY = "BOTTOM";
private static final String GRAVITY_CENTER = "CENTER";
public ToastModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public Map<String, Object> getTypedExportedConstants() {
final Map<String, Object> constants = MapBuilder.newHashMap();
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
constants.put(GRAVITY_TOP_KEY, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
constants.put(GRAVITY_BOTTOM_KEY, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
constants.put(GRAVITY_CENTER, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
return constants;
}
@Override
public void show(final String message, final double durationDouble) {
final int duration = (int) durationDouble;
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
});
}
@Override
public void showWithGravity(
final String message, final double durationDouble, final double gravityDouble) {
final int duration = (int) durationDouble;
final int gravity = (int) gravityDouble;
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(getReactApplicationContext(), message, duration);
toast.setGravity(gravity, 0, 0);
toast.show();
}
});
}
@Override
public void showWithGravityAndOffset(
final String message,
final double durationDouble,
final double gravityDouble,
final double xOffsetDouble,
final double yOffsetDouble) {
final int duration = (int) durationDouble;
final int gravity = (int) gravityDouble;
final int xOffset = (int) xOffsetDouble;
final int yOffset = (int) yOffsetDouble;
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(getReactApplicationContext(), message, duration);
toast.setGravity(gravity, xOffset, yOffset);
toast.show();
}
});
}
}
@@ -0,0 +1,80 @@
/*
* 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.modules.toast
import android.view.Gravity
import android.widget.Toast
import com.facebook.fbreact.specs.NativeToastAndroidSpec
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.module.annotations.ReactModule
/** [NativeModule] that allows JS to show an Android Toast. */
@ReactModule(name = NativeToastAndroidSpec.NAME)
public class ToastModule(reactContext: ReactApplicationContext) :
NativeToastAndroidSpec(reactContext) {
override public fun getTypedExportedConstants(): Map<String, Any> =
mutableMapOf(
DURATION_SHORT_KEY to Toast.LENGTH_SHORT,
DURATION_LONG_KEY to Toast.LENGTH_LONG,
GRAVITY_TOP_KEY to (Gravity.TOP or Gravity.CENTER_HORIZONTAL),
GRAVITY_BOTTOM_KEY to (Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL),
GRAVITY_CENTER to (Gravity.CENTER_HORIZONTAL or Gravity.CENTER_VERTICAL),
)
override public fun show(message: String?, durationDouble: Double) {
val duration = durationDouble.toInt()
UiThreadUtil.runOnUiThread(
Runnable { Toast.makeText(getReactApplicationContext(), message, duration).show() })
}
override public fun showWithGravity(
message: String?,
durationDouble: Double,
gravityDouble: Double
) {
val duration = durationDouble.toInt()
val gravity = gravityDouble.toInt()
UiThreadUtil.runOnUiThread(
Runnable {
val toast = Toast.makeText(getReactApplicationContext(), message, duration)
toast.setGravity(gravity, 0, 0)
toast.show()
})
}
override public fun showWithGravityAndOffset(
message: String?,
durationDouble: Double,
gravityDouble: Double,
xOffsetDouble: Double,
yOffsetDouble: Double
) {
val duration = durationDouble.toInt()
val gravity = gravityDouble.toInt()
val xOffset = xOffsetDouble.toInt()
val yOffset = yOffsetDouble.toInt()
UiThreadUtil.runOnUiThread(
Runnable {
val toast = Toast.makeText(getReactApplicationContext(), message, duration)
toast.setGravity(gravity, xOffset, yOffset)
toast.show()
})
}
public companion object {
public const val NAME: String = NativeToastAndroidSpec.NAME
private const val DURATION_SHORT_KEY = "SHORT"
private const val DURATION_LONG_KEY = "LONG"
private const val GRAVITY_TOP_KEY = "TOP"
private const val GRAVITY_BOTTOM_KEY = "BOTTOM"
private const val GRAVITY_CENTER = "CENTER"
}
}