diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index 60a5be970d4547..5fa72a0ffd02b0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -9,7 +9,9 @@ import androidx.annotation.Nullable; import com.facebook.react.TurboReactPackage; +import com.facebook.react.ViewManagerOnDemandReactPackage; import com.facebook.react.animated.NativeAnimatedModule; +import com.facebook.react.bridge.ModuleSpec; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.module.annotations.ReactModule; @@ -38,6 +40,7 @@ import com.facebook.react.modules.vibration.VibrationModule; import com.facebook.react.modules.websocket.WebSocketModule; import com.facebook.react.turbomodule.core.interfaces.TurboModule; +import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.ViewManager; import com.facebook.react.views.drawer.ReactDrawerLayoutManager; import com.facebook.react.views.image.ReactImageManager; @@ -56,9 +59,11 @@ import com.facebook.react.views.unimplementedview.ReactUnimplementedViewManager; import com.facebook.react.views.view.ReactViewManager; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.inject.Provider; /** Package defining basic modules and view managers. */ @ReactModuleList( @@ -85,9 +90,10 @@ VibrationModule.class, WebSocketModule.class, }) -public class MainReactPackage extends TurboReactPackage { +public class MainReactPackage extends TurboReactPackage implements ViewManagerOnDemandReactPackage { private MainPackageConfig mConfig; + private @Nullable Map mViewManagers; public MainReactPackage() {} @@ -175,6 +181,181 @@ public List createViewManagers(ReactApplicationContext reactContext return viewManagers; } + private static void appendMap( + Map map, String name, Provider provider) { + map.put(name, ModuleSpec.viewManagerSpec(provider)); + } + + /** @return a map of view managers that should be registered with {@link UIManagerModule} */ + public Map getViewManagersMap(final ReactApplicationContext reactContext) { + if (mViewManagers == null) { + Map viewManagers = new HashMap<>(); + appendMap( + viewManagers, + ReactDrawerLayoutManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactDrawerLayoutManager(); + } + }); + appendMap( + viewManagers, + ReactHorizontalScrollViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactHorizontalScrollViewManager(); + } + }); + appendMap( + viewManagers, + ReactHorizontalScrollContainerViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactHorizontalScrollContainerViewManager(); + } + }); + appendMap( + viewManagers, + ReactProgressBarViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactProgressBarViewManager(); + } + }); + appendMap( + viewManagers, + ReactScrollViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactScrollViewManager(); + } + }); + appendMap( + viewManagers, + ReactSwitchManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactSwitchManager(); + } + }); + appendMap( + viewManagers, + SwipeRefreshLayoutManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new SwipeRefreshLayoutManager(); + } + }); + appendMap( + viewManagers, + FrescoBasedReactTextInlineImageViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new FrescoBasedReactTextInlineImageViewManager(); + } + }); + appendMap( + viewManagers, + ReactImageManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactImageManager(); + } + }); + appendMap( + viewManagers, + ReactModalHostManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactModalHostManager(); + } + }); + appendMap( + viewManagers, + ReactRawTextManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactRawTextManager(); + } + }); + appendMap( + viewManagers, + ReactTextInputManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactTextInputManager(); + } + }); + appendMap( + viewManagers, + ReactTextViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactTextViewManager(); + } + }); + appendMap( + viewManagers, + ReactViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactViewManager(); + } + }); + appendMap( + viewManagers, + ReactVirtualTextViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactVirtualTextViewManager(); + } + }); + appendMap( + viewManagers, + ReactUnimplementedViewManager.REACT_CLASS, + new Provider() { + @Override + public NativeModule get() { + return new ReactUnimplementedViewManager(); + } + }); + mViewManagers = viewManagers; + } + return mViewManagers; + } + + @Override + public List getViewManagers(ReactApplicationContext reactContext) { + return new ArrayList<>(getViewManagersMap(reactContext).values()); + } + + @Override + public Collection getViewManagerNames(ReactApplicationContext reactContext) { + return getViewManagersMap(reactContext).keySet(); + } + + @Override + public @Nullable ViewManager createViewManager( + ReactApplicationContext reactContext, String viewManagerName) { + ModuleSpec spec = getViewManagersMap(reactContext).get(viewManagerName); + return spec != null ? (ViewManager) spec.getProvider().get() : null; + } + @Override public ReactModuleInfoProvider getReactModuleInfoProvider() { try {