Skip to content

[v3] updates from fork #241

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

Merged
merged 15 commits into from
Jan 12, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Remote message received");
// debug
Log.d(TAG, "From: " + remoteMessage.getFrom());

if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}

if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
if (remoteMessage.getNotification() != null) {

}
Intent i = new Intent(FirestackMessaging.INTENT_NAME_NOTIFICATION);
i.putExtra("data", remoteMessage);
sendOrderedBroadcast(i, null);
Expand Down
30 changes: 12 additions & 18 deletions android/src/main/java/io/fullstack/firestack/FirestackModule.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package io.fullstack.firestack;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.HashMap;

import android.util.Log;
import android.content.Context;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReactContext;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
Expand All @@ -32,16 +30,10 @@ interface KeySetterFn {
@SuppressWarnings("WeakerAccess")
public 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) {
public FirestackModule(ReactApplicationContext reactContext) {
super(reactContext);
this.context = context;
mReactContext = reactContext;

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

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

FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(this.context);
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(getReactApplicationContext().getBaseContext());

if (defaultOptions == null) {
defaultOptions = new FirebaseOptions.Builder().build();
Expand Down Expand Up @@ -154,7 +146,7 @@ public String setKeyOrDefault(
try {
Log.i(TAG, "Configuring app");
if (app == null) {
app = FirebaseApp.initializeApp(this.context, builder.build());
app = FirebaseApp.initializeApp(getReactApplicationContext().getBaseContext(), builder.build());
}
Log.i(TAG, "Configured");

Expand Down Expand Up @@ -189,14 +181,14 @@ public void serverValue(@Nullable final Callback onComplete) {
public void onHostResume() {
WritableMap params = Arguments.createMap();
params.putBoolean("isForground", true);
Utils.sendEvent(mReactContext, "FirestackAppState", params);
Utils.sendEvent(getReactApplicationContext(), "FirestackAppState", params);
}

@Override
public void onHostPause() {
WritableMap params = Arguments.createMap();
params.putBoolean("isForground", false);
Utils.sendEvent(mReactContext, "FirestackAppState", params);
Utils.sendEvent(getReactApplicationContext(), "FirestackAppState", params);
}

@Override
Expand All @@ -208,6 +200,8 @@ public void onHostDestroy() {
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("googleApiAvailability", getPlayServicesStatus());

// TODO remove once this has been moved on ios
constants.put("serverValueTimestamp", ServerValue.TIMESTAMP);
return constants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FirestackPackage() {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FirestackModule(reactContext, reactContext.getBaseContext()));
modules.add(new FirestackModule(reactContext));
modules.add(new FirestackAuth(reactContext));
modules.add(new FirestackDatabase(reactContext));
modules.add(new FirestackAnalytics(reactContext));
Expand Down
9 changes: 7 additions & 2 deletions android/src/main/java/io/fullstack/firestack/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import com.facebook.react.bridge.ReadableType;
Expand Down Expand Up @@ -82,8 +83,12 @@ public static WritableMap dataSnapshotToMap(
data.putString("value", null);
}
} else {
WritableMap valueMap = Utils.castSnapshotValue(dataSnapshot);
data.putMap("value", valueMap);
Object value = Utils.castSnapshotValue(dataSnapshot);
if (value instanceof WritableNativeArray) {
data.putArray("value", (WritableArray) value);
} else {
data.putMap("value", (WritableMap) value);
}
}

// Child keys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package io.fullstack.firestack.analytics;

import java.util.Map;
import android.util.Log;
import android.os.Bundle;
import android.app.Activity;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

import io.fullstack.firestack.Utils;

public class FirestackAnalytics extends ReactContextBaseJavaModule {

private static final String TAG = "FirestackAnalytics";

private ReactApplicationContext context;
private FirebaseAnalytics mFirebaseAnalytics;

public FirestackAnalytics(ReactApplicationContext reactContext) {
super(reactContext);
context = reactContext;
Log.d(TAG, "New instance");
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this.context);
}

/**
Expand All @@ -37,11 +31,8 @@ public String getName() {
}

@ReactMethod
public void logEvent(final String name, final ReadableMap params) {
Map<String, Object> m = Utils.recursivelyDeconstructReadableMap(params);
final Bundle bundle = makeEventBundle(name, m);
Log.d(TAG, "Logging event " + name);
mFirebaseAnalytics.logEvent(name, bundle);
public void logEvent(final String name, @Nullable final ReadableMap params) {
FirebaseAnalytics.getInstance(getReactApplicationContext()).logEvent(name, Arguments.toBundle(params));
}

/**
Expand All @@ -50,7 +41,7 @@ public void logEvent(final String name, final ReadableMap params) {
*/
@ReactMethod
public void setAnalyticsCollectionEnabled(final Boolean enabled) {
mFirebaseAnalytics.setAnalyticsCollectionEnabled(enabled);
FirebaseAnalytics.getInstance(getReactApplicationContext()).setAnalyticsCollectionEnabled(enabled);
}

/**
Expand All @@ -62,12 +53,12 @@ public void setAnalyticsCollectionEnabled(final Boolean enabled) {
public void setCurrentScreen(final String screenName, final String screenClassOverride) {
final Activity activity = getCurrentActivity();
if (activity != null) {
Log.d(TAG, "setCurrentScreen " + screenName + " - " + screenClassOverride);
// needs to be run on main thread
Log.d(TAG, "setCurrentScreen " + screenName + " - " + screenClassOverride);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
mFirebaseAnalytics.setCurrentScreen(activity, screenName, screenClassOverride);
FirebaseAnalytics.getInstance(getReactApplicationContext()).setCurrentScreen(activity, screenName, screenClassOverride);
}
});
}
Expand All @@ -79,7 +70,7 @@ public void run() {
*/
@ReactMethod
public void setMinimumSessionDuration(final double milliseconds) {
mFirebaseAnalytics.setMinimumSessionDuration((long) milliseconds);
FirebaseAnalytics.getInstance(getReactApplicationContext()).setMinimumSessionDuration((long) milliseconds);
}

/**
Expand All @@ -88,7 +79,7 @@ public void setMinimumSessionDuration(final double milliseconds) {
*/
@ReactMethod
public void setSessionTimeoutDuration(final double milliseconds) {
mFirebaseAnalytics.setSessionTimeoutDuration((long) milliseconds);
FirebaseAnalytics.getInstance(getReactApplicationContext()).setSessionTimeoutDuration((long) milliseconds);
}

/**
Expand All @@ -97,7 +88,7 @@ public void setSessionTimeoutDuration(final double milliseconds) {
*/
@ReactMethod
public void setUserId(final String id) {
mFirebaseAnalytics.setUserId(id);
FirebaseAnalytics.getInstance(getReactApplicationContext()).setUserId(id);
}

/**
Expand All @@ -107,144 +98,6 @@ public void setUserId(final String id) {
*/
@ReactMethod
public void setUserProperty(final String name, final String value) {
mFirebaseAnalytics.setUserProperty(name, value);
}

// todo refactor/clean me
private Bundle makeEventBundle(final String name, final Map<String, Object> map) {
Bundle bundle = new Bundle();
// Available from the FirestackAnalytics event
if (map.containsKey("id")) {
String id = (String) map.get("id");
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
}
if (map.containsKey("name")) {
String val = (String) map.get("name");
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, val);
}
if (map.containsKey("category")) {
String val = (String) map.get("category");
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, val);
}
if (map.containsKey("quantity")) {
double val = (double) map.get("quantity");
bundle.putDouble(FirebaseAnalytics.Param.QUANTITY, val);
}
if (map.containsKey("price")) {
double val = (double) map.get("price");
bundle.putDouble(FirebaseAnalytics.Param.PRICE, val);
}
if (map.containsKey("value")) {
double val = (double) map.get("value");
bundle.putDouble(FirebaseAnalytics.Param.VALUE, val);
}
if (map.containsKey("currency")) {
String val = (String) map.get("currency");
bundle.putString(FirebaseAnalytics.Param.CURRENCY, val);
}
if (map.containsKey("origin")) {
String val = (String) map.get("origin");
bundle.putString(FirebaseAnalytics.Param.ORIGIN, val);
}
if (map.containsKey("item_location_id")) {
String val = (String) map.get("item_location_id");
bundle.putString(FirebaseAnalytics.Param.ITEM_LOCATION_ID, val);
}
if (map.containsKey("location")) {
String val = (String) map.get("location");
bundle.putString(FirebaseAnalytics.Param.LOCATION, val);
}
if (map.containsKey("destination")) {
String val = (String) map.get("destination");
bundle.putString(FirebaseAnalytics.Param.DESTINATION, val);
}
if (map.containsKey("start_date")) {
String val = (String) map.get("start_date");
bundle.putString(FirebaseAnalytics.Param.START_DATE, val);
}
if (map.containsKey("end_date")) {
String val = (String) map.get("end_date");
bundle.putString(FirebaseAnalytics.Param.END_DATE, val);
}
if (map.containsKey("transaction_id")) {
String val = (String) map.get("transaction_id");
bundle.putString(FirebaseAnalytics.Param.TRANSACTION_ID, val);
}
if (map.containsKey("number_of_nights")) {
long val = (long) map.get("number_of_nights");
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_NIGHTS, val);
}
if (map.containsKey("number_of_rooms")) {
long val = (long) map.get("number_of_rooms");
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_ROOMS, val);
}
if (map.containsKey("number_of_passengers")) {
long val = (long) map.get("number_of_passengers");
bundle.putLong(FirebaseAnalytics.Param.NUMBER_OF_PASSENGERS, val);
}
if (map.containsKey("travel_class")) {
String val = (String) map.get("travel_class");
bundle.putString(FirebaseAnalytics.Param.TRAVEL_CLASS, val);
}
if (map.containsKey("coupon")) {
String val = (String) map.get("coupon");
bundle.putString(FirebaseAnalytics.Param.COUPON, val);
}
if (map.containsKey("tax")) {
long val = (long) map.get("tax");
bundle.putLong(FirebaseAnalytics.Param.TAX, val);
}
if (map.containsKey("shipping")) {
double val = (double) map.get("shipping");
bundle.putDouble(FirebaseAnalytics.Param.SHIPPING, val);
}
if (map.containsKey("group_id")) {
String val = (String) map.get("group_id");
bundle.putString(FirebaseAnalytics.Param.GROUP_ID, val);
}
if (map.containsKey("level")) {
long val = (long) map.get("level");
bundle.putLong(FirebaseAnalytics.Param.LEVEL, val);
}
if (map.containsKey("character")) {
String val = (String) map.get("character");
bundle.putString(FirebaseAnalytics.Param.CHARACTER, val);
}
if (map.containsKey("score")) {
long val = (long) map.get("score");
bundle.putLong(FirebaseAnalytics.Param.SCORE, val);
}
if (map.containsKey("search_term")) {
String val = (String) map.get("search_term");
bundle.putString(FirebaseAnalytics.Param.SEARCH_TERM, val);
}
if (map.containsKey("content_type")) {
String val = (String) map.get("content_type");
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, val);
}
if (map.containsKey("sign_up_method")) {
String val = (String) map.get("sign_up_method");
bundle.putString(FirebaseAnalytics.Param.SIGN_UP_METHOD, val);
}
if (map.containsKey("virtual_currency_name")) {
String val = (String) map.get("virtual_currency_name");
bundle.putString(FirebaseAnalytics.Param.VIRTUAL_CURRENCY_NAME, val);
}
if (map.containsKey("achievement_id")) {
String val = (String) map.get("achievement_id");
bundle.putString(FirebaseAnalytics.Param.ACHIEVEMENT_ID, val);
}
if (map.containsKey("flight_number")) {
String val = (String) map.get("flight_number");
bundle.putString(FirebaseAnalytics.Param.FLIGHT_NUMBER, val);
}

for (Map.Entry<String, Object> entry : map.entrySet()) {
if (bundle.getBundle(entry.getKey()) == null) {
bundle.putString(entry.getKey(), entry.getValue().toString());
}
}

return bundle;
FirebaseAnalytics.getInstance(getReactApplicationContext()).setUserProperty(name, value);
}
}
Loading