Skip to content

Commit

Permalink
Migrate packages to not eager initialize view managers
Browse files Browse the repository at this point in the history
Summary:
Migrate packages need for Catalyst app to use lazy view manager initialisation

Changelog:
[Android][Changed] - Migrate packages to not eager initialize view managers

Reviewed By: javache

Differential Revision: D43711708

fbshipit-source-id: fd172fc664d68fb7ee1d5a980516cdf48e30a979
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Mar 6, 2023
1 parent d2dd79f commit d7eb3bf
Showing 1 changed file with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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<String, ModuleSpec> mViewManagers;

public MainReactPackage() {}

Expand Down Expand Up @@ -175,6 +181,181 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
return viewManagers;
}

private static void appendMap(
Map<String, ModuleSpec> map, String name, Provider<? extends NativeModule> provider) {
map.put(name, ModuleSpec.viewManagerSpec(provider));
}

/** @return a map of view managers that should be registered with {@link UIManagerModule} */
public Map<String, ModuleSpec> getViewManagersMap(final ReactApplicationContext reactContext) {
if (mViewManagers == null) {
Map<String, ModuleSpec> viewManagers = new HashMap<>();
appendMap(
viewManagers,
ReactDrawerLayoutManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactDrawerLayoutManager();
}
});
appendMap(
viewManagers,
ReactHorizontalScrollViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactHorizontalScrollViewManager();
}
});
appendMap(
viewManagers,
ReactHorizontalScrollContainerViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactHorizontalScrollContainerViewManager();
}
});
appendMap(
viewManagers,
ReactProgressBarViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactProgressBarViewManager();
}
});
appendMap(
viewManagers,
ReactScrollViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactScrollViewManager();
}
});
appendMap(
viewManagers,
ReactSwitchManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactSwitchManager();
}
});
appendMap(
viewManagers,
SwipeRefreshLayoutManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new SwipeRefreshLayoutManager();
}
});
appendMap(
viewManagers,
FrescoBasedReactTextInlineImageViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new FrescoBasedReactTextInlineImageViewManager();
}
});
appendMap(
viewManagers,
ReactImageManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactImageManager();
}
});
appendMap(
viewManagers,
ReactModalHostManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactModalHostManager();
}
});
appendMap(
viewManagers,
ReactRawTextManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactRawTextManager();
}
});
appendMap(
viewManagers,
ReactTextInputManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactTextInputManager();
}
});
appendMap(
viewManagers,
ReactTextViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactTextViewManager();
}
});
appendMap(
viewManagers,
ReactViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactViewManager();
}
});
appendMap(
viewManagers,
ReactVirtualTextViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactVirtualTextViewManager();
}
});
appendMap(
viewManagers,
ReactUnimplementedViewManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new ReactUnimplementedViewManager();
}
});
mViewManagers = viewManagers;
}
return mViewManagers;
}

@Override
public List<ModuleSpec> getViewManagers(ReactApplicationContext reactContext) {
return new ArrayList<>(getViewManagersMap(reactContext).values());
}

@Override
public Collection<String> 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 {
Expand Down

0 comments on commit d7eb3bf

Please sign in to comment.