Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
react-native-fbsdk-0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhuowen committed Jul 22, 2016
1 parent c191a43 commit eea514d
Show file tree
Hide file tree
Showing 41 changed files with 575 additions and 308 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -22,6 +22,9 @@ DerivedData
*.xcuserstate
project.xcworkspace

# Cocoapods
Pods/

# node.js
#
node_modules/
Expand Down
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
# Sample app
Sample/
3 changes: 1 addition & 2 deletions Android/build.gradle
Expand Up @@ -2,14 +2,13 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
Expand Down
Expand Up @@ -21,6 +21,7 @@
package com.facebook.reactnative.androidsdk;

import android.os.Bundle;
import android.util.SparseArray;

import com.facebook.AccessToken;
import com.facebook.FacebookRequestError;
Expand All @@ -33,122 +34,106 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

/**
* FBGraphRequestModule holds a list of Request objects and send them to Facebook in a single
* FBGraphRequestModule holds a list of request objects and send them to Facebook in a single
* round-trip.
*/
public class FBGraphRequestModule extends ReactContextBaseJavaModule {
private SparseArray<WritableMap> mResponses;

private class GraphRequestBatchCallback implements GraphRequestBatch.Callback {

private int mBatchId;
private int mBatchID;
private Callback mCallback;

public GraphRequestBatchCallback(int batchId, Callback callback) {
mBatchId = batchId;
public GraphRequestBatchCallback(int batchID, Callback callback) {
mBatchID = batchID;
mCallback = callback;
}

@Override
public void onBatchCompleted(GraphRequestBatch batch) {
if (mCallback != null) {
mCallback.invoke(null, "success");
}
mBatchLookup.remove(mBatchId);
mBatchCallbacks.remove(mBatchId);
mCallback = null;
WritableMap result = Arguments.createMap();
result.putString("result", "batch finished executing or timed out");
mCallback.invoke(null, result, mResponses.get(mBatchID));
mResponses.remove(mBatchID);
}
}

private class GraphRequestCallback implements GraphRequest.Callback {

private Callback mCallback;
private String mIndex;
private int mBatchID;

public GraphRequestCallback(Callback callback) {
mCallback = callback;
public GraphRequestCallback(int index, int batchID) {
mIndex = String.valueOf(index);
mBatchID = batchID;
}

@Override
public void onCompleted(GraphResponse response) {
if (mCallback != null) {
mCallback.invoke(buildFacebookRequestError(response.getError()), response.getRawResponse());
}
mCallback = null;
WritableArray responseArray = Arguments.createArray();
responseArray.pushMap(buildFacebookRequestError(response.getError()));
responseArray.pushMap(buildGraphResponse(response));
mResponses.get(mBatchID).putArray(mIndex, responseArray);
}
}

public FBGraphRequestModule(ReactApplicationContext reactContext) {
super(reactContext);
mBatchLookup = new HashMap<>();
mBatchCallbacks = new HashMap<>();
mResponses = new SparseArray<WritableMap>();
}

@Override
public String getName() {
return "FBGraphRequest";
}

private HashMap<Integer, GraphRequestBatch> mBatchLookup;
private HashMap<Integer, Callback> mBatchCallbacks;

/**
* Add a single {@link GraphRequest} to current batch.
* @param batchId which indicates the GraphRequestBatch on which to apply this method.
* @param graphRequestMap must contain a valid {@link GraphRequest} object.
* @param callback Use Callback to pass result of the single request back to JS.
*/
@ReactMethod
public void addToConnection(int batchId, ReadableMap graphRequestMap, Callback callback) {
GraphRequest graphRequest = buildRequest(graphRequestMap, callback);
GraphRequestBatch graphRequestBatch = mBatchLookup.get(batchId);
if (graphRequestBatch == null) {
graphRequestBatch = new GraphRequestBatch();
mBatchLookup.put(batchId, graphRequestBatch);
}
graphRequestBatch.add(graphRequest);
}

/**
* Adds a batch-level callback which will be called when all requests in the batch have finished
* executing.
* @param batchId which indicates the GraphRequestBatch on which to apply this method.
* @param callback Use Callback to pass result of the batch back to JS.
*/
@ReactMethod
public void addBatchCallback(int batchId, Callback callback) {
mBatchCallbacks.put(batchId, callback);
}

/**
* Executes this batch asynchronously. This function will return immediately, and the batch will
* be processed on a separate thread. In order to process results of a request, or determine
* whether a request succeeded or failed, a callback must be specified when creating the request.
* @param batchId which indicates the GraphRequestBatch on which to apply this method.
* @param timeout Sets the timeout to wait for responses from the server before a timeout error occurs.
* The default input is 0, which means no timeout.
* Send the batch of requests.
* @param requestBatch
* @param timeout
* @param batchCallback
*/
@ReactMethod
public void start(int batchId, int timeout) {
GraphRequestBatch graphRequestBatch = mBatchLookup.get(batchId);
if (graphRequestBatch != null) {
graphRequestBatch.setTimeout(timeout);
graphRequestBatch.addCallback(
new GraphRequestBatchCallback(batchId, mBatchCallbacks.get(batchId)));
graphRequestBatch.executeAsync();
public void start(ReadableArray requestBatch, int timeout, Callback batchCallback) {
GraphRequestBatch batch = new GraphRequestBatch();
int potentialID = 0;
int batchID = 0;
synchronized (this) {
do {
batchID = potentialID++;
} while (mResponses.get(batchID) != null);
mResponses.put(batchID, Arguments.createMap());
}
for (int i = 0; i < requestBatch.size(); i++) {
GraphRequest request = buildRequest(requestBatch.getMap(i));
request.setCallback(new GraphRequestCallback(i, batchID));
batch.add(request);
}
batch.setTimeout(timeout);
GraphRequestBatchCallback callback = new GraphRequestBatchCallback(batchID, batchCallback);
batch.addCallback(callback);
batch.executeAsync();
}

private GraphRequest buildRequest(ReadableMap requestMap, Callback callback) {
private GraphRequest buildRequest(ReadableMap requestMap) {
GraphRequest graphRequest = new GraphRequest();
graphRequest.setGraphPath(requestMap.getString("graphPath"));
setConfig(graphRequest, requestMap.getMap("config"));
graphRequest.setCallback(new GraphRequestCallback(callback));
return graphRequest;
}

Expand All @@ -175,8 +160,7 @@ private void setConfig(GraphRequest graphRequest, ReadableMap configMap) {
null,
null,
null,
null)
);
null));
} else {
graphRequest.setAccessToken(AccessToken.getCurrentAccessToken());
}
Expand All @@ -195,7 +179,7 @@ private Bundle buildParameters(ReadableMap parameterMap) {
return parameters;
}

private ReadableMap buildFacebookRequestError(FacebookRequestError error) {
private WritableMap buildFacebookRequestError(FacebookRequestError error) {
if (error == null) {
return null;
}
Expand Down Expand Up @@ -229,4 +213,65 @@ private ReadableMap buildFacebookRequestError(FacebookRequestError error) {
}
return errorMap;
}

private WritableMap buildGraphResponse(GraphResponse response) {
if (response.getJSONObject() != null) {
return convertJSONObject(response.getJSONObject());
}
return Arguments.createMap();
}

private WritableArray convertJSONArray(JSONArray jsonArray) {
WritableArray result = Arguments.createArray();
for (int i = 0; i < jsonArray.length(); i++) {
Object object;
try {
object = jsonArray.get(i);
} catch (JSONException e) {
return result;
}
if (object instanceof JSONObject) {
result.pushMap(convertJSONObject((JSONObject) object));
} else if (object instanceof JSONArray) {
result.pushArray(convertJSONArray((JSONArray) object));
} else if (object instanceof String) {
result.pushString((String) object);
} else if (object instanceof Integer) {
result.pushInt((int) object);
} else if (object instanceof Boolean) {
result.pushBoolean((Boolean) object);
} else if (object instanceof Double) {
result.pushDouble((Double) object);
}
}
return result;
}

private WritableMap convertJSONObject(JSONObject object) {
WritableMap result = Arguments.createMap();
Iterator<String> keyIterator = object.keys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
Object value;
try {
value = object.get(key);
} catch (JSONException e) {
return result;
}
if (value instanceof JSONObject) {
result.putMap(key, convertJSONObject((JSONObject) value));
} else if (value instanceof JSONArray) {
result.putArray(key, convertJSONArray((JSONArray) value));
} else if (value instanceof String) {
result.putString(key, (String) value);
} else if (value instanceof Integer) {
result.putInt(key, (int) value);
} else if (value instanceof Boolean) {
result.putBoolean(key, (Boolean) value);
} else if (value instanceof Double) {
result.putDouble(key, (Double) value);
}
}
return result;
}
}
Expand Up @@ -34,7 +34,7 @@

public class FBSDKPackage implements ReactPackage {

public static String VERSION_TO_RELEASE = "ReactNative-v0.2.2";
public static final String VERSION_TO_RELEASE = "ReactNative-v0.3.0";

private CallbackManager mCallbackManager;
public FBSDKPackage(CallbackManager callbackManager) {
Expand Down
5 changes: 1 addition & 4 deletions README.md
Expand Up @@ -84,7 +84,7 @@ import com.facebook.reactnative.androidsdk.FBSDKPackage;
public class MainApplication extends Application implements ReactApplication {

private static CallbackManager mCallbackManager = CallbackManager.Factory.create();

protected static CallbackManager getCallbackManager() {
return mCallbackManager;
}
Expand Down Expand Up @@ -158,9 +158,6 @@ Make sure you have the latest [Xcode](https://developer.apple.com/xcode/) instal

- Make sure that `libRCTFBSDK.a` shows up in the **Link Binary with Libraries** section of your build target's **Build Phases**.

## GIVE FEEDBACK
Please report bugs or issues to https://developers.facebook.com/bugs/

## Usage
### [Login](https://developers.facebook.com/docs/facebook-login)
#### Login Button + Access Token
Expand Down
Expand Up @@ -825,4 +825,4 @@
/* End XCConfigurationList section */
};
rootObject = 83CBB9F71A601CBA00E9B192 /* Project object */;
}
}
12 changes: 8 additions & 4 deletions iOS/RCTFBSDK.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
93B905C21D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 93B905C11D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.m */; };
93E0043A1CE3C33F000598E3 /* FBSDKCoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 93E004371CE3C33F000598E3 /* FBSDKCoreKit.framework */; };
93E0043B1CE3C33F000598E3 /* FBSDKLoginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 93E004381CE3C33F000598E3 /* FBSDKLoginKit.framework */; };
93E0043C1CE3C33F000598E3 /* FBSDKShareKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 93E004391CE3C33F000598E3 /* FBSDKShareKit.framework */; };
Expand Down Expand Up @@ -46,6 +47,8 @@

/* Begin PBXFileReference section */
9350E0F11CE3B0920041D815 /* libRCTFBSDK.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTFBSDK.a; sourceTree = BUILT_PRODUCTS_DIR; };
93B905C01D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RCTFBSDKGraphRequestConnectionContainer.h; path = core/RCTFBSDKGraphRequestConnectionContainer.h; sourceTree = "<group>"; };
93B905C11D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RCTFBSDKGraphRequestConnectionContainer.m; path = core/RCTFBSDKGraphRequestConnectionContainer.m; sourceTree = "<group>"; };
93E004371CE3C33F000598E3 /* FBSDKCoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FBSDKCoreKit.framework; path = ../../../../../../Documents/FacebookSDK/FBSDKCoreKit.framework; sourceTree = "<group>"; };
93E004381CE3C33F000598E3 /* FBSDKLoginKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FBSDKLoginKit.framework; path = ../../../../../../Documents/FacebookSDK/FBSDKLoginKit.framework; sourceTree = "<group>"; };
93E004391CE3C33F000598E3 /* FBSDKShareKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FBSDKShareKit.framework; path = ../../../../../../Documents/FacebookSDK/FBSDKShareKit.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,6 +147,8 @@
93E004DA1CE3D286000598E3 /* core */ = {
isa = PBXGroup;
children = (
93B905C01D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.h */,
93B905C11D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.m */,
93E005301CE3D63D000598E3 /* RCTFBSDKInitializer.m */,
93E004DB1CE3D292000598E3 /* RCTConvert+FBSDKAccessToken.h */,
93E004DC1CE3D292000598E3 /* RCTConvert+FBSDKAccessToken.m */,
Expand Down Expand Up @@ -277,6 +282,7 @@
93E005131CE3D2D3000598E3 /* RCTFBSDKShareAPI.m in Sources */,
93E004F11CE3D2B4000598E3 /* RCTFBSDKLoginManager.m in Sources */,
93E005121CE3D2D3000598E3 /* RCTFBSDKSendButtonManager.m in Sources */,
93B905C21D2D987F0013CC92 /* RCTFBSDKGraphRequestConnectionContainer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -371,14 +377,13 @@
COPY_PHASE_STRIP = NO;
FRAMEWORK_SEARCH_PATHS = (
"~/Documents/FacebookSDK",
"$(PROJECT_DIR)/../../../ios/**",
"$(PROJECT_DIR)/../../../ios/Frameworks",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
"$(SRCROOT)/../../react/**",
"${SRCROOT}/../../react-native/React/**",
"${SRCROOT}/../../../ios/**",
);
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
OTHER_LDFLAGS = "-ObjC";
Expand All @@ -394,14 +399,13 @@
COPY_PHASE_STRIP = YES;
FRAMEWORK_SEARCH_PATHS = (
"~/Documents/FacebookSDK",
"$(PROJECT_DIR)/../../../ios/**",
"$(PROJECT_DIR)/../../../ios/Frameworks",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
"$(SRCROOT)/../../react/**",
"${SRCROOT}/../../react-native/React/**",
"${SRCROOT}/../../../ios/**",
);
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
OTHER_LDFLAGS = "-ObjC";
Expand Down

0 comments on commit eea514d

Please sign in to comment.