Migrate Android view managers to type-safe commands generated by JS codegen

Summary:
## Changelog:

[General] [Changed] - Migrate Android view managers to type-safe commands generated by JS codegen.

Reviewed By: JoshuaGross, mdvacca

Differential Revision: D21406461

fbshipit-source-id: 93584b240314254675a36a58c4d0c0880d6889fb
This commit is contained in:
Lulu Wu 2020-05-12 04:35:34 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 8f90ce26a5
Коммит 63099c40e6
14 изменённых файлов: 55 добавлений и 27 удалений

Просмотреть файл

@ -38,6 +38,13 @@ public class ReactFeatureFlags {
*/ */
public static boolean useViewManagerDelegates = false; public static boolean useViewManagerDelegates = false;
/**
* Should this application use a {@link com.facebook.react.uimanager.ViewManagerDelegate} (if
* provided) to execute the view commands. If {@code false}, then {@code receiveCommand} method
* inside view manager will be called instead.
*/
public static boolean useViewManagerDelegatesForCommands = false;
/** /**
* Should this application use Catalyst Teardown V2? This is an experiment to use a V2 of the * Should this application use Catalyst Teardown V2? This is an experiment to use a V2 of the
* CatalystInstanceImpl `destroy` method. * CatalystInstanceImpl `destroy` method.

Просмотреть файл

@ -113,4 +113,7 @@ public abstract class BaseViewManagerDelegate<T extends View, U extends BaseView
break; break;
} }
} }
@Override
public void receiveCommand(T view, String commandName, ReadableArray args) {}
} }

Просмотреть файл

@ -29,6 +29,7 @@ import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.RetryableMountingLayerException; import com.facebook.react.bridge.RetryableMountingLayerException;
import com.facebook.react.bridge.SoftAssertions; import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.touch.JSResponderHandler; import com.facebook.react.touch.JSResponderHandler;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationController; import com.facebook.react.uimanager.layoutanimation.LayoutAnimationController;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationListener; import com.facebook.react.uimanager.layoutanimation.LayoutAnimationListener;
@ -784,7 +785,13 @@ public class NativeViewHierarchyManager {
+ commandId); + commandId);
} }
ViewManager viewManager = resolveViewManager(reactTag); ViewManager viewManager = resolveViewManager(reactTag);
viewManager.receiveCommand(view, commandId, args); ViewManagerDelegate delegate;
if (ReactFeatureFlags.useViewManagerDelegatesForCommands
&& (delegate = viewManager.getDelegate()) != null) {
delegate.receiveCommand(view, commandId, args);
} else {
viewManager.receiveCommand(view, commandId, args);
}
} }
/** /**

Просмотреть файл

@ -9,13 +9,17 @@ package com.facebook.react.uimanager;
import android.view.View; import android.view.View;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReadableArray;
/** /**
* This is an interface that must be implemented by classes that wish to take over the * This is an interface that must be implemented by classes that wish to take over the
* responsibility of setting properties of all views managed by the view manager. * responsibility of setting properties of all views managed by the view manager and executing view
* commands.
* *
* @param <T> the type of the view supported by this delegate * @param <T> the type of the view supported by this delegate
*/ */
public interface ViewManagerDelegate<T extends View> { public interface ViewManagerDelegate<T extends View> {
void setProperty(T view, String propName, @Nullable Object value); void setProperty(T view, String propName, @Nullable Object value);
void receiveCommand(T view, String commandName, ReadableArray args);
} }

Просмотреть файл

@ -47,13 +47,14 @@ public class AndroidDrawerLayoutManagerDelegate<T extends View, U extends BaseVi
} }
} }
public void receiveCommand(AndroidDrawerLayoutManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case "openDrawer": case "openDrawer":
viewManager.openDrawer(view); mViewManager.openDrawer(view);
break; break;
case "closeDrawer": case "closeDrawer":
viewManager.closeDrawer(view); mViewManager.closeDrawer(view);
break; break;
} }
} }

Просмотреть файл

@ -47,10 +47,11 @@ public class AndroidSwipeRefreshLayoutManagerDelegate<T extends View, U extends
} }
} }
public void receiveCommand(AndroidSwipeRefreshLayoutManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case "setNativeRefreshing": case "setNativeRefreshing":
viewManager.setNativeRefreshing(view, args.getBoolean(0)); mViewManager.setNativeRefreshing(view, args.getBoolean(0));
break; break;
} }
} }

Просмотреть файл

@ -56,10 +56,11 @@ public class AndroidSwitchManagerDelegate<T extends View, U extends BaseViewMana
} }
} }
public void receiveCommand(AndroidSwitchManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case "setNativeValue": case "setNativeValue":
viewManager.setNativeValue(view, args.getBoolean(0)); mViewManager.setNativeValue(view, args.getBoolean(0));
break; break;
} }
} }

Просмотреть файл

@ -43,13 +43,14 @@ public class AndroidViewPagerManagerDelegate<T extends View, U extends BaseViewM
} }
} }
public void receiveCommand(AndroidViewPagerManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case "setPage": case "setPage":
viewManager.setPage(view, args.getInt(0)); mViewManager.setPage(view, args.getInt(0));
break; break;
case "setPageWithoutAnimation": case "setPageWithoutAnimation":
viewManager.setPageWithoutAnimation(view, args.getInt(0)); mViewManager.setPageWithoutAnimation(view, args.getInt(0));
break; break;
} }
} }

Просмотреть файл

@ -15,7 +15,6 @@ import com.facebook.react.bridge.ColorPropConverter;
import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.BaseViewManagerDelegate; import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface; import com.facebook.react.uimanager.BaseViewManagerInterface;
import com.facebook.react.uimanager.LayoutShadowNode;
public class SwitchManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & SwitchManagerInterface<T>> extends BaseViewManagerDelegate<T, U> { public class SwitchManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & SwitchManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public SwitchManagerDelegate(U viewManager) { public SwitchManagerDelegate(U viewManager) {
@ -53,10 +52,11 @@ public class SwitchManagerDelegate<T extends View, U extends BaseViewManagerInte
} }
} }
public void receiveCommand(SwitchManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case "setValue": case "setValue":
viewManager.setValue(view, args.getBoolean(0)); mViewManager.setValue(view, args.getBoolean(0));
break; break;
} }
} }

Просмотреть файл

@ -131,7 +131,7 @@ public class SwipeRefreshLayoutManager extends ViewGroupManager<ReactSwipeRefres
@Override @Override
public void setNativeRefreshing(ReactSwipeRefreshLayout view, boolean value) { public void setNativeRefreshing(ReactSwipeRefreshLayout view, boolean value) {
// TODO(T52835863): Implement when view commands start using delegates generated by JS. setRefreshing(view, value);
} }
@Override @Override

Просмотреть файл

@ -177,7 +177,7 @@ public class ReactSwitchManager extends SimpleViewManager<ReactSwitch>
@Override @Override
public void setNativeValue(ReactSwitch view, boolean value) { public void setNativeValue(ReactSwitch view, boolean value) {
// TODO(T52835863): Implement when view commands start using delegates generated by JS. setValueInternal(view, value);
} }
@Override @Override

Просмотреть файл

@ -160,12 +160,12 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager>
@Override @Override
public void setPage(ReactViewPager view, int page) { public void setPage(ReactViewPager view, int page) {
// TODO(T52835863): Implement when view commands start using delegates generated by JS. view.setCurrentItemFromJs(page, true);
} }
@Override @Override
public void setPageWithoutAnimation(ReactViewPager view, int page) { public void setPageWithoutAnimation(ReactViewPager view, int page) {
// TODO(T52835863): Implement when view commands start using delegates generated by JS. view.setCurrentItemFromJs(page, false);
} }
@Override @Override

Просмотреть файл

@ -55,7 +55,8 @@ const propSetterTemplate = `
`; `;
const commandsTemplate = ` const commandsTemplate = `
public void receiveCommand(::_INTERFACE_CLASSNAME_::<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
::_COMMAND_CASES_:: ::_COMMAND_CASES_::
} }
@ -198,7 +199,7 @@ function generateCommandCasesString(
const commandMethods = component.commands const commandMethods = component.commands
.map(command => { .map(command => {
return `case "${command.name}": return `case "${command.name}":
viewManager.${toSafeJavaString( mViewManager.${toSafeJavaString(
command.name, command.name,
false, false,
)}(${getCommandArguments(command)}); )}(${getCommandArguments(command)});

Просмотреть файл

@ -214,13 +214,14 @@ public class CommandNativeComponentManagerDelegate<T extends View, U extends Bas
super.setProperty(view, propName, value); super.setProperty(view, propName, value);
} }
public void receiveCommand(CommandNativeComponentManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case \\"flashScrollIndicators\\": case \\"flashScrollIndicators\\":
viewManager.flashScrollIndicators(view); mViewManager.flashScrollIndicators(view);
break; break;
case \\"allTypes\\": case \\"allTypes\\":
viewManager.allTypes(view, args.getInt(0), (float) args.getDouble(1), args.getDouble(2), args.getString(3), args.getBoolean(4)); mViewManager.allTypes(view, args.getInt(0), (float) args.getDouble(1), args.getDouble(2), args.getString(3), args.getBoolean(4));
break; break;
} }
} }
@ -264,13 +265,14 @@ public class CommandNativeComponentManagerDelegate<T extends View, U extends Bas
} }
} }
public void receiveCommand(CommandNativeComponentManagerInterface<T> viewManager, T view, String commandName, ReadableArray args) { @Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) { switch (commandName) {
case \\"handleRootTag\\": case \\"handleRootTag\\":
viewManager.handleRootTag(view, args.getDouble(0)); mViewManager.handleRootTag(view, args.getDouble(0));
break; break;
case \\"hotspotUpdate\\": case \\"hotspotUpdate\\":
viewManager.hotspotUpdate(view, args.getInt(0), args.getInt(1)); mViewManager.hotspotUpdate(view, args.getInt(0), args.getInt(1));
break; break;
} }
} }