Skip to content

Commit 3cddb22

Browse files
committed
fix(ios): resolve device consent new arch build errors
- Move old-arch RCT_EXPORT_METHODs into #else block to avoid duplicate declarations when New Architecture is enabled. - Move consentStateToDictionary onto RNMParticle (was wrongly on RCTConvert). - Use NSDictionary codegen types for new-arch setDeviceConsentState.
1 parent 1290eb1 commit 3cddb22

1 file changed

Lines changed: 86 additions & 118 deletions

File tree

ios/RNMParticle/RNMParticle.mm

Lines changed: 86 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ @interface MParticleUser ()
1919
- (void)setUserId:(NSNumber *)userId;
2020
@end
2121

22+
@interface RNMParticle (DeviceConsent)
23+
+ (NSDictionary *)consentStateToDictionary:(MPConsentState *)consentState;
24+
@end
25+
2226
// Forward declare so New Arch `logCommerceEvent` can use the same JS→native
2327
// mappings as `RCTConvert (MPCommerceEvent)` (defined later in this file).
2428
@interface RCTConvert (MPCommerceEvent)
@@ -109,29 +113,6 @@ + (void)load {
109113
user.consentState = consentState;
110114
}
111115

112-
RCT_EXPORT_METHOD(setDeviceConsentState:(NSDictionary *)consentState)
113-
{
114-
if (consentState == nil || consentState == (id)[NSNull null]) {
115-
return;
116-
}
117-
[MParticle sharedInstance].deviceConsentState = [RCTConvert MPConsentState:consentState];
118-
}
119-
120-
RCT_EXPORT_METHOD(clearDeviceConsentState)
121-
{
122-
[MParticle sharedInstance].deviceConsentState = nil;
123-
}
124-
125-
RCT_EXPORT_METHOD(getDeviceConsentState:(RCTResponseSenderBlock)callback)
126-
{
127-
MPConsentState *deviceConsent = [MParticle sharedInstance].deviceConsentState;
128-
if (deviceConsent == nil) {
129-
callback(@[[NSNull null]]);
130-
return;
131-
}
132-
callback(@[[RNMParticle consentStateToDictionary:deviceConsent]]);
133-
}
134-
135116
#if TARGET_OS_IOS == 1
136117
RCT_EXPORT_METHOD(logPushRegistration:(NSString *)token senderId:(NSString *)senderId)
137118
{
@@ -611,49 +592,13 @@ - (void)setCCPAConsentState:(JS::NativeMParticle::CCPAConsent &)consent {
611592

612593
- (void)setDeviceConsentState:(JS::NativeMParticle::DeviceConsentState &)consentState {
613594
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
614-
if (consentState.gdpr().has_value()) {
615-
NSMutableDictionary *gdprDict = [NSMutableDictionary dictionary];
616-
for (const auto &entry : consentState.gdpr().value()) {
617-
const auto &consent = entry.second;
618-
NSMutableDictionary *consentDict = [NSMutableDictionary dictionary];
619-
if (consent.consented().has_value()) {
620-
consentDict[@"consented"] = @(consent.consented().value());
621-
}
622-
if (consent.document()) {
623-
consentDict[@"document"] = consent.document();
624-
}
625-
if (consent.timestamp().has_value()) {
626-
consentDict[@"timestamp"] = @(consent.timestamp().value());
627-
}
628-
if (consent.location()) {
629-
consentDict[@"location"] = consent.location();
630-
}
631-
if (consent.hardwareId()) {
632-
consentDict[@"hardwareId"] = consent.hardwareId();
633-
}
634-
gdprDict[[NSString stringWithUTF8String:entry.first.c_str()]] = consentDict;
635-
}
636-
dict[@"gdpr"] = gdprDict;
595+
id gdpr = consentState.gdpr();
596+
if (gdpr != nil && gdpr != [NSNull null]) {
597+
dict[@"gdpr"] = gdpr;
637598
}
638-
if (consentState.ccpa().has_value()) {
639-
const auto &ccpa = consentState.ccpa().value();
640-
NSMutableDictionary *ccpaDict = [NSMutableDictionary dictionary];
641-
if (ccpa.consented().has_value()) {
642-
ccpaDict[@"consented"] = @(ccpa.consented().value());
643-
}
644-
if (ccpa.document()) {
645-
ccpaDict[@"document"] = ccpa.document();
646-
}
647-
if (ccpa.timestamp().has_value()) {
648-
ccpaDict[@"timestamp"] = @(ccpa.timestamp().value());
649-
}
650-
if (ccpa.location()) {
651-
ccpaDict[@"location"] = ccpa.location();
652-
}
653-
if (ccpa.hardwareId()) {
654-
ccpaDict[@"hardwareId"] = ccpa.hardwareId();
655-
}
656-
dict[@"ccpa"] = ccpaDict;
599+
id ccpa = consentState.ccpa();
600+
if (ccpa != nil && ccpa != [NSNull null]) {
601+
dict[@"ccpa"] = ccpa;
657602
}
658603
[MParticle sharedInstance].deviceConsentState = [RCTConvert MPConsentState:dict];
659604
}
@@ -700,6 +645,29 @@ - (void)getDeviceConsentState:(RCTResponseSenderBlock)callback {
700645
user.consentState = consentState;
701646
}
702647

648+
RCT_EXPORT_METHOD(setDeviceConsentState:(NSDictionary *)consentState)
649+
{
650+
if (consentState == nil || consentState == (id)[NSNull null]) {
651+
return;
652+
}
653+
[MParticle sharedInstance].deviceConsentState = [RCTConvert MPConsentState:consentState];
654+
}
655+
656+
RCT_EXPORT_METHOD(clearDeviceConsentState)
657+
{
658+
[MParticle sharedInstance].deviceConsentState = nil;
659+
}
660+
661+
RCT_EXPORT_METHOD(getDeviceConsentState:(RCTResponseSenderBlock)callback)
662+
{
663+
MPConsentState *deviceConsent = [MParticle sharedInstance].deviceConsentState;
664+
if (deviceConsent == nil) {
665+
callback(@[[NSNull null]]);
666+
return;
667+
}
668+
callback(@[[RNMParticle consentStateToDictionary:deviceConsent]]);
669+
}
670+
703671
#endif
704672

705673
// Helper method to create MPProduct from dictionary
@@ -827,6 +795,59 @@ + (BOOL)isNumericIdentityKey:(NSString *)key {
827795
return [numericSet isSupersetOfSet:keyCharacterSet];
828796
}
829797

798+
+ (NSDictionary *)consentStateToDictionary:(MPConsentState *)consentState
799+
{
800+
if (consentState == nil) {
801+
return nil;
802+
}
803+
804+
NSMutableDictionary *result = [NSMutableDictionary dictionary];
805+
NSDictionary<NSString *, MPGDPRConsent *> *gdprState = consentState.gdprConsentState;
806+
if (gdprState.count > 0) {
807+
NSMutableDictionary *gdpr = [NSMutableDictionary dictionaryWithCapacity:gdprState.count];
808+
for (NSString *purpose in gdprState) {
809+
MPGDPRConsent *consent = gdprState[purpose];
810+
NSMutableDictionary *consentDict = [NSMutableDictionary dictionary];
811+
consentDict[@"consented"] = @(consent.consented);
812+
if (consent.document) {
813+
consentDict[@"document"] = consent.document;
814+
}
815+
if (consent.timestamp) {
816+
consentDict[@"timestamp"] = @((long long)([consent.timestamp timeIntervalSince1970] * 1000));
817+
}
818+
if (consent.location) {
819+
consentDict[@"location"] = consent.location;
820+
}
821+
if (consent.hardwareId) {
822+
consentDict[@"hardwareId"] = consent.hardwareId;
823+
}
824+
gdpr[purpose] = consentDict;
825+
}
826+
result[@"gdpr"] = gdpr;
827+
}
828+
829+
MPCCPAConsent *ccpa = consentState.ccpaConsentState;
830+
if (ccpa != nil) {
831+
NSMutableDictionary *ccpaDict = [NSMutableDictionary dictionary];
832+
ccpaDict[@"consented"] = @(ccpa.consented);
833+
if (ccpa.document) {
834+
ccpaDict[@"document"] = ccpa.document;
835+
}
836+
if (ccpa.timestamp) {
837+
ccpaDict[@"timestamp"] = @((long long)([ccpa.timestamp timeIntervalSince1970] * 1000));
838+
}
839+
if (ccpa.location) {
840+
ccpaDict[@"location"] = ccpa.location;
841+
}
842+
if (ccpa.hardwareId) {
843+
ccpaDict[@"hardwareId"] = ccpa.hardwareId;
844+
}
845+
result[@"ccpa"] = ccpaDict;
846+
}
847+
848+
return result.count > 0 ? result : nil;
849+
}
850+
830851
@end
831852

832853
// RCTConvert category methods for mParticle types
@@ -993,59 +1014,6 @@ + (MPAliasRequest *)MPAliasRequest:(NSDictionary *)dict {
9931014
return [MPAliasRequest requestWithSourceMPID:sourceMpid destinationMPID:destinationMpid startTime:startTime endTime:endTime];
9941015
}
9951016

996-
+ (NSDictionary *)consentStateToDictionary:(MPConsentState *)consentState
997-
{
998-
if (consentState == nil) {
999-
return nil;
1000-
}
1001-
1002-
NSMutableDictionary *result = [NSMutableDictionary dictionary];
1003-
NSDictionary<NSString *, MPGDPRConsent *> *gdprState = consentState.gdprConsentState;
1004-
if (gdprState.count > 0) {
1005-
NSMutableDictionary *gdpr = [NSMutableDictionary dictionaryWithCapacity:gdprState.count];
1006-
for (NSString *purpose in gdprState) {
1007-
MPGDPRConsent *consent = gdprState[purpose];
1008-
NSMutableDictionary *consentDict = [NSMutableDictionary dictionary];
1009-
consentDict[@"consented"] = @(consent.consented);
1010-
if (consent.document) {
1011-
consentDict[@"document"] = consent.document;
1012-
}
1013-
if (consent.timestamp) {
1014-
consentDict[@"timestamp"] = @((long long)([consent.timestamp timeIntervalSince1970] * 1000));
1015-
}
1016-
if (consent.location) {
1017-
consentDict[@"location"] = consent.location;
1018-
}
1019-
if (consent.hardwareId) {
1020-
consentDict[@"hardwareId"] = consent.hardwareId;
1021-
}
1022-
gdpr[purpose] = consentDict;
1023-
}
1024-
result[@"gdpr"] = gdpr;
1025-
}
1026-
1027-
MPCCPAConsent *ccpa = consentState.ccpaConsentState;
1028-
if (ccpa != nil) {
1029-
NSMutableDictionary *ccpaDict = [NSMutableDictionary dictionary];
1030-
ccpaDict[@"consented"] = @(ccpa.consented);
1031-
if (ccpa.document) {
1032-
ccpaDict[@"document"] = ccpa.document;
1033-
}
1034-
if (ccpa.timestamp) {
1035-
ccpaDict[@"timestamp"] = @((long long)([ccpa.timestamp timeIntervalSince1970] * 1000));
1036-
}
1037-
if (ccpa.location) {
1038-
ccpaDict[@"location"] = ccpa.location;
1039-
}
1040-
if (ccpa.hardwareId) {
1041-
ccpaDict[@"hardwareId"] = ccpa.hardwareId;
1042-
}
1043-
result[@"ccpa"] = ccpaDict;
1044-
}
1045-
1046-
return result.count > 0 ? result : nil;
1047-
}
1048-
10491017
@end
10501018

10511019
typedef NS_ENUM(NSUInteger, MPReactCommerceEventAction) {

0 commit comments

Comments
 (0)