Skip to content

Commit

Permalink
Add Android bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaelvilleneuve committed Sep 27, 2018
1 parent b9f8992 commit a527bf8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
59 changes: 58 additions & 1 deletion android/src/main/java/com/reactlibrary/RNFastStorageModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.tencent.mmkv.MMKV;

public class RNFastStorageModule extends ReactContextBaseJavaModule {

Expand All @@ -19,4 +20,60 @@ public RNFastStorageModule(ReactApplicationContext reactContext) {
public String getName() {
return "RNFastStorage";
}

@ReactMethod
public void setupLibrary() {
MMKV.initialize(getReactApplicationContext());
}

@ReactMethod
public void setItem(String key, String value, Promise promise) {
try {
MMKV kv = MMKV.defaultMMKV();
kv.encode(key, value);
promise.resolve(value);
} catch (Error e) {
promise.reject("Error", "Unable to setItem");
} catch (Exception e) {
promise.reject("Error", "Unable to setItem");
}
}

@ReactMethod
public void getItem(String key, Promise promise) {
try {
MMKV kv = MMKV.defaultMMKV();
promise.resolve(kv.decodeString(key));
} catch (Error e) {
promise.reject("Error", "Unable to getItem");
} catch (Exception e) {
promise.reject("Error", "Unable to getItem");
}
}

@ReactMethod
public void removeItem(String key, Promise promise) {
try {
MMKV kv = MMKV.defaultMMKV();
kv.removeValueForKey(key);
promise.resolve(key);
} catch (Error e) {
promise.reject("Error", "Unable to removeItem");
} catch (Exception e) {
promise.reject("Error", "Unable to removeItem");
}
}

@ReactMethod
public void clearStore(Promise promise) {
try {
MMKV kv = MMKV.defaultMMKV();
kv.clearAll();
promise.resolve("Done");
} catch (Error e) {
promise.reject("Error", "Unable to removeItem");
} catch (Exception e) {
promise.reject("Error", "Unable to removeItem");
}
}
}
25 changes: 13 additions & 12 deletions android/src/main/java/com/reactlibrary/RNFastStoragePackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;

public class RNFastStoragePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNFastStorageModule(reactContext));
}
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNFastStorageModule(reactContext));
}

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { NativeModules } from 'react-native';
import { NativeModules } from "react-native";

const { RNFastStorage } = NativeModules;

if (RNFastStorage.setupLibrary) RNFastStorage.setupLibrary();

export default RNFastStorage;

0 comments on commit a527bf8

Please sign in to comment.