2017-07-12 14:49:33 +00:00
|
|
|
#import "RNFirebaseRemoteConfig.h"
|
2017-05-19 07:49:49 +00:00
|
|
|
|
2017-05-25 14:24:53 +00:00
|
|
|
#if __has_include(<FirebaseRemoteConfig/FirebaseRemoteConfig.h>)
|
2017-07-04 12:05:19 +00:00
|
|
|
|
2017-08-17 16:25:51 +00:00
|
|
|
#import <FirebaseRemoteConfig/FirebaseRemoteConfig.h>
|
2017-05-31 08:54:02 +00:00
|
|
|
#import <React/RCTConvert.h>
|
2017-07-19 15:22:00 +00:00
|
|
|
#import <React/RCTUtils.h>
|
2017-05-19 07:49:49 +00:00
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus value) {
|
|
|
|
switch (value) {
|
2017-05-19 07:49:49 +00:00
|
|
|
case FIRRemoteConfigFetchStatusNoFetchYet:
|
2017-05-23 13:08:59 +00:00
|
|
|
return @"config/no_fetch_yet";
|
2017-07-29 22:59:17 +00:00
|
|
|
case FIRRemoteConfigFetchStatusSuccess:
|
|
|
|
return @"config/success";
|
2017-05-19 07:49:49 +00:00
|
|
|
case FIRRemoteConfigFetchStatusThrottled:
|
2017-05-23 13:08:59 +00:00
|
|
|
return @"config/throttled";
|
2017-05-19 07:49:49 +00:00
|
|
|
default:
|
2017-05-23 13:08:59 +00:00
|
|
|
return @"config/failure";
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) {
|
|
|
|
switch (value) {
|
2017-05-19 07:49:49 +00:00
|
|
|
case FIRRemoteConfigSourceDefault:
|
2017-05-23 13:08:59 +00:00
|
|
|
return @"default";
|
|
|
|
case FIRRemoteConfigSourceRemote:
|
|
|
|
return @"remote";
|
2017-05-19 07:49:49 +00:00
|
|
|
default:
|
2017-05-23 13:08:59 +00:00
|
|
|
return @"static";
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
NSDictionary *convertFIRRemoteConfigValueToNSDictionary(FIRRemoteConfigValue *value) {
|
|
|
|
return @{@"stringValue": value.stringValue ?: [NSNull null], @"numberValue": value.numberValue ?: [NSNull null], @"dataValue": value.dataValue ? [value.dataValue base64EncodedStringWithOptions:0] : [NSNull null], @"boolValue": @(value.boolValue), @"source": convertFIRRemoteConfigSourceToNSString(value.source)};
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 14:49:33 +00:00
|
|
|
@implementation RNFirebaseRemoteConfig
|
2017-05-19 07:49:49 +00:00
|
|
|
|
2017-07-12 14:49:33 +00:00
|
|
|
RCT_EXPORT_MODULE(RNFirebaseRemoteConfig);
|
2017-05-19 07:49:49 +00:00
|
|
|
|
2017-05-25 14:24:53 +00:00
|
|
|
RCT_EXPORT_METHOD(enableDeveloperMode) {
|
2017-05-19 07:49:49 +00:00
|
|
|
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
|
2017-05-31 08:54:02 +00:00
|
|
|
[FIRRemoteConfig remoteConfig].configSettings = remoteConfigSettings;
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(fetch:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject) {
|
2017-05-31 08:54:02 +00:00
|
|
|
[[FIRRemoteConfig remoteConfig] fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) {
|
2017-05-19 07:49:49 +00:00
|
|
|
if (error) {
|
|
|
|
reject(convertFIRRemoteConfigFetchStatusToNSString(status), error.localizedDescription, error);
|
|
|
|
} else {
|
|
|
|
resolve(convertFIRRemoteConfigFetchStatusToNSString(status));
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(fetchWithExpirationDuration:
|
|
|
|
(nonnull
|
|
|
|
NSNumber *)expirationDuration
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-05-31 08:54:02 +00:00
|
|
|
[[FIRRemoteConfig remoteConfig] fetchWithExpirationDuration:expirationDuration.doubleValue completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) {
|
2017-05-19 07:49:49 +00:00
|
|
|
if (error) {
|
|
|
|
reject(convertFIRRemoteConfigFetchStatusToNSString(status), error.localizedDescription, error);
|
|
|
|
} else {
|
|
|
|
resolve(convertFIRRemoteConfigFetchStatusToNSString(status));
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(activateFetched:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject) {
|
2017-05-31 08:54:02 +00:00
|
|
|
BOOL status = [[FIRRemoteConfig remoteConfig] activateFetched];
|
2017-05-23 10:51:16 +00:00
|
|
|
resolve(@(status));
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(getValue:
|
|
|
|
(NSString *) key
|
|
|
|
resolver:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject) {
|
2017-05-31 08:54:02 +00:00
|
|
|
FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key];
|
2017-05-19 07:49:49 +00:00
|
|
|
resolve(convertFIRRemoteConfigValueToNSDictionary(value));
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(getValues:
|
|
|
|
(NSArray *) keys
|
|
|
|
resolver:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject) {
|
2017-05-23 13:08:59 +00:00
|
|
|
NSMutableArray *valuesArray = [[NSMutableArray alloc] init];
|
2017-05-19 07:49:49 +00:00
|
|
|
for (NSString *key in keys) {
|
2017-07-04 12:05:19 +00:00
|
|
|
FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key];
|
2017-05-23 13:08:59 +00:00
|
|
|
[valuesArray addObject:convertFIRRemoteConfigValueToNSDictionary(value)];
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
2017-05-23 13:08:59 +00:00
|
|
|
resolve(valuesArray);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(getKeysByPrefix:
|
|
|
|
(NSString *) prefix
|
|
|
|
resolver:
|
|
|
|
(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:
|
|
|
|
(RCTPromiseRejectBlock) reject) {
|
2017-05-31 08:54:02 +00:00
|
|
|
NSSet *keys = [[FIRRemoteConfig remoteConfig] keysWithPrefix:prefix];
|
2017-05-23 13:08:59 +00:00
|
|
|
NSMutableArray *keysArray = [[NSMutableArray alloc] init];
|
|
|
|
for (NSString *key in keys) {
|
|
|
|
[keysArray addObject:key];
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
2017-05-23 13:08:59 +00:00
|
|
|
resolve(keysArray);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(setDefaults:
|
|
|
|
(NSDictionary *) defaults) {
|
2017-05-31 08:54:02 +00:00
|
|
|
[[FIRRemoteConfig remoteConfig] setDefaults:defaults];
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:05:19 +00:00
|
|
|
RCT_EXPORT_METHOD(setDefaultsFromResource:
|
|
|
|
(NSString *) fileName) {
|
2017-05-31 08:54:02 +00:00
|
|
|
[[FIRRemoteConfig remoteConfig] setDefaultsFromPlistFileName:fileName];
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2017-05-25 14:24:53 +00:00
|
|
|
|
|
|
|
#else
|
2017-08-17 10:48:53 +00:00
|
|
|
@implementation RNFirebaseRemoteConfig
|
2017-05-25 14:24:53 +00:00
|
|
|
@end
|
2017-05-31 08:54:02 +00:00
|
|
|
#endif
|