Skip to content

Commit 4ae1903

Browse files
feat: Java enum for MPCommerceEventActionType was base 1 (#336)
1 parent 7816556 commit 4ae1903

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

ios/RNMParticle/RNMParticle.mm

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

22+
// Forward declare so New Arch `logCommerceEvent` can use the same JS→native
23+
// mappings as `RCTConvert (MPCommerceEvent)` (defined later in this file).
24+
@interface RCTConvert (MPCommerceEvent)
25+
+ (MPCommerceEventAction)MPCommerceEventAction:(id)json;
26+
+ (MPPromotionAction)MPPromotionAction:(id)json;
27+
@end
28+
2229
@implementation RNMParticle
2330

2431
RCT_EXTERN void RCTRegisterModule(Class);
@@ -447,11 +454,14 @@ - (void)logCommerceEvent:(JS::NativeMParticle::CommerceEvent &)commerceEvent {
447454
MPCommerceEvent *mpCommerceEvent = [[MPCommerceEvent alloc] init];
448455

449456
if (commerceEvent.productActionType().has_value()) {
450-
mpCommerceEvent.action = (MPCommerceEventAction)commerceEvent.productActionType().value();
457+
mpCommerceEvent.action = [RCTConvert MPCommerceEventAction:@(commerceEvent.productActionType().value())];
451458
}
452459

453460
if (commerceEvent.promotionActionType().has_value()) {
454-
mpCommerceEvent.promotionContainer = [[MPPromotionContainer alloc] initWithAction:(MPPromotionAction)commerceEvent.promotionActionType().value() promotion:nil];
461+
MPPromotionAction promotionAction =
462+
[RCTConvert MPPromotionAction:@(commerceEvent.promotionActionType().value())];
463+
mpCommerceEvent.promotionContainer =
464+
[[MPPromotionContainer alloc] initWithAction:promotionAction promotion:nil];
455465
}
456466

457467
if (commerceEvent.products().has_value()) {
@@ -778,7 +788,7 @@ + (MPCommerceEvent *)MPCommerceEvent:(NSDictionary *)dict {
778788
MPCommerceEvent *commerceEvent = [[MPCommerceEvent alloc] init];
779789

780790
if (dict[@"productActionType"] && dict[@"productActionType"] != [NSNull null]) {
781-
commerceEvent.action = (MPCommerceEventAction)[dict[@"productActionType"] integerValue];
791+
commerceEvent.action = [RCTConvert MPCommerceEventAction:dict[@"productActionType"]];
782792
}
783793

784794
if (dict[@"products"] && dict[@"products"] != [NSNull null]) {
@@ -920,6 +930,7 @@ + (MPPromotion *)MPPromotion:(id)json;
920930
+ (MPTransactionAttributes *)MPTransactionAttributes:(id)json;
921931
+ (MPProduct *)MPProduct:(id)json;
922932
+ (MPCommerceEventAction)MPCommerceEventAction:(id)json;
933+
+ (MPPromotionAction)MPPromotionAction:(id)json;
923934
+ (MPIdentityApiRequest *)MPIdentityApiRequest:(id)json;
924935
+ (MPIdentityApiResult *)MPIdentityApiResult:(id)json;
925936
+ (MPAliasRequest *)MPAliasRequest:(id)json;
@@ -989,7 +1000,7 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
9891000
}
9901001

9911002
+ (MPPromotionContainer *)MPPromotionContainer:(id)json {
992-
MPPromotionAction promotionAction = (MPPromotionAction)[json[@"promotionActionType"] intValue];
1003+
MPPromotionAction promotionAction = [RCTConvert MPPromotionAction:json[@"promotionActionType"]];
9931004
MPPromotionContainer *promotionContainer = [[MPPromotionContainer alloc] initWithAction:promotionAction promotion:nil];
9941005
NSArray *jsonPromotions = json[@"promotions"];
9951006
[jsonPromotions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -1039,6 +1050,20 @@ + (MPProduct *)MPProduct:(id)json {
10391050
return product;
10401051
}
10411052

1053+
+ (MPPromotionAction)MPPromotionAction:(NSNumber *)json {
1054+
// JS `PromotionActionType`: View = 0, Click = 1 (js/index.tsx).
1055+
// Apple `MPPromotionAction`: Click = 0, View = 1 (MPPromotion.h).
1056+
switch ([json intValue]) {
1057+
case 0:
1058+
return MPPromotionActionView;
1059+
case 1:
1060+
return MPPromotionActionClick;
1061+
default:
1062+
// Match Android `convertPromotionActionType`: non-zero → Click
1063+
return MPPromotionActionClick;
1064+
}
1065+
}
1066+
10421067
+ (MPCommerceEventAction)MPCommerceEventAction:(NSNumber *)json {
10431068
int actionInt = [json intValue];
10441069
MPCommerceEventAction action;

0 commit comments

Comments
 (0)