Add setNativeValue command to ReactSwitchManager

Summary: Fabric doesn't support setNativeProps, so we have to use commands instead to set the value of the native component.

Reviewed By: JoshuaGross

Differential Revision: D17736274

fbshipit-source-id: 18c47365926c3c2cfc3551f4b5b6cc72e4162367
This commit is contained in:
Oleksandr Melnykov
2019-10-07 03:54:09 -07:00
committed by Facebook Github Bot
parent 6166645560
commit 3560093115
4 changed files with 54 additions and 7 deletions
@@ -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<T extends View, U extends BaseViewMana
super.setProperty(view, propName, value);
}
}
public void receiveCommand(AndroidSwitchManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) {
switch (commandName) {
case "setNativeValue":
viewManager.setNativeValue(view, args.getBoolean(0));
break;
}
}
}
@@ -22,4 +22,5 @@ public interface AndroidSwitchManagerInterface<T extends View> {
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);
}
@@ -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<ReactSwitch>
@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<ReactSwitch>
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<ReactSwitch>
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);
}
}