diff --git a/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java b/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java index 906008e2..3e402486 100644 --- a/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java +++ b/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java @@ -34,10 +34,10 @@ @ReactModule(name = AsyncStorageModule.NAME) public final class AsyncStorageModule - extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable, LifecycleEventListener { + extends NativeAsyncStorageModuleSpec implements ModuleDataCleaner.Cleanable, LifecycleEventListener { // changed name to not conflict with AsyncStorage from RN repo - public static final String NAME = "RNC_AsyncSQLiteDBStorage"; + public static final String NAME = "RNCAsyncStorage"; // SQL variable number limit, defined by SQLITE_LIMIT_VARIABLE_NUMBER: // https://raw.githubusercontent.com/android/platform_external_sqlite/master/dist/sqlite3.c @@ -110,6 +110,7 @@ public void onHostDestroy() { * (key, null) for the keys that haven't been found. */ @ReactMethod + @Override public void multiGet(final ReadableArray keys, final Callback callback) { if (keys == null) { callback.invoke(AsyncStorageErrorUtil.getInvalidKeyError(null), null); @@ -183,6 +184,7 @@ protected void doInBackgroundGuarded(Void... params) { * The insertion will replace conflicting (key, value) pairs. */ @ReactMethod + @Override public void multiSet(final ReadableArray keyValueArray, final Callback callback) { if (keyValueArray.size() == 0) { callback.invoke(); @@ -248,6 +250,7 @@ protected void doInBackgroundGuarded(Void... params) { * Removes all rows of the keys given. */ @ReactMethod + @Override public void multiRemove(final ReadableArray keys, final Callback callback) { if (keys.size() == 0) { callback.invoke(); @@ -300,6 +303,7 @@ protected void doInBackgroundGuarded(Void... params) { * of the given keys, if they exist. */ @ReactMethod + @Override public void multiMerge(final ReadableArray keyValueArray, final Callback callback) { new GuardedAsyncTask(getReactApplicationContext()) { @Override @@ -362,6 +366,7 @@ protected void doInBackgroundGuarded(Void... params) { * Clears the database. */ @ReactMethod + @Override public void clear(final Callback callback) { new GuardedAsyncTask(getReactApplicationContext()) { @Override @@ -385,6 +390,7 @@ protected void doInBackgroundGuarded(Void... params) { * Returns an array with all keys from the database. */ @ReactMethod + @Override public void getAllKeys(final Callback callback) { new GuardedAsyncTask(getReactApplicationContext()) { @Override diff --git a/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java b/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java index 28a78408..5ccd552c 100644 --- a/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java +++ b/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java @@ -7,42 +7,100 @@ package com.reactnativecommunity.asyncstorage; -import android.util.Log; -import com.facebook.react.ReactPackage; +import com.facebook.react.TurboReactPackage; +import com.facebook.react.ViewManagerOnDemandReactPackage; +import com.facebook.react.bridge.ModuleSpec; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContext; +import com.facebook.react.module.annotations.ReactModule; +import com.facebook.react.module.annotations.ReactModuleList; +import com.facebook.react.module.model.ReactModuleInfo; +import com.facebook.react.module.model.ReactModuleInfoProvider; +import com.facebook.react.turbomodule.core.interfaces.TurboModule; import com.facebook.react.uimanager.ViewManager; -import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +@ReactModuleList( + nativeModules = { + AsyncStorageModule.class, + } +) +public class AsyncStoragePackage extends TurboReactPackage implements ViewManagerOnDemandReactPackage { + + @Override + public List getViewManagerNames(ReactApplicationContext reactContext) { + return null; + } -public class AsyncStoragePackage implements ReactPackage { @Override - public List createNativeModules(ReactApplicationContext reactContext) { - - List moduleList = new ArrayList<>(1); - - if (BuildConfig.AsyncStorage_useNextStorage) { - try { - Class storageClass = Class.forName("com.reactnativecommunity.asyncstorage.next.StorageModule"); - NativeModule inst = (NativeModule) storageClass.getDeclaredConstructor(new Class[]{ReactContext.class}).newInstance(reactContext); - moduleList.add(inst); - AsyncLocalStorageUtil.verifyAndForceSqliteCheckpoint(reactContext); - } catch (Exception e) { - String message = "Something went wrong when initializing module:" - + "\n" - + e.getCause().getClass() - + "\n" - + "Cause:" + e.getCause().getLocalizedMessage(); - Log.e("AsyncStorage_Next", message); - } - } else { - moduleList.add(new AsyncStorageModule(reactContext)); + protected List getViewManagers(ReactApplicationContext reactContext) { + return null; + } + + @Override + public @Nullable ViewManager createViewManager( + ReactApplicationContext reactContext, String viewManagerName) { + return null; + } + + @Override + public NativeModule getModule(String name, @Nonnull ReactApplicationContext reactContext) { + switch (name) { + case AsyncStorageModule.NAME: + return new AsyncStorageModule(reactContext); + default: + return null; } + } + + @Override + public ReactModuleInfoProvider getReactModuleInfoProvider() { + try { + Class reactModuleInfoProviderClass = + Class.forName("com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider"); + return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); + } catch (ClassNotFoundException e) { + // ReactModuleSpecProcessor does not run at build-time. Create this ReactModuleInfoProvider by + // hand. + return new ReactModuleInfoProvider() { + @Override + public Map getReactModuleInfos() { + final Map reactModuleInfoMap = new HashMap<>(); - return moduleList; + Class[] moduleList = + new Class[] { + AsyncStorageModule.class, + }; + + for (Class moduleClass : moduleList) { + ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class); + + reactModuleInfoMap.put( + reactModule.name(), + new ReactModuleInfo( + reactModule.name(), + moduleClass.getName(), + reactModule.canOverrideExistingModule(), + reactModule.needsEagerInit(), + reactModule.hasConstants(), + reactModule.isCxxModule(), + TurboModule.class.isAssignableFrom(moduleClass))); + } + + return reactModuleInfoMap; + } + }; + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException( + "No ReactModuleInfoProvider for com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider", e); + } } // Deprecated in RN 0.47 diff --git a/android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt b/android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt index ade13167..883d1d3f 100644 --- a/android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt +++ b/android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt @@ -17,7 +17,7 @@ import kotlinx.coroutines.asExecutor import kotlinx.coroutines.launch class StorageModule(reactContext: ReactContext) : ReactContextBaseJavaModule(), CoroutineScope { - override fun getName() = "RNC_AsyncSQLiteDBStorage" + override fun getName() = "RNCAsyncStorage" // this executor is not used by the module, but it must exists here due to // Detox relying on this implementation detail to run