Skip to content

Commit

Permalink
feat: add support for new architecture in compatibility mode (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldonadel committed Apr 10, 2024
1 parent 6adba2f commit 7fda601
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 60 deletions.
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

0 comments on commit 7fda601

Please sign in to comment.