|
7 | 7 |
|
8 | 8 | package com.reactnativecommunity.asyncstorage;
|
9 | 9 |
|
10 |
| -import android.util.Log; |
11 |
| -import com.facebook.react.ReactPackage; |
| 10 | +import com.facebook.react.TurboReactPackage; |
| 11 | +import com.facebook.react.ViewManagerOnDemandReactPackage; |
| 12 | +import com.facebook.react.bridge.ModuleSpec; |
12 | 13 | import com.facebook.react.bridge.JavaScriptModule;
|
13 | 14 | import com.facebook.react.bridge.NativeModule;
|
14 | 15 | import com.facebook.react.bridge.ReactApplicationContext;
|
15 |
| -import com.facebook.react.bridge.ReactContext; |
| 16 | +import com.facebook.react.module.annotations.ReactModule; |
| 17 | +import com.facebook.react.module.annotations.ReactModuleList; |
| 18 | +import com.facebook.react.module.model.ReactModuleInfo; |
| 19 | +import com.facebook.react.module.model.ReactModuleInfoProvider; |
| 20 | +import com.facebook.react.turbomodule.core.interfaces.TurboModule; |
16 | 21 | import com.facebook.react.uimanager.ViewManager;
|
17 |
| -import java.util.ArrayList; |
18 | 22 | import java.util.Collections;
|
| 23 | +import java.util.HashMap; |
19 | 24 | import java.util.List;
|
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import javax.annotation.Nonnull; |
| 28 | +import javax.annotation.Nullable; |
| 29 | + |
| 30 | +@ReactModuleList( |
| 31 | + nativeModules = { |
| 32 | + AsyncStorageModule.class, |
| 33 | + } |
| 34 | +) |
| 35 | +public class AsyncStoragePackage extends TurboReactPackage implements ViewManagerOnDemandReactPackage { |
| 36 | + |
| 37 | + @Override |
| 38 | + public List<String> getViewManagerNames(ReactApplicationContext reactContext) { |
| 39 | + return null; |
| 40 | + } |
20 | 41 |
|
21 |
| -public class AsyncStoragePackage implements ReactPackage { |
22 | 42 | @Override
|
23 |
| - public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { |
24 |
| - |
25 |
| - List<NativeModule> moduleList = new ArrayList<>(1); |
26 |
| - |
27 |
| - if (BuildConfig.AsyncStorage_useNextStorage) { |
28 |
| - try { |
29 |
| - Class storageClass = Class.forName("com.reactnativecommunity.asyncstorage.next.StorageModule"); |
30 |
| - NativeModule inst = (NativeModule) storageClass.getDeclaredConstructor(new Class[]{ReactContext.class}).newInstance(reactContext); |
31 |
| - moduleList.add(inst); |
32 |
| - AsyncLocalStorageUtil.verifyAndForceSqliteCheckpoint(reactContext); |
33 |
| - } catch (Exception e) { |
34 |
| - String message = "Something went wrong when initializing module:" |
35 |
| - + "\n" |
36 |
| - + e.getCause().getClass() |
37 |
| - + "\n" |
38 |
| - + "Cause:" + e.getCause().getLocalizedMessage(); |
39 |
| - Log.e("AsyncStorage_Next", message); |
40 |
| - } |
41 |
| - } else { |
42 |
| - moduleList.add(new AsyncStorageModule(reactContext)); |
| 43 | + protected List<ModuleSpec> getViewManagers(ReactApplicationContext reactContext) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public @Nullable ViewManager createViewManager( |
| 49 | + ReactApplicationContext reactContext, String viewManagerName) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public NativeModule getModule(String name, @Nonnull ReactApplicationContext reactContext) { |
| 55 | + switch (name) { |
| 56 | + case AsyncStorageModule.NAME: |
| 57 | + return new AsyncStorageModule(reactContext); |
| 58 | + default: |
| 59 | + return null; |
43 | 60 | }
|
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public ReactModuleInfoProvider getReactModuleInfoProvider() { |
| 65 | + try { |
| 66 | + Class<?> reactModuleInfoProviderClass = |
| 67 | + Class.forName("com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider"); |
| 68 | + return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); |
| 69 | + } catch (ClassNotFoundException e) { |
| 70 | + // ReactModuleSpecProcessor does not run at build-time. Create this ReactModuleInfoProvider by |
| 71 | + // hand. |
| 72 | + return new ReactModuleInfoProvider() { |
| 73 | + @Override |
| 74 | + public Map<String, ReactModuleInfo> getReactModuleInfos() { |
| 75 | + final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>(); |
44 | 76 |
|
45 |
| - return moduleList; |
| 77 | + Class<? extends NativeModule>[] moduleList = |
| 78 | + new Class[] { |
| 79 | + AsyncStorageModule.class, |
| 80 | + }; |
| 81 | + |
| 82 | + for (Class<? extends NativeModule> moduleClass : moduleList) { |
| 83 | + ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class); |
| 84 | + |
| 85 | + reactModuleInfoMap.put( |
| 86 | + reactModule.name(), |
| 87 | + new ReactModuleInfo( |
| 88 | + reactModule.name(), |
| 89 | + moduleClass.getName(), |
| 90 | + reactModule.canOverrideExistingModule(), |
| 91 | + reactModule.needsEagerInit(), |
| 92 | + reactModule.hasConstants(), |
| 93 | + reactModule.isCxxModule(), |
| 94 | + TurboModule.class.isAssignableFrom(moduleClass))); |
| 95 | + } |
| 96 | + |
| 97 | + return reactModuleInfoMap; |
| 98 | + } |
| 99 | + }; |
| 100 | + } catch (InstantiationException | IllegalAccessException e) { |
| 101 | + throw new RuntimeException( |
| 102 | + "No ReactModuleInfoProvider for com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider", e); |
| 103 | + } |
46 | 104 | }
|
47 | 105 |
|
48 | 106 | // Deprecated in RN 0.47
|
|
0 commit comments