Skip to content

Instantly share code, notes, and snippets.

@alxsimo
Last active December 15, 2016 16:42
Show Gist options
  • Save alxsimo/2ddfa07e054c5e609a988a70e7a883ae to your computer and use it in GitHub Desktop.
Save alxsimo/2ddfa07e054c5e609a988a70e7a883ae to your computer and use it in GitHub Desktop.
[Rx] RxFirebaseRemoteConfig
package com.milanuncios.milanunciosandroid.common.remoteconfig;
import com.alexsimo.toolbelt.optional.Optional;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.milanuncios.milanunciosandroid.BuildConfig;
import java.util.Map;
import rx.AsyncEmitter;
import rx.Observable;
public class RxFirebaseRemoteConfig {
private final Optional<Integer> defaultConfigurationFile;
private final Optional<Map<String, Object>> defaultConfigurationMap;
private FirebaseRemoteConfig remoteConfig;
public RxFirebaseRemoteConfig(FirebaseRemoteConfigSettings settings, Integer defaultConfig) {
this.defaultConfigurationFile = Optional.of(defaultConfig);
this.defaultConfigurationMap = Optional.absent();
init(settings);
}
public RxFirebaseRemoteConfig(FirebaseRemoteConfigSettings settings,
Map<String, Object> defaultConfig) {
this.defaultConfigurationFile = Optional.absent();
this.defaultConfigurationMap = Optional.of(defaultConfig);
init(settings);
}
public Observable<Boolean> refresh() {
return Observable.fromEmitter(emitter -> remoteConfig.fetch(calculateCacheExpiration())
.addOnSuccessListener(success -> refreshSuccess(emitter))
.addOnFailureListener(emitter::onError), AsyncEmitter.BackpressureMode.LATEST);
}
public Observable<Boolean> getFreshBoolean(String key) {
return refresh()
.filter(refreshed -> refreshed)
.flatMap(filtered -> getBoolean(key));
}
public Observable<Boolean> getBoolean(String key) {
return Observable.fromCallable(() -> remoteConfig.getBoolean(key));
}
public Observable<String> getString(String key) {
return Observable.fromCallable(() -> remoteConfig.getString(key));
}
public Observable<Long> getLong(String key) {
return Observable.fromCallable(() -> remoteConfig.getLong(key));
}
private void init(FirebaseRemoteConfigSettings settings) {
remoteConfig = FirebaseRemoteConfig.getInstance();
remoteConfig.setConfigSettings(settings);
defaultConfigurationPrecondition();
setDefaultConfiguration();
}
private void refreshSuccess(AsyncEmitter<Boolean> emitter) {
remoteConfig.activateFetched();
emitter.onNext(true);
emitter.onCompleted();
}
private void defaultConfigurationPrecondition() {
if (noDefaultConfigurationAvailable()) {
throw new IllegalArgumentException(
"You need to specify at least one default configuration parameter");
}
}
private boolean noDefaultConfigurationAvailable() {
return !defaultConfigurationFile.isPresent() && !defaultConfigurationMap.isPresent();
}
private void setDefaultConfiguration() {
if (defaultConfigurationFile.isPresent()) {
remoteConfig.setDefaults(defaultConfigurationFile.get());
} else {
remoteConfig.setDefaults(defaultConfigurationMap.get());
}
}
private long calculateCacheExpiration() {
int ONE_MINUTE = 3600;
long cacheExpiration = ONE_MINUTE;
if (remoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
return cacheExpiration;
}
@FireZenk
Copy link

What is the publ in the last line? Something seems wrong ;-)

@alxsimo
Copy link
Author

alxsimo commented Dec 14, 2016

Done! Thanks for the notice @FireZenk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment