Skip to content

Commit 3676612

Browse files
authored
Merge pull request #241 from Salakar/master
[v3] updates from fork
2 parents 5db5e3a + 3845a12 commit 3676612

20 files changed

+973
-845
lines changed

android/src/main/java/io/fullstack/firestack/FirestackMessagingService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
1818
Log.d(TAG, "Remote message received");
1919
// debug
2020
Log.d(TAG, "From: " + remoteMessage.getFrom());
21+
2122
if (remoteMessage.getData().size() > 0) {
2223
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
2324
}
25+
2426
if (remoteMessage.getNotification() != null) {
2527
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
2628
}
27-
if (remoteMessage.getNotification() != null) {
2829

29-
}
3030
Intent i = new Intent(FirestackMessaging.INTENT_NAME_NOTIFICATION);
3131
i.putExtra("data", remoteMessage);
3232
sendOrderedBroadcast(i, null);

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package io.fullstack.firestack;
22

3-
import java.util.Date;
4-
import java.util.HashMap;
53
import java.util.Map;
4+
import java.util.HashMap;
65

76
import android.util.Log;
87
import android.content.Context;
98
import android.support.annotation.Nullable;
109

10+
import com.facebook.react.bridge.Callback;
1111
import com.facebook.react.bridge.Arguments;
12+
import com.facebook.react.bridge.ReadableMap;
13+
import com.facebook.react.bridge.WritableMap;
14+
import com.facebook.react.bridge.ReactMethod;
1215
import com.facebook.react.bridge.LifecycleEventListener;
1316
import com.facebook.react.bridge.ReactApplicationContext;
1417
import com.facebook.react.bridge.ReactContextBaseJavaModule;
15-
import com.facebook.react.bridge.ReactMethod;
16-
import com.facebook.react.bridge.Callback;
17-
import com.facebook.react.bridge.WritableMap;
18-
import com.facebook.react.bridge.ReadableMap;
19-
import com.facebook.react.bridge.ReactContext;
2018

2119
import com.google.android.gms.common.ConnectionResult;
2220
import com.google.android.gms.common.GoogleApiAvailability;
@@ -32,16 +30,10 @@ interface KeySetterFn {
3230
@SuppressWarnings("WeakerAccess")
3331
public class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
3432
private static final String TAG = "Firestack";
35-
private Context context;
36-
private ReactContext mReactContext;
3733
private FirebaseApp app;
3834

39-
public FirestackModule(ReactApplicationContext reactContext, Context context) {
35+
public FirestackModule(ReactApplicationContext reactContext) {
4036
super(reactContext);
41-
this.context = context;
42-
mReactContext = reactContext;
43-
44-
Log.d(TAG, "New instance");
4537
}
4638

4739
@Override
@@ -69,7 +61,7 @@ public void configureWithOptions(final ReadableMap params, @Nullable final Callb
6961
Log.i(TAG, "configureWithOptions");
7062

7163
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
72-
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(this.context);
64+
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(getReactApplicationContext().getBaseContext());
7365

7466
if (defaultOptions == null) {
7567
defaultOptions = new FirebaseOptions.Builder().build();
@@ -154,7 +146,7 @@ public String setKeyOrDefault(
154146
try {
155147
Log.i(TAG, "Configuring app");
156148
if (app == null) {
157-
app = FirebaseApp.initializeApp(this.context, builder.build());
149+
app = FirebaseApp.initializeApp(getReactApplicationContext().getBaseContext(), builder.build());
158150
}
159151
Log.i(TAG, "Configured");
160152

@@ -189,14 +181,14 @@ public void serverValue(@Nullable final Callback onComplete) {
189181
public void onHostResume() {
190182
WritableMap params = Arguments.createMap();
191183
params.putBoolean("isForground", true);
192-
Utils.sendEvent(mReactContext, "FirestackAppState", params);
184+
Utils.sendEvent(getReactApplicationContext(), "FirestackAppState", params);
193185
}
194186

195187
@Override
196188
public void onHostPause() {
197189
WritableMap params = Arguments.createMap();
198190
params.putBoolean("isForground", false);
199-
Utils.sendEvent(mReactContext, "FirestackAppState", params);
191+
Utils.sendEvent(getReactApplicationContext(), "FirestackAppState", params);
200192
}
201193

202194
@Override
@@ -208,6 +200,8 @@ public void onHostDestroy() {
208200
public Map<String, Object> getConstants() {
209201
final Map<String, Object> constants = new HashMap<>();
210202
constants.put("googleApiAvailability", getPlayServicesStatus());
203+
204+
// TODO remove once this has been moved on ios
211205
constants.put("serverValueTimestamp", ServerValue.TIMESTAMP);
212206
return constants;
213207
}

android/src/main/java/io/fullstack/firestack/FirestackPackage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public FirestackPackage() {
3232
@Override
3333
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
3434
List<NativeModule> modules = new ArrayList<>();
35-
modules.add(new FirestackModule(reactContext, reactContext.getBaseContext()));
35+
modules.add(new FirestackModule(reactContext));
3636
modules.add(new FirestackAuth(reactContext));
3737
modules.add(new FirestackDatabase(reactContext));
3838
modules.add(new FirestackAnalytics(reactContext));

android/src/main/java/io/fullstack/firestack/Utils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.facebook.react.bridge.ReadableMap;
1414
import com.facebook.react.bridge.ReactContext;
1515
import com.facebook.react.bridge.WritableArray;
16+
import com.facebook.react.bridge.WritableNativeArray;
1617
import com.facebook.react.modules.core.DeviceEventManagerModule;
1718

1819
import com.facebook.react.bridge.ReadableType;
@@ -82,8 +83,12 @@ public static WritableMap dataSnapshotToMap(
8283
data.putString("value", null);
8384
}
8485
} else {
85-
WritableMap valueMap = Utils.castSnapshotValue(dataSnapshot);
86-
data.putMap("value", valueMap);
86+
Object value = Utils.castSnapshotValue(dataSnapshot);
87+
if (value instanceof WritableNativeArray) {
88+
data.putArray("value", (WritableArray) value);
89+
} else {
90+
data.putMap("value", (WritableMap) value);
91+
}
8792
}
8893

8994
// Child keys
Lines changed: 11 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
package io.fullstack.firestack.analytics;
22

3-
import java.util.Map;
43
import android.util.Log;
5-
import android.os.Bundle;
64
import android.app.Activity;
5+
import android.support.annotation.Nullable;
76

7+
import com.facebook.react.bridge.Arguments;
88
import com.facebook.react.bridge.ReactMethod;
99
import com.facebook.react.bridge.ReadableMap;
1010
import com.google.firebase.analytics.FirebaseAnalytics;
1111
import com.facebook.react.bridge.ReactApplicationContext;
1212
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1313

14-
import io.fullstack.firestack.Utils;
1514

1615
public class FirestackAnalytics extends ReactContextBaseJavaModule {
1716

1817
private static final String TAG = "FirestackAnalytics";
1918

20-
private ReactApplicationContext context;
21-
private FirebaseAnalytics mFirebaseAnalytics;
22-
2319
public FirestackAnalytics(ReactApplicationContext reactContext) {
2420
super(reactContext);
25-
context = reactContext;
2621
Log.d(TAG, "New instance");
27-
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this.context);
2822
}
2923

3024
/**
@@ -37,11 +31,8 @@ public String getName() {
3731
}
3832

3933
@ReactMethod
40-
public void logEvent(final String name, final ReadableMap params) {
41-
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(params);
42-
final Bundle bundle = makeEventBundle(name, m);
43-
Log.d(TAG, "Logging event " + name);
44-
mFirebaseAnalytics.logEvent(name, bundle);
34+
public void logEvent(final String name, @Nullable final ReadableMap params) {
35+
FirebaseAnalytics.getInstance(getReactApplicationContext()).logEvent(name, Arguments.toBundle(params));
4536
}
4637

4738
/**
@@ -50,7 +41,7 @@ public void logEvent(final String name, final ReadableMap params) {
5041
*/
5142
@ReactMethod
5243
public void setAnalyticsCollectionEnabled(final Boolean enabled) {
53-
mFirebaseAnalytics.setAnalyticsCollectionEnabled(enabled);
44+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setAnalyticsCollectionEnabled(enabled);
5445
}
5546

5647
/**
@@ -62,12 +53,12 @@ public void setAnalyticsCollectionEnabled(final Boolean enabled) {
6253
public void setCurrentScreen(final String screenName, final String screenClassOverride) {
6354
final Activity activity = getCurrentActivity();
6455
if (activity != null) {
65-
Log.d(TAG, "setCurrentScreen " + screenName + " - " + screenClassOverride);
6656
// needs to be run on main thread
57+
Log.d(TAG, "setCurrentScreen " + screenName + " - " + screenClassOverride);
6758
activity.runOnUiThread(new Runnable() {
6859
@Override
6960
public void run() {
70-
mFirebaseAnalytics.setCurrentScreen(activity, screenName, screenClassOverride);
61+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setCurrentScreen(activity, screenName, screenClassOverride);
7162
}
7263
});
7364
}
@@ -79,7 +70,7 @@ public void run() {
7970
*/
8071
@ReactMethod
8172
public void setMinimumSessionDuration(final double milliseconds) {
82-
mFirebaseAnalytics.setMinimumSessionDuration((long) milliseconds);
73+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setMinimumSessionDuration((long) milliseconds);
8374
}
8475

8576
/**
@@ -88,7 +79,7 @@ public void setMinimumSessionDuration(final double milliseconds) {
8879
*/
8980
@ReactMethod
9081
public void setSessionTimeoutDuration(final double milliseconds) {
91-
mFirebaseAnalytics.setSessionTimeoutDuration((long) milliseconds);
82+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setSessionTimeoutDuration((long) milliseconds);
9283
}
9384

9485
/**
@@ -97,7 +88,7 @@ public void setSessionTimeoutDuration(final double milliseconds) {
9788
*/
9889
@ReactMethod
9990
public void setUserId(final String id) {
100-
mFirebaseAnalytics.setUserId(id);
91+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setUserId(id);
10192
}
10293

10394
/**
@@ -107,144 +98,6 @@ public void setUserId(final String id) {
10798
*/
10899
@ReactMethod
109100
public void setUserProperty(final String name, final String value) {
110-
mFirebaseAnalytics.setUserProperty(name, value);
111-
}
112-
113-
// todo refactor/clean me
114-
private Bundle makeEventBundle(final String name, final Map<String, Object> map) {
115-
Bundle bundle = new Bundle();
116-
// Available from the FirestackAnalytics event
117-
if (map.containsKey("id")) {
118-
String id = (String) map.get("id");
119-
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
120-
}
121-
if (map.containsKey("name")) {
122-
String val = (String) map.get("name");
123-
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, val);
124-
}
125-
if (map.containsKey("category")) {
126-
String val = (String) map.get("category");
127-
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, val);
128-
}
129-
if (map.containsKey("quantity")) {
130-
double val = (double) map.get("quantity");
131-
bundle.putDouble(FirebaseAnalytics.Param.QUANTITY, val);
132-
}
133-
if (map.containsKey("price")) {
134-
double val = (double) map.get("price");
135-
bundle.putDouble(FirebaseAnalytics.Param.PRICE, val);
136-
}
137-
if (map.containsKey("value")) {
138-
double val = (double) map.get("value");
139-
bundle.putDouble(FirebaseAnalytics.Param.VALUE, val);
140-
}
141-
if (map.containsKey("currency")) {
142-
String val = (String) map.get("currency");
143-
bundle.putString(FirebaseAnalytics.Param.CURRENCY, val);
144-
}
145-
if (map.containsKey("origin")) {
146-
String val = (String) map.get("origin");
147-
bundle.putString(FirebaseAnalytics.Param.ORIGIN, val);
148-
}
149-
if (map.containsKey("item_location_id")) {
150-
String val = (String) map.get("item_location_id");
151-
bundle.putString(FirebaseAnalytics.Param.ITEM_LOCATION_ID, val);
152-
}
153-
if (map.containsKey("location")) {
154-
String val = (String) map.get("location");
155-
bundle.putString(FirebaseAnalytics.Param.LOCATION, val);
156-
}
157-
if (map.containsKey("destination")) {
158-
String val = (String) map.get("destination");
159-
bundle.putString(FirebaseAnalytics.Param.DESTINATION, val);
160-
}
161-
if (map.containsKey("start_date")) {
162-
String val = (String) map.get("start_date");
163-
bundle.putString(FirebaseAnalytics.Param.START_DATE, val);
164-
}
165-
if (map.containsKey("end_date")) {
166-
String val = (String) map.get("end_date");
167-
bundle.putString(FirebaseAnalytics.Param.END_DATE, val);
168-
}
169-
if (map.containsKey("transaction_id")) {
170-
String val = (String) map.get("transaction_id");
171-
bundle.putString(FirebaseAnalytics.Param.TRANSACTION_ID, val);
172-
}
173-
if (map.containsKey("number_of_nights")) {
174-
long val = (long) map.get("number_of_nights");
175-
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_NIGHTS, val);
176-
}
177-
if (map.containsKey("number_of_rooms")) {
178-
long val = (long) map.get("number_of_rooms");
179-
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_ROOMS, val);
180-
}
181-
if (map.containsKey("number_of_passengers")) {
182-
long val = (long) map.get("number_of_passengers");
183-
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_PASSENGERS, val);
184-
}
185-
if (map.containsKey("travel_class")) {
186-
String val = (String) map.get("travel_class");
187-
bundle.putString(FirebaseAnalytics.Param.TRAVEL_CLASS, val);
188-
}
189-
if (map.containsKey("coupon")) {
190-
String val = (String) map.get("coupon");
191-
bundle.putString(FirebaseAnalytics.Param.COUPON, val);
192-
}
193-
if (map.containsKey("tax")) {
194-
long val = (long) map.get("tax");
195-
bundle.putLong(FirebaseAnalytics.Param.TAX, val);
196-
}
197-
if (map.containsKey("shipping")) {
198-
double val = (double) map.get("shipping");
199-
bundle.putDouble(FirebaseAnalytics.Param.SHIPPING, val);
200-
}
201-
if (map.containsKey("group_id")) {
202-
String val = (String) map.get("group_id");
203-
bundle.putString(FirebaseAnalytics.Param.GROUP_ID, val);
204-
}
205-
if (map.containsKey("level")) {
206-
long val = (long) map.get("level");
207-
bundle.putLong(FirebaseAnalytics.Param.LEVEL, val);
208-
}
209-
if (map.containsKey("character")) {
210-
String val = (String) map.get("character");
211-
bundle.putString(FirebaseAnalytics.Param.CHARACTER, val);
212-
}
213-
if (map.containsKey("score")) {
214-
long val = (long) map.get("score");
215-
bundle.putLong(FirebaseAnalytics.Param.SCORE, val);
216-
}
217-
if (map.containsKey("search_term")) {
218-
String val = (String) map.get("search_term");
219-
bundle.putString(FirebaseAnalytics.Param.SEARCH_TERM, val);
220-
}
221-
if (map.containsKey("content_type")) {
222-
String val = (String) map.get("content_type");
223-
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, val);
224-
}
225-
if (map.containsKey("sign_up_method")) {
226-
String val = (String) map.get("sign_up_method");
227-
bundle.putString(FirebaseAnalytics.Param.SIGN_UP_METHOD, val);
228-
}
229-
if (map.containsKey("virtual_currency_name")) {
230-
String val = (String) map.get("virtual_currency_name");
231-
bundle.putString(FirebaseAnalytics.Param.VIRTUAL_CURRENCY_NAME, val);
232-
}
233-
if (map.containsKey("achievement_id")) {
234-
String val = (String) map.get("achievement_id");
235-
bundle.putString(FirebaseAnalytics.Param.ACHIEVEMENT_ID, val);
236-
}
237-
if (map.containsKey("flight_number")) {
238-
String val = (String) map.get("flight_number");
239-
bundle.putString(FirebaseAnalytics.Param.FLIGHT_NUMBER, val);
240-
}
241-
242-
for (Map.Entry<String, Object> entry : map.entrySet()) {
243-
if (bundle.getBundle(entry.getKey()) == null) {
244-
bundle.putString(entry.getKey(), entry.getValue().toString());
245-
}
246-
}
247-
248-
return bundle;
101+
FirebaseAnalytics.getInstance(getReactApplicationContext()).setUserProperty(name, value);
249102
}
250103
}

0 commit comments

Comments
 (0)