|
| 1 | +package com.helloworld.newarchitecture; |
| 2 | + |
| 3 | +import android.app.Application; |
| 4 | +import androidx.annotation.NonNull; |
| 5 | +import com.facebook.react.PackageList; |
| 6 | +import com.facebook.react.ReactInstanceManager; |
| 7 | +import com.facebook.react.ReactNativeHost; |
| 8 | +import com.facebook.react.ReactPackage; |
| 9 | +import com.facebook.react.ReactPackageTurboModuleManagerDelegate; |
| 10 | +import com.facebook.react.bridge.JSIModulePackage; |
| 11 | +import com.facebook.react.bridge.JSIModuleProvider; |
| 12 | +import com.facebook.react.bridge.JSIModuleSpec; |
| 13 | +import com.facebook.react.bridge.JSIModuleType; |
| 14 | +import com.facebook.react.bridge.JavaScriptContextHolder; |
| 15 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 16 | +import com.facebook.react.bridge.UIManager; |
| 17 | +import com.facebook.react.fabric.ComponentFactory; |
| 18 | +import com.facebook.react.fabric.CoreComponentsRegistry; |
| 19 | +import com.facebook.react.fabric.EmptyReactNativeConfig; |
| 20 | +import com.facebook.react.fabric.FabricJSIModuleProvider; |
| 21 | +import com.facebook.react.uimanager.ViewManagerRegistry; |
| 22 | +import com.helloworld.BuildConfig; |
| 23 | +import com.helloworld.newarchitecture.components.MainComponentsRegistry; |
| 24 | +import com.helloworld.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +/** |
| 29 | + * A {@link ReactNativeHost} that helps you load everything needed for the New Architecture, both |
| 30 | + * TurboModule delegates and the Fabric Renderer. |
| 31 | + * |
| 32 | + * <p>Please note that this class is used ONLY if you opt-in for the New Architecture (see the |
| 33 | + * `newArchEnabled` property). Is ignored otherwise. |
| 34 | + */ |
| 35 | +public class MainApplicationReactNativeHost extends ReactNativeHost { |
| 36 | + public MainApplicationReactNativeHost(Application application) { |
| 37 | + super(application); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean getUseDeveloperSupport() { |
| 42 | + return BuildConfig.DEBUG; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected List<ReactPackage> getPackages() { |
| 47 | + List<ReactPackage> packages = new PackageList(this).getPackages(); |
| 48 | + // Packages that cannot be autolinked yet can be added manually here, for example: |
| 49 | + // packages.add(new MyReactNativePackage()); |
| 50 | + // TurboModules must also be loaded here providing a valid TurboReactPackage implementation: |
| 51 | + // packages.add(new TurboReactPackage() { ... }); |
| 52 | + // If you have custom Fabric Components, their ViewManagers should also be loaded here |
| 53 | + // inside a ReactPackage. |
| 54 | + return packages; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected String getJSMainModuleName() { |
| 59 | + return "index"; |
| 60 | + } |
| 61 | + |
| 62 | + @NonNull |
| 63 | + @Override |
| 64 | + protected ReactPackageTurboModuleManagerDelegate.Builder |
| 65 | + getReactPackageTurboModuleManagerDelegateBuilder() { |
| 66 | + // Here we provide the ReactPackageTurboModuleManagerDelegate Builder. This is necessary |
| 67 | + // for the new architecture and to use TurboModules correctly. |
| 68 | + return new MainApplicationTurboModuleManagerDelegate.Builder(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected JSIModulePackage getJSIModulePackage() { |
| 73 | + return new JSIModulePackage() { |
| 74 | + @Override |
| 75 | + public List<JSIModuleSpec> getJSIModules( |
| 76 | + final ReactApplicationContext reactApplicationContext, |
| 77 | + final JavaScriptContextHolder jsContext) { |
| 78 | + final List<JSIModuleSpec> specs = new ArrayList<>(); |
| 79 | + |
| 80 | + // Here we provide a new JSIModuleSpec that will be responsible of providing the |
| 81 | + // custom Fabric Components. |
| 82 | + specs.add( |
| 83 | + new JSIModuleSpec() { |
| 84 | + @Override |
| 85 | + public JSIModuleType getJSIModuleType() { |
| 86 | + return JSIModuleType.UIManager; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public JSIModuleProvider<UIManager> getJSIModuleProvider() { |
| 91 | + final ComponentFactory componentFactory = new ComponentFactory(); |
| 92 | + CoreComponentsRegistry.register(componentFactory); |
| 93 | + |
| 94 | + // Here we register a Components Registry. |
| 95 | + // The one that is generated with the template contains no components |
| 96 | + // and just provides you the one from React Native core. |
| 97 | + MainComponentsRegistry.register(componentFactory); |
| 98 | + |
| 99 | + final ReactInstanceManager reactInstanceManager = getReactInstanceManager(); |
| 100 | + |
| 101 | + ViewManagerRegistry viewManagerRegistry = |
| 102 | + new ViewManagerRegistry( |
| 103 | + reactInstanceManager.getOrCreateViewManagers(reactApplicationContext)); |
| 104 | + |
| 105 | + return new FabricJSIModuleProvider( |
| 106 | + reactApplicationContext, |
| 107 | + componentFactory, |
| 108 | + new EmptyReactNativeConfig(), |
| 109 | + viewManagerRegistry); |
| 110 | + } |
| 111 | + }); |
| 112 | + return specs; |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
| 116 | +} |
0 commit comments