From 25e8bb6d5aa63edfdc24e7918e17a736b43ad822 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Tue, 23 May 2017 14:08:59 +0100 Subject: [PATCH] [config] Standardize ios & android --- .../config/RNFirebaseRemoteConfig.java | 10 +-- docs/modules/config.md | 20 ++++- ios/RNFirebase/RNFirebaseRemoteConfig.m | 40 +++++----- lib/modules/{remoteConfig => config}/index.js | 74 ++++++++++--------- 4 files changed, 81 insertions(+), 63 deletions(-) rename lib/modules/{remoteConfig => config}/index.js (64%) diff --git a/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java b/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java index 83d0a07e..daeb68ec 100644 --- a/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java +++ b/android/src/main/java/io/invertase/firebase/config/RNFirebaseRemoteConfig.java @@ -63,8 +63,8 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule { } @ReactMethod - public void fetchWithExpirationDuration(long expirationDuration, final Promise promise) { - fetchInternal(promise, true, expirationDuration); + public void fetchWithExpirationDuration(double expirationDuration, final Promise promise) { + fetchInternal(promise, true, (long) expirationDuration); } @ReactMethod @@ -74,13 +74,13 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule { } @ReactMethod - public void configValueForKey(String key, final Promise promise) { + public void getValue(String key, final Promise promise) { FirebaseRemoteConfigValue value = FirebaseRemoteConfig.getInstance().getValue(key); promise.resolve(convertRemoteConfigValue(value)); } @ReactMethod - public void configValuesForKeys(ReadableArray keys, final Promise promise) { + public void getValues(ReadableArray keys, final Promise promise) { WritableArray array = Arguments.createArray(); List keysList = Utils.recursivelyDeconstructReadableArray(keys); @@ -93,7 +93,7 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule { } @ReactMethod - public void keysWithPrefix(String prefix, final Promise promise) { + public void getKeysByPrefix(String prefix, final Promise promise) { Set keys = FirebaseRemoteConfig.getInstance().getKeysByPrefix(prefix); WritableArray array = Arguments.createArray(); diff --git a/docs/modules/config.md b/docs/modules/config.md index 4c0ad169..483e3095 100644 --- a/docs/modules/config.md +++ b/docs/modules/config.md @@ -23,10 +23,12 @@ Any values in the defaults but not on Firebase will be untouched. }); ``` -### fetch(): `Promise` +### fetch(duration?: `number`): `Promise` -Fetches the remote config data from Firebase, defined in the dashboard. To have fetched data available in the active config, use -`activateFetched`. +Fetches the remote config data from Firebase, defined in the dashboard. +If duration is defined (seconds), data will be locally cached for this duration. + +The default duration is 43200 seconds (12 hours). To force a cache refresh call the method with a duration of 0. Thrown errors can be one of the following: * config/failure - Config fetch failed. @@ -111,6 +113,18 @@ Gets multiple values by key. Returns an object of keys with the same object retu .catch(console.error); ``` +### getKeysByPrefix(prefix?: `String`): `Promise >` + +Returns all keys as an array by a prefix. If no prefix is defined all keys are returned. + +```js +firebase.config() + .getKeysByPrefix() + .then((keys) => { + + }); +``` + ## Usage ```js diff --git a/ios/RNFirebase/RNFirebaseRemoteConfig.m b/ios/RNFirebase/RNFirebaseRemoteConfig.m index 55f118b9..c41e6727 100644 --- a/ios/RNFirebase/RNFirebaseRemoteConfig.m +++ b/ios/RNFirebase/RNFirebaseRemoteConfig.m @@ -17,29 +17,25 @@ NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus { switch(value){ case FIRRemoteConfigFetchStatusNoFetchYet: - return @"remoteConfigFetchStatusNoFetchYet"; - case FIRRemoteConfigFetchStatusSuccess: - return @"remoteConfigFetchStatusSuccess"; + return @"config/no_fetch_yet"; case FIRRemoteConfigFetchStatusFailure: - return @"remoteConfigFetchStatusFailure"; + return @"config/failure"; case FIRRemoteConfigFetchStatusThrottled: - return @"remoteConfigFetchStatusThrottled"; + return @"config/throttled"; default: - return @"remoteConfigFetchStatusFailure"; + return @"config/failure"; } } NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) { switch(value) { - case FIRRemoteConfigSourceRemote: - return @"remoteConfigSourceRemote"; case FIRRemoteConfigSourceDefault: - return @"remoteConfigSourceDefault"; - case FIRRemoteConfigSourceStatic: - return @"remoteConfigSourceStatic"; + return @"default"; + case FIRRemoteConfigSourceRemote: + return @"remote"; default: - return @"remoteConfigSourceStatic"; + return @"static"; } } @@ -112,7 +108,7 @@ RCT_EXPORT_METHOD(activateFetched:(RCTPromiseResolveBlock)resolve resolve(@(status)); } -RCT_EXPORT_METHOD(configValueForKey:(NSString *)key +RCT_EXPORT_METHOD(getValue:(NSString *)key resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { @@ -120,28 +116,28 @@ RCT_EXPORT_METHOD(configValueForKey:(NSString *)key resolve(convertFIRRemoteConfigValueToNSDictionary(value)); } -RCT_EXPORT_METHOD(configValuesForKeys:(NSArray *)keys +RCT_EXPORT_METHOD(getValues:(NSArray *)keys resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - NSMutableDictionary *res = [[NSMutableDictionary alloc] init]; + NSMutableArray *valuesArray = [[NSMutableArray alloc] init]; for (NSString *key in keys) { FIRRemoteConfigValue *value = [self.remoteConfig configValueForKey:key]; - res[key] = convertFIRRemoteConfigValueToNSDictionary(value); + [valuesArray addObject:convertFIRRemoteConfigValueToNSDictionary(value)]; } - resolve(res); + resolve(valuesArray); } -RCT_EXPORT_METHOD(keysWithPrefix:(NSString *)prefix +RCT_EXPORT_METHOD(getKeysByPrefix:(NSString *)prefix resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { NSSet *keys = [self.remoteConfig keysWithPrefix:prefix]; - if (keys.count) { - resolve(keys); - } else { - reject(@"no_keys_matching_prefix", @"There are no keys that match that prefix", nil); + NSMutableArray *keysArray = [[NSMutableArray alloc] init]; + for (NSString *key in keys) { + [keysArray addObject:key]; } + resolve(keysArray); } RCT_EXPORT_METHOD(setDefaults:(NSDictionary *)defaults) diff --git a/lib/modules/remoteConfig/index.js b/lib/modules/config/index.js similarity index 64% rename from lib/modules/remoteConfig/index.js rename to lib/modules/config/index.js index a5edc54a..b8acf788 100644 --- a/lib/modules/remoteConfig/index.js +++ b/lib/modules/config/index.js @@ -19,6 +19,24 @@ export default class RemoteConfig extends Base { this.developerModeEnabled = false; } + /** + * Converts a native map to single JS value + * @param nativeValue + * @returns {*} + * @private + */ + _nativeValueToJS(nativeValue) { + return { + source: nativeValue.source, + val() { + if (nativeValue.boolValue !== null && (nativeValue.stringValue === 'true' || nativeValue.stringValue === 'false' || nativeValue.stringValue === null)) return nativeValue.boolValue; + if (nativeValue.numberValue !== null && (nativeValue.stringValue == null || nativeValue.stringValue === '' || `${nativeValue.numberValue}` === nativeValue.stringValue)) return nativeValue.numberValue; + if (nativeValue.dataValue !== nativeValue.stringValue && (nativeValue.stringValue == null || nativeValue.stringValue === '')) return nativeValue.dataValue; + return nativeValue.stringValue; + }, + }; + } + /** * Enable Remote Config developer mode to allow for frequent refreshes of the cache */ @@ -26,7 +44,7 @@ export default class RemoteConfig extends Base { if (!this.developerModeEnabled) { this.log.debug('Enabled developer mode'); FirebaseRemoteConfig.enableDeveloperMode(); - this.developerModeEnabled = true + this.developerModeEnabled = true; } } @@ -34,36 +52,16 @@ export default class RemoteConfig extends Base { * Fetches Remote Config data * Call activateFetched to make fetched data available in app * @returns {*|Promise.}: - * One of - * - remoteConfitFetchStatusSuccess - * - remoteConfitFetchStatusFailure - * - remoteConfitFetchStatusThrottled - * rejects on remoteConfitFetchStatusFailure and remoteConfitFetchStatusThrottled - * resolves on remoteConfitFetchStatusSuccess */ - fetch() { + fetch(expiration?: number) { + if (expiration !== undefined) { + this.log.debug(`Fetching remote config data with expiration ${expiration.toString()}`); + return FirebaseRemoteConfig.fetchWithExpirationDuration(expiration); + } this.log.debug('Fetching remote config data'); return FirebaseRemoteConfig.fetch(); } - /** - * Fetches Remote Config data and sets a duration that specifies how long config data lasts. - * Call activateFetched to make fetched data available - * @param expiration: Duration that defines how long fetched config data is available, in - * seconds. When the config data expires, a new fetch is required. - * @returns {*|Promise.} - * One of - * - remoteConfitFetchStatusSuccess - * - remoteConfitFetchStatusFailure - * - remoteConfitFetchStatusThrottled - * rejects on remoteConfitFetchStatusFailure and remoteConfitFetchStatusThrottled - * resolves on remoteConfitFetchStatusSuccess - */ - fetchWithExpirationDuration(expiration: Number) { - this.log.debug(`Fetching remote config data with expiration ${expiration.toString()}`); - return FirebaseRemoteConfig.fetchWithExpirationDuration(expiration); - } - /** * Applies Fetched Config data to the Active Config * @returns {*|Promise.} @@ -88,13 +86,15 @@ export default class RemoteConfig extends Base { * "source" : OneOf(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * } */ - configValueForKey(key: String) { - return FirebaseRemoteConfig.configValueForKey(key); + getValue(key: String) { + return FirebaseRemoteConfig + .getValue(key || '') + .then(this._nativeValueToJS); } /** * Gets the config value of the default namespace. - * @param key: Config key + * @param keys: Config key * @returns {*|Promise.}, will always resolve. * Result will be a dictionary of key and config objects * Object looks like @@ -106,8 +106,16 @@ export default class RemoteConfig extends Base { * "source" : OneOf(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * } */ - configValuesForKeys(keys: Array) { - return FirebaseRemoteConfig.configValuesForKeys(keys); + getValues(keys: Array) { + return FirebaseRemoteConfig + .getValues(keys || []) + .then((nativeValues) => { + const values = {}; + for (let i = 0, len = keys.length; i < len; i++) { + values[keys[i]] = this._nativeValueToJS(nativeValues[i]); + } + return values; + }); } /** @@ -115,8 +123,8 @@ export default class RemoteConfig extends Base { * @param prefix: The key prefix to look for. If prefix is nil or empty, returns all the keys. * @returns {*|Promise.>} */ - keysWithPrefix(prefix: String) { - return FirebaseRemoteConfig.keysWithPrefix(prefix); + getKeysByPrefix(prefix?: String) { + return FirebaseRemoteConfig.getKeysByPrefix(prefix); } /**