diff --git a/Libraries/Components/Switch/AndroidSwitchNativeComponent.js b/Libraries/Components/Switch/AndroidSwitchNativeComponent.js index 5634aa4199d..92d73f7b115 100644 --- a/Libraries/Components/Switch/AndroidSwitchNativeComponent.js +++ b/Libraries/Components/Switch/AndroidSwitchNativeComponent.js @@ -10,11 +10,14 @@ 'use strict'; +import * as React from 'react'; + import type { WithDefault, BubblingEventHandler, } from 'react-native/Libraries/Types/CodegenTypes'; +import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands'; import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; import type {HostComponent} from 'react-native/Libraries/Renderer/shims/ReactNativeTypes'; @@ -43,6 +46,19 @@ type NativeProps = $ReadOnly<{| onChange?: BubblingEventHandler, |}>; +type NativeType = HostComponent; + +interface NativeCommands { + +setNativeValue: ( + viewRef: React.ElementRef, + value: boolean, + ) => void; +} + +export const Commands: NativeCommands = codegenNativeCommands({ + supportedCommands: ['setNativeValue'], +}); + export default (codegenNativeComponent('AndroidSwitch', { interfaceOnly: true, -}): HostComponent); +}): NativeType); diff --git a/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerDelegate.java index e3e6a6fa15a..b801a9cdab2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerDelegate.java @@ -11,6 +11,7 @@ package com.facebook.react.viewmanagers; import android.view.View; import androidx.annotation.Nullable; +import com.facebook.react.bridge.ReadableArray; import com.facebook.react.uimanager.BaseViewManagerDelegate; import com.facebook.react.uimanager.BaseViewManagerInterface; import com.facebook.react.uimanager.LayoutShadowNode; @@ -53,4 +54,12 @@ public class AndroidSwitchManagerDelegate viewManager, T view, String commandName, ReadableArray args) { + switch (commandName) { + case "setNativeValue": + viewManager.setNativeValue(view, args.getBoolean(0)); + break; + } + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerInterface.java b/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerInterface.java index 565c9ead11d..2ff293b3be6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerInterface.java +++ b/ReactAndroid/src/main/java/com/facebook/react/viewmanagers/AndroidSwitchManagerInterface.java @@ -22,4 +22,5 @@ public interface AndroidSwitchManagerInterface { void setOn(T view, boolean value); void setThumbTintColor(T view, @Nullable Integer value); void setTrackTintColor(T view, @Nullable Integer value); + void setNativeValue(T view, boolean value); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java index 45a43900029..57969afb589 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java @@ -11,8 +11,10 @@ package com.facebook.react.views.switchview; import android.content.Context; import android.view.View; import android.widget.CompoundButton; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.uimanager.LayoutShadowNode; import com.facebook.react.uimanager.PixelUtil; @@ -128,17 +130,13 @@ public class ReactSwitchManager extends SimpleViewManager @Override @ReactProp(name = ViewProps.ON) public void setOn(ReactSwitch view, boolean on) { - this.setValue(view, on); + setValueInternal(view, on); } @Override @ReactProp(name = "value") public void setValue(ReactSwitch view, boolean value) { - // we set the checked change listener to null and then restore it so that we don't fire an - // onChange event to JS when JS itself is updating the value of the switch - view.setOnCheckedChangeListener(null); - view.setOn(value); - view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER); + setValueInternal(view, value); } @Override @@ -171,6 +169,21 @@ public class ReactSwitchManager extends SimpleViewManager view.setTrackColor(color); } + @Override + public void setNativeValue(ReactSwitch view, boolean value) { + // TODO(T52835863): Implement when view commands start using delegates generated by JS. + } + + @Override + public void receiveCommand( + @NonNull ReactSwitch view, String commandId, @Nullable ReadableArray args) { + switch (commandId) { + case "setNativeValue": + setValueInternal(view, args != null && args.getBoolean(0)); + break; + } + } + @Override protected void addEventEmitters(final ThemedReactContext reactContext, final ReactSwitch view) { view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER); @@ -199,4 +212,12 @@ public class ReactSwitchManager extends SimpleViewManager PixelUtil.toDIPFromPixel(view.getMeasuredWidth()), PixelUtil.toDIPFromPixel(view.getMeasuredHeight())); } + + private static void setValueInternal(ReactSwitch view, boolean value) { + // we set the checked change listener to null and then restore it so that we don't fire an + // onChange event to JS when JS itself is updating the value of the switch + view.setOnCheckedChangeListener(null); + view.setOn(value); + view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER); + } }