Skip to content

Commit

Permalink
Android implementation (for java version)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-piasecki committed Feb 6, 2023
1 parent ba302ca commit a090679
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<Void, Void>(getReactApplicationContext()) {
@Override
Expand Down Expand Up @@ -362,6 +366,7 @@ protected void doInBackgroundGuarded(Void... params) {
* Clears the database.
*/
@ReactMethod
@Override
public void clear(final Callback callback) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
@Override
Expand All @@ -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<Void, Void>(getReactApplicationContext()) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getViewManagerNames(ReactApplicationContext reactContext) {
return null;
}

public class AsyncStoragePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {

List<NativeModule> 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<ModuleSpec> 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<String, ReactModuleInfo> getReactModuleInfos() {
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();

return moduleList;
Class<? extends NativeModule>[] moduleList =
new Class[] {
AsyncStorageModule.class,
};

for (Class<? extends NativeModule> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a090679

Please sign in to comment.