2222import com .google .firebase .FirebaseApp ;
2323import com .google .firebase .FirebaseOptions ;
2424
25+ interface KeySetterFn {
26+ String setKeyOrDefault (String a , String b );
27+ }
28+
2529class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
2630 private static final String TAG = "Firestack" ;
2731 private Context context ;
@@ -42,47 +46,109 @@ public String getName() {
4246 }
4347
4448 @ ReactMethod
45- public void configureWithOptions (ReadableMap params , @ Nullable final Callback onComplete ) {
49+ public void configureWithOptions (final ReadableMap params , @ Nullable final Callback onComplete ) {
4650 Log .i (TAG , "configureWithOptions" );
4751
4852 FirebaseOptions .Builder builder = new FirebaseOptions .Builder ();
53+ FirebaseOptions defaultOptions = FirebaseOptions .fromResource (this .context );
54+
55+ KeySetterFn fn = new KeySetterFn () {
56+ public String setKeyOrDefault (
57+ final String key ,
58+ final String defaultValue ) {
59+ if (params .hasKey (key )) {
60+ // User-set key
61+ final String val = params .getString (key );
62+ Log .d (TAG , "Setting " + key + " from params to: " + val );
63+ return val ;
64+ } else if (defaultValue != null && defaultValue != "" ) {
65+ Log .d (TAG , "Setting " + key + " from params to: " + defaultValue );
66+ return defaultValue ;
67+ } else {
68+ return null ;
69+ }
70+ }
71+ };
4972
50- if ( params . hasKey ("applicationId" )) {
51- final String applicationId = params . getString ( "applicationId" );
52- Log . d ( TAG , "Setting applicationId from params " + applicationId );
53- builder .setApplicationId (applicationId );
73+ String val = fn . setKeyOrDefault ("applicationId" ,
74+ defaultOptions . getApplicationId () );
75+ if ( val != null ) {
76+ builder .setApplicationId (val );
5477 }
55- if (params .hasKey ("apiKey" )) {
56- final String apiKey = params .getString ("apiKey" );
57- Log .d (TAG , "Setting API key from params " + apiKey );
58- builder .setApiKey (apiKey );
78+
79+ val = fn .setKeyOrDefault ("apiKey" ,
80+ defaultOptions .getApiKey ());
81+ if (val != null ) {
82+ builder .setApiKey (val );
5983 }
60- if (params .hasKey ("APIKey" )) {
61- final String apiKey = params .getString ("APIKey" );
62- Log .d (TAG , "Setting API key from params " + apiKey );
63- builder .setApiKey (apiKey );
84+
85+ val = fn .setKeyOrDefault ("gcmSenderID" ,
86+ defaultOptions .getGcmSenderId ());
87+ if (val != null ) {
88+ builder .setGcmSenderId (val );
6489 }
65- if (params .hasKey ("gcmSenderID" )) {
66- final String gcmSenderID = params .getString ("gcmSenderID" );
67- Log .d (TAG , "Setting gcmSenderID from params " + gcmSenderID );
68- builder .setGcmSenderId (gcmSenderID );
90+
91+ val = fn .setKeyOrDefault ("storageBucket" ,
92+ defaultOptions .getStorageBucket ());
93+ if (val != null ) {
94+ builder .setStorageBucket (val );
6995 }
70- if (params .hasKey ("storageBucket" )) {
71- final String storageBucket = params .getString ("storageBucket" );
72- Log .d (TAG , "Setting storageBucket from params " + storageBucket );
73- builder .setStorageBucket (storageBucket );
96+
97+ val = fn .setKeyOrDefault ("databaseURL" ,
98+ defaultOptions .getDatabaseUrl ());
99+ if (val != null ) {
100+ builder .setDatabaseUrl (val );
74101 }
75- if (params .hasKey ("databaseURL" )) {
76- final String databaseURL = params .getString ("databaseURL" );
77- Log .d (TAG , "Setting databaseURL from params " + databaseURL );
78- builder .setDatabaseUrl (databaseURL );
102+
103+ val = fn .setKeyOrDefault ("databaseUrl" ,
104+ defaultOptions .getDatabaseUrl ());
105+ if (val != null ) {
106+ builder .setDatabaseUrl (val );
79107 }
80- if (params .hasKey ("clientID" )) {
81- final String clientID = params .getString ("clientID" );
82- Log .d (TAG , "Setting clientID from params " + clientID );
83- builder .setApplicationId (clientID );
108+
109+ val = fn .setKeyOrDefault ("clientId" ,
110+ defaultOptions .getApplicationId ());
111+ if (val != null ) {
112+ builder .setApplicationId (val );
84113 }
85114
115+
116+ // if (params.hasKey("applicationId")) {
117+ // final String applicationId = params.getString("applicationId");
118+ // Log.d(TAG, "Setting applicationId from params " + applicationId);
119+ // builder.setApplicationId(applicationId);
120+ // }
121+ // if (params.hasKey("apiKey")) {
122+ // final String apiKey = params.getString("apiKey");
123+ // Log.d(TAG, "Setting API key from params " + apiKey);
124+ // builder.setApiKey(apiKey);
125+ // }
126+ // if (params.hasKey("APIKey")) {
127+ // final String apiKey = params.getString("APIKey");
128+ // Log.d(TAG, "Setting API key from params " + apiKey);
129+ // builder.setApiKey(apiKey);
130+ // }
131+ // if (params.hasKey("gcmSenderID")) {
132+ // final String gcmSenderID = params.getString("gcmSenderID");
133+ // Log.d(TAG, "Setting gcmSenderID from params " + gcmSenderID );
134+ // builder.setGcmSenderId(gcmSenderID);
135+ // }
136+ // if (params.hasKey("storageBucket")) {
137+ // final String storageBucket = params.getString("storageBucket");
138+ // Log.d(TAG, "Setting storageBucket from params " + storageBucket);
139+ // builder.setStorageBucket(storageBucket);
140+ // }
141+ // if (params.hasKey("databaseURL")) {
142+ // final String databaseURL = params.getString("databaseURL");
143+ // Log.d(TAG, "Setting databaseURL from params " + databaseURL);
144+ // builder.setDatabaseUrl(databaseURL);
145+ // }
146+ // if (params.hasKey("clientID")) {
147+ // final String clientID = params.getString("clientID");
148+ // Log.d(TAG, "Setting clientID from params " + clientID);
149+ // builder.setApplicationId(clientID);
150+ // }
151+
86152 try {
87153 Log .i (TAG , "Configuring app" );
88154 if (app == null ) {
0 commit comments