Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configureWithOptions fix for android #336

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ android {

dependencies {
compile 'com.facebook.react:react-native:0.20.+'
compile 'com.google.android.gms:play-services-base:+'
compile 'com.google.android.gms:play-services-base:10.2.1'

compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-analytics:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-auth:10.2.1'
compile 'com.google.firebase:firebase-analytics:10.2.1'
compile 'com.google.firebase:firebase-database:10.2.1'
compile 'com.google.firebase:firebase-storage:10.2.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'
}

242 changes: 102 additions & 140 deletions android/src/main/java/io/fullstack/firestack/FirestackModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import android.content.Context;
import android.util.Log;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand All @@ -24,171 +28,129 @@
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.ServerValue;

interface KeySetterFn {
String setKeyOrDefault(String a, String b);
}

class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
private static final String TAG = "Firestack";
private Context context;
private ReactContext mReactContext;
private FirebaseApp app;

public FirestackModule(ReactApplicationContext reactContext, Context context) {
super(reactContext);
this.context = context;
mReactContext = reactContext;

Log.d(TAG, "New instance");
}

@Override
public String getName() {
return TAG;
}

@ReactMethod
public void configureWithOptions(final ReadableMap params, @Nullable final Callback onComplete) {
Log.i(TAG, "configureWithOptions");

FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(this.context);
private static final String TAG = "Firestack";
private Context context;
private ReactContext mReactContext;
private FirebaseApp app;

enum ConfigurationValues {
API_KEY("apiKey", "APIKey") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setApiKey(value);
}
},
APPLICATION_ID("applicationID", "applicationId") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setApplicationId(value);
}
},
GCM_SENDER_ID("gcmSenderID", "GCMSenderID") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setGcmSenderId(value);
}
},
STORAGE_BUCKET("storageBucket") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setStorageBucket(value);
}
}, DATABASE_URL("databaseUrl", "databaseURL") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setDatabaseUrl(value);
}
};

ConfigurationValues(String... keys) {
this.keys = Arrays.asList(keys);
}

if (defaultOptions == null) {
defaultOptions = new FirebaseOptions.Builder().build();
}
public void apply(ReadableMap params, FirebaseOptions.Builder builder) {
String value = lookUp(params);
Log.d(TAG, "Setting " + this.name() + " from params to: " + value);
doApply(builder, value);
}

KeySetterFn fn = new KeySetterFn() {
public String setKeyOrDefault(
final String key,
final String defaultValue) {
if (params.hasKey(key)) {
// User-set key
final String val = params.getString(key);
Log.d(TAG, "Setting " + key + " from params to: " + val);
return val;
} else if (defaultValue != null && !defaultValue.equals("")) {
Log.d(TAG, "Setting " + key + " from params to: " + defaultValue);
return defaultValue;
} else {
return null;
private String lookUp(ReadableMap params) {
for (String key : keys) {
if (params.hasKey(key)) {
return params.getString(key);
}
}
return "";
}
}
};

String val = fn.setKeyOrDefault("applicationId",
defaultOptions.getApplicationId());
if (val != null) {
builder.setApplicationId(val);
}
protected abstract void doApply(FirebaseOptions.Builder builder, String value);

val = fn.setKeyOrDefault("apiKey",
defaultOptions.getApiKey());
if (val != null) {
builder.setApiKey(val);
private final List<String> keys;
}

val = fn.setKeyOrDefault("gcmSenderID",
defaultOptions.getGcmSenderId());
if (val != null) {
builder.setGcmSenderId(val);
}
public FirestackModule(ReactApplicationContext reactContext, Context context) {
super(reactContext);
this.context = context;
mReactContext = reactContext;

val = fn.setKeyOrDefault("storageBucket",
defaultOptions.getStorageBucket());
if (val != null) {
builder.setStorageBucket(val);
Log.d(TAG, "New instance");
}

val = fn.setKeyOrDefault("databaseURL",
defaultOptions.getDatabaseUrl());
if (val != null) {
builder.setDatabaseUrl(val);
@Override
public String getName() {
return TAG;
}

val = fn.setKeyOrDefault("databaseUrl",
defaultOptions.getDatabaseUrl());
if (val != null) {
builder.setDatabaseUrl(val);
@ReactMethod
public void configureWithOptions(final ReadableMap params, @Nullable final Callback onComplete) {
Log.i(TAG, "configureWithOptions");
try {
app = retrieveOrConfigure(params);
WritableMap resp = Arguments.createMap();
resp.putString("msg", "success");
onComplete.invoke(null, resp);
} catch (Exception ex) {
Log.e(TAG, "ERROR configureWithOptions");
Log.e(TAG, ex.getMessage());
WritableMap resp = Arguments.createMap();
resp.putString("msg", ex.getMessage());
onComplete.invoke(resp);
}
}

val = fn.setKeyOrDefault("clientId",
defaultOptions.getApplicationId());
if (val != null) {
builder.setApplicationId(val);
private FirebaseApp retrieveOrConfigure(ReadableMap params) {
if (FirebaseApp.getApps(context).size() > 0) {
return FirebaseApp.getInstance();
}
return configure(params);
}


// if (params.hasKey("applicationId")) {
// final String applicationId = params.getString("applicationId");
// Log.d(TAG, "Setting applicationId from params " + applicationId);
// builder.setApplicationId(applicationId);
// }
// if (params.hasKey("apiKey")) {
// final String apiKey = params.getString("apiKey");
// Log.d(TAG, "Setting API key from params " + apiKey);
// builder.setApiKey(apiKey);
// }
// if (params.hasKey("APIKey")) {
// final String apiKey = params.getString("APIKey");
// Log.d(TAG, "Setting API key from params " + apiKey);
// builder.setApiKey(apiKey);
// }
// if (params.hasKey("gcmSenderID")) {
// final String gcmSenderID = params.getString("gcmSenderID");
// Log.d(TAG, "Setting gcmSenderID from params " + gcmSenderID );
// builder.setGcmSenderId(gcmSenderID);
// }
// if (params.hasKey("storageBucket")) {
// final String storageBucket = params.getString("storageBucket");
// Log.d(TAG, "Setting storageBucket from params " + storageBucket);
// builder.setStorageBucket(storageBucket);
// }
// if (params.hasKey("databaseURL")) {
// final String databaseURL = params.getString("databaseURL");
// Log.d(TAG, "Setting databaseURL from params " + databaseURL);
// builder.setDatabaseUrl(databaseURL);
// }
// if (params.hasKey("clientID")) {
// final String clientID = params.getString("clientID");
// Log.d(TAG, "Setting clientID from params " + clientID);
// builder.setApplicationId(clientID);
// }

try {
@NonNull
private FirebaseApp configure(ReadableMap params) {
Log.i(TAG, "Configuring app");
if (app == null) {
app = FirebaseApp.initializeApp(this.context, builder.build());
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
for (ConfigurationValues configurationValue : ConfigurationValues.values()) {
configurationValue.apply(params, builder);
}
FirebaseApp firebaseApp = FirebaseApp.initializeApp(this.context, builder.build());
Log.i(TAG, "Configured");

WritableMap resp = Arguments.createMap();
resp.putString("msg", "success");
onComplete.invoke(null, resp);
return firebaseApp;
}
catch (Exception ex){
Log.e(TAG, "ERROR configureWithOptions");
Log.e(TAG, ex.getMessage());

WritableMap resp = Arguments.createMap();
resp.putString("msg", ex.getMessage());

onComplete.invoke(resp);
}
}
@ReactMethod
public void serverValue(@Nullable final Callback onComplete) {
WritableMap timestampMap = Arguments.createMap();
for (Map.Entry<String, String> entry : ServerValue.TIMESTAMP.entrySet()) {
timestampMap.putString(entry.getKey(), entry.getValue());
}

@ReactMethod
public void serverValue(@Nullable final Callback onComplete) {
WritableMap timestampMap = Arguments.createMap();
for (Map.Entry<String, String> entry : ServerValue.TIMESTAMP.entrySet()) {
timestampMap.putString(entry.getKey(), entry.getValue());
WritableMap map = Arguments.createMap();
map.putMap("TIMESTAMP", timestampMap);
onComplete.invoke(null, map);
}

WritableMap map = Arguments.createMap();
map.putMap("TIMESTAMP", timestampMap);
onComplete.invoke(null, map);
}

// Internal helpers
@Override
public void onHostResume() {
Expand All @@ -208,4 +170,4 @@ public void onHostPause() {
public void onHostDestroy() {

}
}
}