Skip to content

Commit a090679

Browse files
committed
Android implementation (for java version)
1 parent ba302ca commit a090679

File tree

3 files changed

+93
-29
lines changed

3 files changed

+93
-29
lines changed

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434

3535
@ReactModule(name = AsyncStorageModule.NAME)
3636
public final class AsyncStorageModule
37-
extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable, LifecycleEventListener {
37+
extends NativeAsyncStorageModuleSpec implements ModuleDataCleaner.Cleanable, LifecycleEventListener {
3838

3939
// changed name to not conflict with AsyncStorage from RN repo
40-
public static final String NAME = "RNC_AsyncSQLiteDBStorage";
40+
public static final String NAME = "RNCAsyncStorage";
4141

4242
// SQL variable number limit, defined by SQLITE_LIMIT_VARIABLE_NUMBER:
4343
// https://raw.githubusercontent.com/android/platform_external_sqlite/master/dist/sqlite3.c
@@ -110,6 +110,7 @@ public void onHostDestroy() {
110110
* (key, null) for the keys that haven't been found.
111111
*/
112112
@ReactMethod
113+
@Override
113114
public void multiGet(final ReadableArray keys, final Callback callback) {
114115
if (keys == null) {
115116
callback.invoke(AsyncStorageErrorUtil.getInvalidKeyError(null), null);
@@ -183,6 +184,7 @@ protected void doInBackgroundGuarded(Void... params) {
183184
* The insertion will replace conflicting (key, value) pairs.
184185
*/
185186
@ReactMethod
187+
@Override
186188
public void multiSet(final ReadableArray keyValueArray, final Callback callback) {
187189
if (keyValueArray.size() == 0) {
188190
callback.invoke();
@@ -248,6 +250,7 @@ protected void doInBackgroundGuarded(Void... params) {
248250
* Removes all rows of the keys given.
249251
*/
250252
@ReactMethod
253+
@Override
251254
public void multiRemove(final ReadableArray keys, final Callback callback) {
252255
if (keys.size() == 0) {
253256
callback.invoke();
@@ -300,6 +303,7 @@ protected void doInBackgroundGuarded(Void... params) {
300303
* of the given keys, if they exist.
301304
*/
302305
@ReactMethod
306+
@Override
303307
public void multiMerge(final ReadableArray keyValueArray, final Callback callback) {
304308
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
305309
@Override
@@ -362,6 +366,7 @@ protected void doInBackgroundGuarded(Void... params) {
362366
* Clears the database.
363367
*/
364368
@ReactMethod
369+
@Override
365370
public void clear(final Callback callback) {
366371
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
367372
@Override
@@ -385,6 +390,7 @@ protected void doInBackgroundGuarded(Void... params) {
385390
* Returns an array with all keys from the database.
386391
*/
387392
@ReactMethod
393+
@Override
388394
public void getAllKeys(final Callback callback) {
389395
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
390396
@Override

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,100 @@
77

88
package com.reactnativecommunity.asyncstorage;
99

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;
1213
import com.facebook.react.bridge.JavaScriptModule;
1314
import com.facebook.react.bridge.NativeModule;
1415
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;
1621
import com.facebook.react.uimanager.ViewManager;
17-
import java.util.ArrayList;
1822
import java.util.Collections;
23+
import java.util.HashMap;
1924
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+
}
2041

21-
public class AsyncStoragePackage implements ReactPackage {
2242
@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;
4360
}
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<>();
4476

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+
}
46104
}
47105

48106
// Deprecated in RN 0.47

android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import kotlinx.coroutines.asExecutor
1717
import kotlinx.coroutines.launch
1818

1919
class StorageModule(reactContext: ReactContext) : ReactContextBaseJavaModule(), CoroutineScope {
20-
override fun getName() = "RNC_AsyncSQLiteDBStorage"
20+
override fun getName() = "RNCAsyncStorage"
2121

2222
// this executor is not used by the module, but it must exists here due to
2323
// Detox relying on this implementation detail to run

0 commit comments

Comments
 (0)