|
| 1 | +#import "RNMPRokt.h" |
| 2 | +#if defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle.h>) |
| 3 | + #import <mParticle_Apple_SDK/mParticle.h> |
| 4 | +#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK_NoLocation/mParticle.h>) |
| 5 | + #import <mParticle_Apple_SDK_NoLocation/mParticle.h> |
| 6 | +#else |
| 7 | + #import "mParticle.h" |
| 8 | +#endif |
| 9 | +#if defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle_Apple_SDK-Swift.h>) |
| 10 | + #import <mParticle_Apple_SDK/mParticle_Apple_SDK-Swift.h> |
| 11 | +#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK_NoLocation/mParticle_Apple_SDK-Swift.h>) |
| 12 | + #import <mParticle_Apple_SDK_NoLocation/mParticle_Apple_SDK-Swift.h> |
| 13 | +#else |
| 14 | + #import "mParticle_Apple_SDK-Swift.h" |
| 15 | +#endif |
| 16 | +#import <React/RCTConvert.h> |
| 17 | + |
| 18 | +@implementation RNMPRokt |
| 19 | + |
| 20 | +RCT_EXTERN void RCTRegisterModule(Class); |
| 21 | + |
| 22 | ++ (NSString *)moduleName { |
| 23 | + return @"MPRokt"; |
| 24 | +} |
| 25 | + |
| 26 | ++ (void)load { |
| 27 | + RCTRegisterModule(self); |
| 28 | +} |
| 29 | + |
| 30 | +RCT_EXPORT_METHOD(selectPlacements:(NSString *) identifer attributes:(NSDictionary *)attributes placeholders:(NSDictionary * _Nullable)placeholders roktConfig:(NSDictionary * _Nullable)roktConfig fontFilesMap:(NSDictionary * _Nullable)fontFilesMap) |
| 31 | +{ |
| 32 | + NSMutableDictionary *finalAttributes = [self convertToMutableDictionaryOfStrings:attributes]; |
| 33 | + MPRoktConfig *config = [self buildRoktConfigFromDict:roktConfig]; |
| 34 | + // TODO: Add placeholders and fontFilesMap |
| 35 | + [[[MParticle sharedInstance] rokt] selectPlacements:identifer attributes:finalAttributes]; |
| 36 | +} |
| 37 | + |
| 38 | +- (NSMutableDictionary*)convertToMutableDictionaryOfStrings:(NSDictionary*)attributes |
| 39 | +{ |
| 40 | + NSMutableDictionary *finalAttributes = [attributes mutableCopy]; |
| 41 | + NSArray *keysForNullValues = [finalAttributes allKeysForObject:[NSNull null]]; |
| 42 | + [finalAttributes removeObjectsForKeys:keysForNullValues]; |
| 43 | + |
| 44 | + NSSet *keys = [finalAttributes keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) { |
| 45 | + return ![obj isKindOfClass:[NSString class]]; |
| 46 | + }]; |
| 47 | + |
| 48 | + [finalAttributes removeObjectsForKeys:[keys allObjects]]; |
| 49 | + return finalAttributes; |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +- (MPColorMode)stringToColorMode:(NSString*)colorString |
| 54 | +{ |
| 55 | + if ([colorString isEqualToString:@"light"]) { |
| 56 | + return MPColorModeLight; |
| 57 | + } |
| 58 | + else if ([colorString isEqualToString:@"dark"]) { |
| 59 | + return MPColorModeDark; |
| 60 | + } |
| 61 | + else { |
| 62 | + return MPColorModeSystem; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +- (MPRoktConfig *)buildRoktConfigFromDict:(NSDictionary<NSString *, id> *)configMap { |
| 67 | + MPRoktConfig *config = [[MPRoktConfig alloc] init]; |
| 68 | + BOOL isConfigEmpty = YES; |
| 69 | + |
| 70 | + NSString *colorModeString = configMap[@"colorMode"]; |
| 71 | + if (colorModeString && [colorModeString isKindOfClass:[NSString class]]) { |
| 72 | + if (@available(iOS 12.0, *)) { |
| 73 | + isConfigEmpty = NO; |
| 74 | + if ([colorModeString isEqualToString:@"dark"]) { |
| 75 | + if (@available(iOS 13.0, *)) { |
| 76 | + config.colorMode = MPColorModeDark; |
| 77 | + } |
| 78 | + } else if ([colorModeString isEqualToString:@"light"]) { |
| 79 | + config.colorMode = MPColorModeLight; |
| 80 | + } else { |
| 81 | + // default: "system" |
| 82 | + config.colorMode = MPColorModeSystem; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + NSDictionary *cacheConfigMap = configMap[@"cacheConfig"]; |
| 88 | + if (cacheConfigMap && [cacheConfigMap isKindOfClass:[NSDictionary class]]) { |
| 89 | + isConfigEmpty = NO; |
| 90 | + NSNumber *cacheDuration = cacheConfigMap[@"cacheDurationInSeconds"]; |
| 91 | + if (!cacheDuration) { |
| 92 | + cacheDuration = @0; |
| 93 | + } |
| 94 | + NSDictionary<NSString *, NSString *> *cacheAttributes = cacheConfigMap[@"cacheAttributes"]; |
| 95 | + config.cacheAttributes = cacheAttributes; |
| 96 | + config.cacheDuration = cacheDuration; |
| 97 | + } |
| 98 | + |
| 99 | + return isConfigEmpty ? nil : config; |
| 100 | +} |
| 101 | + |
| 102 | +@end |
0 commit comments