Skip to content

Commit

Permalink
Add more native access methods for android (#343)
Browse files Browse the repository at this point in the history
* Add more native access methods for android

* Add methods for decoding double

* Update encode methods, add int setters/getters

* Fix missing annotation import

* Change putString to encode
  • Loading branch information
josephyanks authored Aug 16, 2024
1 parent 92a6e47 commit 4d17859
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
41 changes: 36 additions & 5 deletions android/src/main/java/com/ammarahmed/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -205,9 +203,18 @@ public int decodeInt(String key, int defaultValue) {

private native int decodeInt(long handle, String key, int defaultValue);

public void putString(String key, @Nullable String value) {
encodeString(nativeHandle, key, value);
public double decodeDouble(String key) {
return decodeDouble(nativeHandle, key, 0);
}

public double decodeDouble(String key, double defaultValue) {
return decodeDouble(nativeHandle, key, defaultValue);
}

private native double decodeDouble(long handle, String key, double defaultValue);

public boolean encode(String key, @Nullable String value) {
return encodeString(nativeHandle, key, value);
}

private native boolean encodeString(long handle, String key, @Nullable String value);
Expand All @@ -218,13 +225,37 @@ public boolean encode(String key, double value) {

private native boolean encodeDouble(long handle, String key, double value);

public boolean encode(String key, int value) {
return encodeInt(nativeHandle, key, value);
}

private native boolean encodeInt(long handle, String key, int value);

public boolean encode(String key, @Nullable Set<String> value) {
return encodeSet(nativeHandle, key, (value == null) ? null : value.toArray(new String[0]));
}

private native boolean encodeSet(long handle, String key, @Nullable String[] value);

@Nullable
public native String[] getAllKeys(long handle);

@NonNull
public Set<String> getAllKeys() {
String[] result = getAllKeys(nativeHandle);

if(result == null) {
return Collections.emptySet();
}

return new HashSet<>(Arrays.asList(result));
}

@Nullable
private native String decodeString(long handle, String key, String defaultValue);

@Nullable
public String decodeString(String key, @Nullable String defaultValue) {
return decodeString(nativeHandle, key, defaultValue);
}
}
6 changes: 3 additions & 3 deletions android/src/main/java/com/ammarahmed/mmkv/RNMMKVModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void migrate() {
}
Gson gson = new Gson();
String json = gson.toJson(child);
kv.putString(key, json);
kv.encode(key, json);

if (isEncrypted) {
String alias = (String) child.get("alias");
Expand Down Expand Up @@ -137,7 +137,7 @@ public void writeToJSON(MMKV kvv) {
if (bundle != null) {
WritableMap map = Arguments.fromBundle(bundle);
String obj = gson.toJson(map.toHashMap());
kvv.putString(string, obj);
kvv.encode(string, obj);
}

}
Expand All @@ -152,7 +152,7 @@ public void writeToJSON(MMKV kvv) {
if (map.getArray(string) != null) {
ArrayList<Object> list = map.getArray(string).toArrayList();
String obj = gson.toJson(list);
kvv.putString(string, obj);
kvv.encode(string, obj);
}

}
Expand Down
56 changes: 56 additions & 0 deletions android/src/main/rnmmkv/rnmmkv-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,21 @@ Java_com_ammarahmed_mmkv_MMKV_removeValueForKey(JNIEnv *env, jobject instance, j
}
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_ammarahmed_mmkv_MMKV_decodeString(JNIEnv *env, jobject obj, jlong handle, jstring oKey,
jstring default_value) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (kv && oKey) {
string key = jstring2string(env, oKey);
string value;
if (kv->getString(key, value)) {
return string2jstring(env, value);
}
}
return default_value;
}

extern "C" JNIEXPORT jobjectArray JNICALL
Java_com_ammarahmed_mmkv_MMKV_decodeStringSet(JNIEnv *env, jobject, jlong handle, jstring oKey)
{
Expand Down Expand Up @@ -870,6 +885,24 @@ Java_com_ammarahmed_mmkv_MMKV_decodeInt(JNIEnv *env, jobject obj, jlong handle,
return defaultValue;
}

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_ammarahmed_mmkv_MMKV_decodeDouble(JNIEnv *env, jobject obj, jlong handle, jstring oKey,
jdouble default_value) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (kv && oKey) {
string key = jstring2string(env, oKey);
bool hasValue;
double value = kv->getDouble(key, 0, &hasValue);

if (hasValue) {
return value;
}
}
return default_value;
}


extern "C" JNIEXPORT jboolean JNICALL
Java_com_ammarahmed_mmkv_MMKV_checkProcessMode(JNIEnv *env, jclass clazz, jlong handle)
{
Expand Down Expand Up @@ -916,6 +949,17 @@ Java_com_ammarahmed_mmkv_MMKV_encodeDouble(JNIEnv *env, jobject thiz, jlong hand
return (jboolean) false;
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_ammarahmed_mmkv_MMKV_encodeInt(JNIEnv *env, jobject thiz, jlong handle, jstring oKey, jint value) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (kv && oKey) {
string key = jstring2string(env, oKey);
return (jboolean) kv->set((int) value, key);
}
return (jboolean) false;
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_ammarahmed_mmkv_MMKV_encodeSet(JNIEnv *env, jobject thiz, jlong handle, jstring oKey,
jobjectArray arrStr)
Expand All @@ -938,6 +982,18 @@ Java_com_ammarahmed_mmkv_MMKV_encodeSet(JNIEnv *env, jobject thiz, jlong handle,
return (jboolean) false;
}

extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_ammarahmed_mmkv_MMKV_getAllKeys(JNIEnv *env, jobject obj, jlong handle) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (!kv) {
return nullptr;
}
auto keys = kv->allKeys();

return vector2jarray(env, keys);
}

extern "C" JNIEXPORT void JNICALL
Java_com_ammarahmed_mmkv_MMKV_jniInitialize(JNIEnv *env, jclass clazz, jstring rootDir,
jint logLevel)
Expand Down

0 comments on commit 4d17859

Please sign in to comment.