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

feat: add support for new architecture in compatibility mode #505

Merged
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 @@ -51,7 +51,7 @@ public void setDataProcessingOptions(ReadableArray options, int country, int sta
* [FB SDK Best Practices for GDPR Compliance](https://developers.facebook.com/docs/app-events/gdpr-compliance/)
*/
@ReactMethod
public static void initializeSDK() {
public void initializeSDK() {
FacebookSdk.fullyInitialize();
}

Expand All @@ -60,7 +60,7 @@ public static void initializeSDK() {
* @param appID app id
*/
@ReactMethod
public static void setAppID(String appID) {
public void setAppID(String appID) {
FacebookSdk.setApplicationId(appID);
}

Expand All @@ -69,27 +69,27 @@ public static void setAppID(String appID) {
* @param clientToken client token
*/
@ReactMethod
public static void setClientToken(String clientToken) {
public void setClientToken(String clientToken) {
FacebookSdk.setClientToken(clientToken);
}

@ReactMethod
public static void setAppName(String displayName) {
public void setAppName(String displayName) {
FacebookSdk.setApplicationName(displayName);
}

@ReactMethod
public static void setGraphAPIVersion(String version) {
public void setGraphAPIVersion(String version) {
FacebookSdk.setGraphApiVersion(version);
}

@ReactMethod
public static void setAutoLogAppEventsEnabled(Boolean enabled) {
public void setAutoLogAppEventsEnabled(Boolean enabled) {
FacebookSdk.setAutoLogAppEventsEnabled(enabled);
}

@ReactMethod
public static void setAdvertiserIDCollectionEnabled(Boolean enabled) {
public void setAdvertiserIDCollectionEnabled(Boolean enabled) {
FacebookSdk.setAdvertiserIDCollectionEnabled(enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.events.EventDispatcher;

import java.util.Set;

Expand All @@ -42,11 +43,13 @@
public class RCTLoginButton extends LoginButton {

private final CallbackManager mCallbackManager;
private final EventDispatcher mEventDispatcher;

public RCTLoginButton(ThemedReactContext context, CallbackManager callbackManager) {
super(context);
this.setToolTipMode(ToolTipMode.NEVER_DISPLAY);
mCallbackManager = callbackManager;
mEventDispatcher = UIManagerHelper.getEventDispatcherForReactTag((ReactContext) getContext(), getId());
init();
}

Expand All @@ -60,10 +63,8 @@ protected void onCurrentAccessTokenChanged(
WritableMap event = Arguments.createMap();
event.putString("type", "logoutFinished");
ReactContext context = (ReactContext) getContext();
context.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
mEventDispatcher.dispatchEvent(new RCTLoginButtonEvent(UIManagerHelper.getSurfaceId(context), getId(), event));

}
}
};
Expand All @@ -85,10 +86,7 @@ public void onSuccess(LoginResult loginResult) {
setToStringArray(loginResult.getRecentlyDeniedPermissions())));
event.putMap("result", result);
ReactContext context = (ReactContext) getContext();
context.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
mEventDispatcher.dispatchEvent(new RCTLoginButtonEvent(UIManagerHelper.getSurfaceId(context), getId(), event));
}

@Override
Expand All @@ -100,10 +98,7 @@ public void onError(FacebookException error) {
result.putBoolean("isCancelled", false);
event.putMap("result", result);
ReactContext context = (ReactContext) getContext();
context.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
mEventDispatcher.dispatchEvent(new RCTLoginButtonEvent(UIManagerHelper.getSurfaceId(context), getId(), event));
}

@Override
Expand All @@ -115,10 +110,7 @@ public void onCancel() {
result.putBoolean("isCancelled", true);
event.putMap("result", result);
ReactContext context = (ReactContext) getContext();
context.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
mEventDispatcher.dispatchEvent(new RCTLoginButtonEvent(UIManagerHelper.getSurfaceId(context), getId(), event));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.facebook.reactnative.androidsdk;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;

public class RCTLoginButtonEvent extends Event<RCTLoginButtonEvent> {
public static final String EVENT_NAME = "topChange";
private final WritableMap mEvent;

public RCTLoginButtonEvent(int surfaceId, int viewTag, WritableMap event) {
super(surfaceId,viewTag);
mEvent = event;
}

@NonNull
@Override
public String getEventName() {
return EVENT_NAME;
}

@Override
protected WritableMap getEventData() {
return mEvent;
}
}
2 changes: 1 addition & 1 deletion ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

#import <React/RCTViewManager.h>

@interface RCTFBSDKLoginButtonManager : RCTViewManager <FBSDKLoginButtonDelegate>
@interface RCTFBSDKLoginButtonManager : RCTViewManager

@end
39 changes: 4 additions & 35 deletions ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import "RCTFBSDKLoginButtonManager.h"
#import "RCTFBSDKLoginButtonView.h"

#import <React/RCTBridge.h>
#import <React/RCTComponentEvent.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>
Expand All @@ -34,13 +34,13 @@ @implementation RCTFBSDKLoginButtonManager

- (UIView *)view
{
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.delegate = self;
return loginButton;
return [[RCTFBSDKLoginButtonView alloc] init];
}

#pragma mark - Properties

RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(permissions, NSStringArray)

RCT_EXPORT_VIEW_PROPERTY(defaultAudience, FBSDKDefaultAudience)
Expand All @@ -60,36 +60,5 @@ - (UIView *)view
[view setTooltipBehavior:json ? [RCTConvert FBSDKLoginButtonTooltipBehavior:json] : FBSDKLoginButtonTooltipBehaviorAutomatic];
}

#pragma mark - FBSDKLoginButtonDelegate

- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error
{
NSDictionary *body = @{
@"type": @"loginFinished",
@"error": error ? RCTJSErrorFromNSError(error) : [NSNull null],
@"result": error ? [NSNull null] : @{
@"isCancelled": @(result.isCancelled),
@"grantedPermissions": result.isCancelled ? [NSNull null] : result.grantedPermissions.allObjects,
@"declinedPermissions": result.isCancelled ? [NSNull null] : result.declinedPermissions.allObjects,
},
};

RCTComponentEvent *event = [[RCTComponentEvent alloc] initWithName:@"topChange"
viewTag:loginButton.reactTag
body:body];
[self.bridge.eventDispatcher sendEvent:event];
}

- (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton
{
NSDictionary *body = @{
@"type": @"logoutFinished",
};

RCTComponentEvent *event = [[RCTComponentEvent alloc] initWithName:@"topChange"
viewTag:loginButton.reactTag
body:body];
[self.bridge.eventDispatcher sendEvent:event];
}

@end
10 changes: 10 additions & 0 deletions ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import <FBSDKLoginKit/FBSDKLoginKit.h>

#import <React/RCTComponent.h>
#import <React/RCTEventDispatcher.h>

@interface RCTFBSDKLoginButtonView: UIView<FBSDKLoginButtonDelegate>

@property (nonatomic, copy) RCTBubblingEventBlock onChange;

@end
59 changes: 59 additions & 0 deletions ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#import "RCTFBSDKLoginButtonView.h"

#import <React/RCTComponentEvent.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>


@interface RCTFBSDKLoginButtonView ()

@property (nonatomic, strong) FBSDKLoginButton *loginButton;

@end

@implementation RCTFBSDKLoginButtonView

- (instancetype)init
{
self = [super init];
if (self) {
self.loginButton = [[FBSDKLoginButton alloc] init];
self.loginButton.delegate = self;
self.loginButton.frame = self.bounds;
self.loginButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self addSubview:_loginButton];

return self;
}
return self;
}

#pragma mark - FBSDKLoginButtonDelegate

- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error
{
NSDictionary *body = @{
@"type": @"loginFinished",
@"error": error ? RCTJSErrorFromNSError(error) : [NSNull null],
@"result": error ? [NSNull null] : @{
@"isCancelled": @(result.isCancelled),
@"grantedPermissions": result.isCancelled ? [NSNull null] : result.grantedPermissions.allObjects,
@"declinedPermissions": result.isCancelled ? [NSNull null] : result.declinedPermissions.allObjects,
},
};

self.onChange(body);
}

- (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton
{
NSDictionary *body = @{
@"type": @"logoutFinished",
};

self.onChange(body);
}

@end
Loading