Skip to content

Commit

Permalink
Replace the deprecated event dispatching via RCTEventDispatcher with … (
Browse files Browse the repository at this point in the history
#66)

* Replace the deprecated event dispatching via RCTEventDispatcher with RCTBubblingEventBlocks for iOS.

* Fix broken build.

* Fix broken include due to bad merge.

* Use RCTBubblingEventBlocks in RNAdMobInterstitial & RNAdMobRewarded as well.

* Revert "Use RCTBubblingEventBlocks in RNAdMobInterstitial & RNAdMobRewarded as well."

This reverts commit 3bec144.

* Update RNDFPBannerView.h

Added right include check
  • Loading branch information
mars-lan authored and alvaromb committed Mar 21, 2017
1 parent 7ba89a5 commit bd39ad9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 103 deletions.
13 changes: 10 additions & 3 deletions ios/BannerView.h
@@ -1,7 +1,7 @@
#if __has_include(<React/RCTEventDispatcher.h>)
#import <React/RCTEventDispatcher.h>
#import <React/RCTComponent.h>
#else
#import "RCTEventDispatcher.h"
#import "RCTComponent.h"
#endif

@import GoogleMobileAds;
Expand All @@ -14,7 +14,14 @@
@property (nonatomic, copy) NSString *adUnitID;
@property (nonatomic, copy) NSString *testDeviceID;

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd;
@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication;

- (GADAdSize)getAdSizeFromString:(NSString *)bannerSize;
- (void)loadBanner;

Expand Down
69 changes: 28 additions & 41 deletions ios/BannerView.m
Expand Up @@ -12,20 +12,8 @@

@implementation BannerView {
GADBannerView *_bannerView;
RCTEventDispatcher *_eventDispatcher;
}

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super initWithFrame:CGRectZero])) {
_eventDispatcher = eventDispatcher;
}
return self;
}

RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)

- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
{
RCTLogError(@"AdMob Banner cannot have any subviews");
Expand Down Expand Up @@ -65,13 +53,12 @@ -(void)loadBanner
GADAdSize size = [self getAdSizeFromString:_bannerSize];
_bannerView = [[GADBannerView alloc] initWithAdSize:size];
if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) {
[_eventDispatcher
sendInputEventWithName:@"onSizeChange"
body:@{
@"target": self.reactTag,
if (self.onSizeChange) {
self.onSizeChange(@{
@"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width],
@"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height]
}];
});
}
}
_bannerView.delegate = self;
_bannerView.adUnitID = _adUnitID;
Expand Down Expand Up @@ -132,49 +119,49 @@ -(void)layoutSubviews
[self addSubview:_bannerView];
}

- (void)removeFromSuperview
{
_eventDispatcher = nil;
[super removeFromSuperview];
}

/// Tells the delegate an ad request loaded an ad.
- (void)adViewDidReceiveAd:(GADBannerView *)adView
{
[_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }];
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
if (self.onAdViewDidReceiveAd) {
self.onAdViewDidReceiveAd(@{});
}
}

/// Tells the delegate an ad request failed.
- (void)adView:(GADBannerView *)adView
didFailToReceiveAdWithError:(GADRequestError *)error
{
[_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }];
didFailToReceiveAdWithError:(GADRequestError *)error {
if (self.onDidFailToReceiveAdWithError) {
self.onDidFailToReceiveAdWithError(@{@"error": [error localizedDescription]});
}
}

/// Tells the delegate that a full screen view will be presented in response
/// to the user clicking on an ad.
- (void)adViewWillPresentScreen:(GADBannerView *)adView
{
[_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }];
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
if (self.onAdViewWillPresentScreen) {
self.onAdViewWillPresentScreen(@{});
}
}

/// Tells the delegate that the full screen view will be dismissed.
- (void)adViewWillDismissScreen:(GADBannerView *)adView
{
[_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }];
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
if (self.onAdViewWillDismissScreen) {
self.onAdViewWillDismissScreen(@{});
}
}

/// Tells the delegate that the full screen view has been dismissed.
- (void)adViewDidDismissScreen:(GADBannerView *)adView
{
[_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }];
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
if (self.onAdViewDidDismissScreen) {
self.onAdViewDidDismissScreen(@{});
}
}

/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
- (void)adViewWillLeaveApplication:(GADBannerView *)adView
{
[_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }];
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
if (self.onAdViewWillLeaveApplication) {
self.onAdViewWillLeaveApplication(@{});
}
}

@end
25 changes: 10 additions & 15 deletions ios/RNAdMobDFPManager.m
Expand Up @@ -15,21 +15,7 @@ @implementation RNAdMobDFPManager

- (UIView *)view
{
return [[RNDFPBannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
}

- (NSArray *) customDirectEventTypes
{
return @[
@"onSizeChange",
@"onAdViewDidReceiveAd",
@"onDidFailToReceiveAdWithError",
@"onAdViewWillPresentScreen",
@"onAdViewWillDismissScreen",
@"onAdViewDidDismissScreen",
@"onAdViewWillLeaveApplication",
@"onAdmobDispatchAppEvent"
];
return [[RNDFPBannerView alloc] init];
}

- (dispatch_queue_t)methodQueue
Expand All @@ -42,4 +28,13 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString);
RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString);

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdmobDispatchAppEvent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock)

@end
23 changes: 9 additions & 14 deletions ios/RNAdMobManager.m
Expand Up @@ -15,20 +15,7 @@ @implementation RNAdMobManager

- (UIView *)view
{
return [[BannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
}

- (NSArray *) customDirectEventTypes
{
return @[
@"onSizeChange",
@"onAdViewDidReceiveAd",
@"onDidFailToReceiveAdWithError",
@"onAdViewWillPresentScreen",
@"onAdViewWillDismissScreen",
@"onAdViewDidDismissScreen",
@"onAdViewWillLeaveApplication"
];
return [[BannerView alloc] init];
}

- (dispatch_queue_t)methodQueue
Expand All @@ -41,4 +28,12 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString);
RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString);

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock)

@end
15 changes: 12 additions & 3 deletions ios/RNDFPBannerView.h
@@ -1,7 +1,7 @@
#if __has_include(<React/RCTEventDispatcher.h>)
#import <React/RCTEventDispatcher.h>
#if __has_include(<React/RCTComponent.h>)
#import <React/RCTComponent.h>
#else
#import "RCTEventDispatcher.h"
#import "RCTComponent.h"
#endif

@import GoogleMobileAds;
Expand All @@ -14,6 +14,15 @@
@property (nonatomic, copy) NSString *adUnitID;
@property (nonatomic, copy) NSString *testDeviceID;

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAdmobDispatchAppEvent;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd;
@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen;
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication;

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
- (GADAdSize)getAdSizeFromString:(NSString *)bannerSize;
- (void)loadBanner;
Expand Down
52 changes: 25 additions & 27 deletions ios/RNDFPBannerView.m
Expand Up @@ -12,20 +12,8 @@

@implementation RNDFPBannerView {
DFPBannerView *_bannerView;
RCTEventDispatcher *_eventDispatcher;
}

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super initWithFrame:CGRectZero])) {
_eventDispatcher = eventDispatcher;
}
return self;
}

RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)

- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
{
RCTLogError(@"AdMob Banner cannot have any subviews");
Expand Down Expand Up @@ -66,13 +54,12 @@ -(void)loadBanner {
_bannerView = [[DFPBannerView alloc] initWithAdSize:size];
[_bannerView setAppEventDelegate:self]; //added Admob event dispatch listener
if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) {
[_eventDispatcher
sendInputEventWithName:@"onSizeChange"
body:@{
@"target": self.reactTag,
if (self.onSizeChange) {
self.onSizeChange(@{
@"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width],
@"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height]
}];
});
}
}
_bannerView.delegate = self;
_bannerView.adUnitID = _adUnitID;
Expand All @@ -97,7 +84,9 @@ - (void)adView:(DFPBannerView *)banner
NSLog(@"Received app event (%@, %@)", name, info);
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
myDictionary[name] = info;
[_eventDispatcher sendInputEventWithName:@"onAdmobDispatchAppEvent" body:@{ @"target": self.reactTag, name: info }];
if (self.onAdmobDispatchAppEvent) {
self.onAdmobDispatchAppEvent(@{ name: info });
}
}

- (void)setBannerSize:(NSString *)bannerSize
Expand All @@ -111,8 +100,6 @@ - (void)setBannerSize:(NSString *)bannerSize
}
}



- (void)setAdUnitID:(NSString *)adUnitID
{
if(![adUnitID isEqual:_adUnitID]) {
Expand Down Expand Up @@ -149,41 +136,52 @@ -(void)layoutSubviews

- (void)removeFromSuperview
{
_eventDispatcher = nil;
[super removeFromSuperview];
}

/// Tells the delegate an ad request loaded an ad.
- (void)adViewDidReceiveAd:(DFPBannerView *)adView {
[_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }];
if (self.onAdViewDidReceiveAd) {
self.onAdViewDidReceiveAd(@{});
}
}

/// Tells the delegate an ad request failed.
- (void)adView:(DFPBannerView *)adView
didFailToReceiveAdWithError:(GADRequestError *)error {
[_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }];
if (self.onDidFailToReceiveAdWithError) {
self.onDidFailToReceiveAdWithError(@{ @"error": [error localizedDescription] });
}
}

/// Tells the delegate that a full screen view will be presented in response
/// to the user clicking on an ad.
- (void)adViewWillPresentScreen:(DFPBannerView *)adView {
[_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }];
if (self.onAdViewWillPresentScreen) {
self.onAdViewWillPresentScreen(@{});
}
}

/// Tells the delegate that the full screen view will be dismissed.
- (void)adViewWillDismissScreen:(DFPBannerView *)adView {
[_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }];
if (self.onAdViewWillDismissScreen) {
self.onAdViewWillDismissScreen(@{});
}
}

/// Tells the delegate that the full screen view has been dismissed.
- (void)adViewDidDismissScreen:(DFPBannerView *)adView {
[_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }];
if (self.onAdViewDidDismissScreen) {
self.onAdViewDidDismissScreen(@{});
}
}

/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
- (void)adViewWillLeaveApplication:(DFPBannerView *)adView {
[_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }];
if (self.onAdViewWillLeaveApplication) {
self.onAdViewWillLeaveApplication(@{});
}
}

@end

0 comments on commit bd39ad9

Please sign in to comment.