Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3394742
D13166.id43630.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D13166.id43630.diff
View Options
diff --git a/native/android/app/src/cpp/CommMMKV.cpp b/native/android/app/src/cpp/CommMMKV.cpp
--- a/native/android/app/src/cpp/CommMMKV.cpp
+++ b/native/android/app/src/cpp/CommMMKV.cpp
@@ -96,6 +96,32 @@
}
method(cls, keysJava);
}
+
+ static void addElementToStringSet(std::string setKey, std::string element) {
+ static const auto cls = javaClassStatic();
+ static auto method = cls->getStaticMethod<void(std::string, std::string)>(
+ "addElementToStringSet");
+ method(cls, setKey, element);
+ }
+
+ static void
+ removeElementFromStringSet(std::string setKey, std::string element) {
+ static const auto cls = javaClassStatic();
+ static auto method = cls->getStaticMethod<void(std::string, std::string)>(
+ "removeElementFromStringSet");
+ method(cls, setKey, element);
+ }
+
+ static int getStringSetSize(std::string setKey) {
+ static const auto cls = javaClassStatic();
+ static auto method =
+ cls->getStaticMethod<JInteger(std::string)>("getStringSetSize");
+ const auto result = method(cls, setKey);
+ if (result) {
+ return result->value();
+ }
+ return 0;
+ }
};
namespace comm {
@@ -157,4 +183,24 @@
NativeAndroidAccessProvider::runTask(
[&]() { CommMMKVJavaClass::removeKeys(keys); });
}
+
+void CommMMKV::addElementToStringSet(std::string setKey, std::string element) {
+ NativeAndroidAccessProvider::runTask(
+ [&]() { CommMMKVJavaClass::addElementToStringSet(setKey, element); });
+}
+
+void CommMMKV::removeElementFromStringSet(
+ std::string setKey,
+ std::string element) {
+ NativeAndroidAccessProvider::runTask([&]() {
+ CommMMKVJavaClass::removeElementFromStringSet(setKey, element);
+ });
+}
+
+int CommMMKV::getStringSetSize(std::string setKey) {
+ int result;
+ NativeAndroidAccessProvider::runTask(
+ [&]() { result = CommMMKVJavaClass::getStringSetSize(setKey); });
+ return result;
+}
} // namespace comm
diff --git a/native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java b/native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
--- a/native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
+++ b/native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
@@ -6,6 +6,7 @@
import app.comm.android.fbjni.PlatformSpecificTools;
import com.tencent.mmkv.MMKV;
import java.util.Base64;
+import java.util.Set;
public class CommMMKV {
private static final int MMKV_ENCRYPTION_KEY_SIZE = 16;
@@ -135,4 +136,48 @@
getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey)
.removeValuesForKeys(keys);
}
+
+ public static void addElementToStringSet(String setKey, String element) {
+ initialize();
+ MMKV mmkv = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey);
+ mmkv.lock();
+ try {
+ Set<String> stringSet = mmkv.decodeStringSet(setKey);
+ if (stringSet != null) {
+ stringSet.add(element);
+ } else {
+ stringSet = Set.of(element);
+ }
+ mmkv.encode(setKey, stringSet);
+ } finally {
+ mmkv.unlock();
+ }
+ }
+
+ public static void removeElementFromStringSet(String setKey, String element) {
+ initialize();
+ MMKV mmkv = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey);
+ mmkv.lock();
+ try {
+ Set<String> stringSet = mmkv.decodeStringSet(setKey);
+ if (stringSet == null) {
+ return;
+ }
+ stringSet.remove(element);
+ mmkv.encode(setKey, stringSet);
+ } finally {
+ mmkv.unlock();
+ }
+ }
+
+ public static int getStringSetSize(String setKey) {
+ initialize();
+ Set<String> stringSet = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey)
+ .decodeStringSet(setKey);
+ if (stringSet == null) {
+ return 0;
+ }
+
+ return stringSet.size();
+ }
}
diff --git a/native/cpp/CommonCpp/Tools/CommMMKV.h b/native/cpp/CommonCpp/Tools/CommMMKV.h
--- a/native/cpp/CommonCpp/Tools/CommMMKV.h
+++ b/native/cpp/CommonCpp/Tools/CommMMKV.h
@@ -27,6 +27,11 @@
static std::vector<std::string> getAllKeys();
static void removeKeys(const std::vector<std::string> &keys);
+ static void addElementToStringSet(std::string setKey, std::string element);
+ static void
+ removeElementFromStringSet(std::string setKey, std::string element);
+ static int getStringSetSize(std::string setKey);
+
class InitFromNSEForbiddenError : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
diff --git a/native/ios/Comm/CommMMKV.mm b/native/ios/Comm/CommMMKV.mm
--- a/native/ios/Comm/CommMMKV.mm
+++ b/native/ios/Comm/CommMMKV.mm
@@ -212,4 +212,61 @@
[mmkv removeValuesForKeys:keysObjC];
}
+void CommMMKV::addElementToStringSet(std::string setKey, std::string element) {
+ NSString *setKeyObjC = [NSString stringWithCString:setKey.c_str()
+ encoding:NSUTF8StringEncoding];
+ NSString *elementObjC = [NSString stringWithCString:element.c_str()
+ encoding:NSUTF8StringEncoding];
+
+ CommMMKV::ScopedCommMMKVLock();
+ MMKV *mmkv = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey);
+ NSMutableSet *stringSet =
+ [mmkv getObjectOfClass:NSMutableSet.class forKey:setKeyObjC];
+
+ if (stringSet) {
+ [stringSet addObject:elementObjC];
+ } else {
+ stringSet = [NSMutableSet setWithObject:elementObjC];
+ }
+
+ [mmkv setObject:stringSet forKey:setKeyObjC];
+}
+
+void CommMMKV::removeElementFromStringSet(
+ std::string setKey,
+ std::string element) {
+ NSString *setKeyObjC = [NSString stringWithCString:setKey.c_str()
+ encoding:NSUTF8StringEncoding];
+ NSString *elementObjC = [NSString stringWithCString:element.c_str()
+ encoding:NSUTF8StringEncoding];
+
+ CommMMKV::ScopedCommMMKVLock();
+ MMKV *mmkv = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey);
+ NSMutableSet *stringSet =
+ [mmkv getObjectOfClass:NSMutableSet.class forKey:setKeyObjC];
+
+ if (!stringSet) {
+ return;
+ }
+
+ [stringSet removeObject:elementObjC];
+ [mmkv setObject:stringSet forKey:setKeyObjC];
+}
+
+int CommMMKV::getStringSetSize(std::string setKey) {
+ NSString *setKeyObjC = [NSString stringWithCString:setKey.c_str()
+ encoding:NSUTF8StringEncoding];
+
+ CommMMKV::ScopedCommMMKVLock();
+ MMKV *mmkv = getMMKVInstance(mmkvIdentifier, mmkvEncryptionKey);
+ NSMutableSet *stringSet =
+ [mmkv getObjectOfClass:NSMutableSet.class forKey:setKeyObjC];
+
+ if (!stringSet) {
+ return 0;
+ }
+
+ return [stringSet count];
+}
+
} // namespace comm
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 1, 9:45 PM (22 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2606169
Default Alt Text
D13166.id43630.diff (6 KB)
Attached To
Mode
D13166: Enrich CommMMKV api with operations that manipulate string set
Attached
Detach File
Event Timeline
Log In to Comment