react-native-firebase/ios/RNFirebase/config/RNFirebaseRemoteConfig.m

145 lines
4.9 KiB
Mathematica
Raw Normal View History

#import "RNFirebaseRemoteConfig.h"
#if __has_include(<FirebaseRemoteConfig/FirebaseRemoteConfig.h>)
2017-07-04 12:05:19 +00:00
#import <FirebaseRemoteConfig/FirebaseRemoteConfig.h>
#import <React/RCTConvert.h>
2017-07-19 15:22:00 +00:00
#import <React/RCTUtils.h>
2017-07-04 12:05:19 +00:00
NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus value) {
switch (value) {
case FIRRemoteConfigFetchStatusNoFetchYet:
2017-05-23 13:08:59 +00:00
return @"config/no_fetch_yet";
case FIRRemoteConfigFetchStatusSuccess:
return @"config/success";
case FIRRemoteConfigFetchStatusThrottled:
2017-05-23 13:08:59 +00:00
return @"config/throttled";
default:
2017-05-23 13:08:59 +00:00
return @"config/failure";
}
}
NSString *convertFIRRemoteConfigFetchStatusToNSStringDescription(FIRRemoteConfigFetchStatus value) {
switch (value) {
case FIRRemoteConfigFetchStatusThrottled:
return @"fetch() operation cannot be completed successfully, due to throttling.";
case FIRRemoteConfigFetchStatusNoFetchYet:
default:
return @"fetch() operation cannot be completed successfully.";
}
}
2017-07-04 12:05:19 +00:00
NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) {
switch (value) {
case FIRRemoteConfigSourceDefault:
2017-05-23 13:08:59 +00:00
return @"default";
case FIRRemoteConfigSourceRemote:
return @"remote";
default:
2017-05-23 13:08:59 +00:00
return @"static";
}
}
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)};
}
@implementation RNFirebaseRemoteConfig
RCT_EXPORT_MODULE(RNFirebaseRemoteConfig);
RCT_EXPORT_METHOD(enableDeveloperMode) {
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
[FIRRemoteConfig remoteConfig].configSettings = remoteConfigSettings;
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(fetch:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
[[FIRRemoteConfig remoteConfig] fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) {
if (error) {
reject(convertFIRRemoteConfigFetchStatusToNSString(status), convertFIRRemoteConfigFetchStatusToNSStringDescription(status), error);
} else {
resolve(nil);
}
}];
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(fetchWithExpirationDuration:
(nonnull
NSNumber *)expirationDuration
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[[FIRRemoteConfig remoteConfig] fetchWithExpirationDuration:expirationDuration.doubleValue completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *__nullable error) {
if (error) {
reject(convertFIRRemoteConfigFetchStatusToNSString(status), convertFIRRemoteConfigFetchStatusToNSStringDescription(status), error);
} else {
resolve(nil);
}
}];
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(activateFetched:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
BOOL status = [[FIRRemoteConfig remoteConfig] activateFetched];
resolve(@(status));
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(getValue:
(NSString *) key
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
FIRRemoteConfigValue *value = [[FIRRemoteConfig remoteConfig] configValueForKey:key];
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];
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-23 13:08:59 +00:00
resolve(valuesArray);
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(getKeysByPrefix:
(NSString *) prefix
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
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-23 13:08:59 +00:00
resolve(keysArray);
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(setDefaults:
(NSDictionary *) defaults) {
[[FIRRemoteConfig remoteConfig] setDefaults:defaults];
}
2017-07-04 12:05:19 +00:00
RCT_EXPORT_METHOD(setDefaultsFromResource:
(NSString *) fileName) {
[[FIRRemoteConfig remoteConfig] setDefaultsFromPlistFileName:fileName];
}
@end
#else
@implementation RNFirebaseRemoteConfig
@end
#endif