From 92d4baa84388b73bbdc0952d16addb2fb5764d78 Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Date: Wed, 20 Mar 2024 11:01:01 -0300 Subject: [PATCH] feat: Add support for the new architecture --- .../androidsdk/FBSettingsModule.java | 14 ++--- .../androidsdk/RCTLoginButton.java | 26 +++----- .../androidsdk/RCTLoginButtonEvent.java | 27 +++++++++ .../login/RCTFBSDKLoginButtonManager.h | 2 +- .../login/RCTFBSDKLoginButtonManager.m | 39 ++---------- ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h | 10 ++++ ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m | 59 +++++++++++++++++++ 7 files changed, 117 insertions(+), 60 deletions(-) create mode 100644 android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButtonEvent.java create mode 100644 ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h create mode 100644 ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m diff --git a/android/src/main/java/com/facebook/reactnative/androidsdk/FBSettingsModule.java b/android/src/main/java/com/facebook/reactnative/androidsdk/FBSettingsModule.java index a12b09dc..dc37b2ff 100644 --- a/android/src/main/java/com/facebook/reactnative/androidsdk/FBSettingsModule.java +++ b/android/src/main/java/com/facebook/reactnative/androidsdk/FBSettingsModule.java @@ -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(); } @@ -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); } @@ -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); } } diff --git a/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButton.java b/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButton.java index 1b27c6bd..8cd40ace 100644 --- a/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButton.java +++ b/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButton.java @@ -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; @@ -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(); } @@ -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)); + } } }; @@ -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 @@ -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 @@ -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)); } }); } diff --git a/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButtonEvent.java b/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButtonEvent.java new file mode 100644 index 00000000..a5358c98 --- /dev/null +++ b/android/src/main/java/com/facebook/reactnative/androidsdk/RCTLoginButtonEvent.java @@ -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 { + 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; + } +} diff --git a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.h b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.h index a744bc99..0a99881b 100644 --- a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.h +++ b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.h @@ -20,6 +20,6 @@ #import -@interface RCTFBSDKLoginButtonManager : RCTViewManager +@interface RCTFBSDKLoginButtonManager : RCTViewManager @end diff --git a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.m b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.m index db259f5f..c5826b5f 100644 --- a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.m +++ b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonManager.m @@ -17,8 +17,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #import "RCTFBSDKLoginButtonManager.h" +#import "RCTFBSDKLoginButtonView.h" -#import #import #import #import @@ -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) @@ -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 diff --git a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h new file mode 100644 index 00000000..40de9cc9 --- /dev/null +++ b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.h @@ -0,0 +1,10 @@ +#import + +#import +#import + +@interface RCTFBSDKLoginButtonView: UIView + +@property (nonatomic, copy) RCTBubblingEventBlock onChange; + +@end diff --git a/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m new file mode 100644 index 00000000..fefa26c1 --- /dev/null +++ b/ios/RCTFBSDK/login/RCTFBSDKLoginButtonView.m @@ -0,0 +1,59 @@ +#import "RCTFBSDKLoginButtonView.h" + +#import +#import +#import +#import + + +@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