|
1 | | -package com.helloworld.newarchitecture.components; |
| 1 | +package com.helloworld.newarchitecture; |
2 | 2 |
|
3 | | -import com.facebook.jni.HybridData; |
4 | | -import com.facebook.proguard.annotations.DoNotStrip; |
| 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; |
5 | 17 | import com.facebook.react.fabric.ComponentFactory; |
6 | | -import com.facebook.soloader.SoLoader; |
| 18 | +import com.facebook.react.fabric.CoreComponentsRegistry; |
| 19 | +import com.facebook.react.fabric.FabricJSIModuleProvider; |
| 20 | +import com.facebook.react.fabric.ReactNativeConfig; |
| 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; |
7 | 27 |
|
8 | 28 | /** |
9 | | - * Class responsible to load the custom Fabric Components. This class has native methods and needs a |
10 | | - * corresponding C++ implementation/header file to work correctly (already placed inside the jni/ |
11 | | - * folder for you). |
| 29 | + * A {@link ReactNativeHost} that helps you load everything needed for the New Architecture, both |
| 30 | + * TurboModule delegates and the Fabric Renderer. |
12 | 31 | * |
13 | 32 | * <p>Please note that this class is used ONLY if you opt-in for the New Architecture (see the |
14 | 33 | * `newArchEnabled` property). Is ignored otherwise. |
15 | 34 | */ |
16 | | -@DoNotStrip |
17 | | -public class MainComponentsRegistry { |
18 | | - static { |
19 | | - SoLoader.loadLibrary("fabricjni"); |
| 35 | +public class MainApplicationReactNativeHost extends ReactNativeHost { |
| 36 | + public MainApplicationReactNativeHost(Application application) { |
| 37 | + super(application); |
20 | 38 | } |
21 | 39 |
|
22 | | - @DoNotStrip private final HybridData mHybridData; |
| 40 | + @Override |
| 41 | + public boolean getUseDeveloperSupport() { |
| 42 | + return BuildConfig.DEBUG; |
| 43 | + } |
23 | 44 |
|
24 | | - @DoNotStrip |
25 | | - private native HybridData initHybrid(ComponentFactory componentFactory); |
| 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 | + } |
26 | 56 |
|
27 | | - @DoNotStrip |
28 | | - private MainComponentsRegistry(ComponentFactory componentFactory) { |
29 | | - mHybridData = initHybrid(componentFactory); |
| 57 | + @Override |
| 58 | + protected String getJSMainModuleName() { |
| 59 | + return "index"; |
30 | 60 | } |
31 | 61 |
|
32 | | - @DoNotStrip |
33 | | - public static MainComponentsRegistry register(ComponentFactory componentFactory) { |
34 | | - return new MainComponentsRegistry(componentFactory); |
| 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 | + ReactNativeConfig.DEFAULT_CONFIG, |
| 109 | + viewManagerRegistry); |
| 110 | + } |
| 111 | + }); |
| 112 | + return specs; |
| 113 | + } |
| 114 | + }; |
35 | 115 | } |
36 | 116 | } |
0 commit comments