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( + "addElementToStringSet"); + method(cls, setKey, element); + } + + static void + removeElementFromStringSet(std::string setKey, std::string element) { + static const auto cls = javaClassStatic(); + static auto method = cls->getStaticMethod( + "removeElementFromStringSet"); + method(cls, setKey, element); + } + + static int getStringSetSize(std::string setKey) { + static const auto cls = javaClassStatic(); + static auto method = + cls->getStaticMethod("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 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 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 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 getAllKeys(); static void removeKeys(const std::vector &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