Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.
Open
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
50 changes: 36 additions & 14 deletions ios/BannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BannerView: UIView, GADAppEventDelegate, GADBannerViewDelegate, GADAdSizeD
var width: Int?, height: Int? = nil

var isAdUnitSet: Bool = false
var isFluid: Bool = true

@objc var onAdClicked: RCTDirectEventBlock?
@objc var onAdClosed: RCTDirectEventBlock?
Expand Down Expand Up @@ -44,6 +45,13 @@ class BannerView: UIView, GADAppEventDelegate, GADBannerViewDelegate, GADAdSizeD
@objc var targeting: NSDictionary? = [:]
@objc var testDeviceIds: Array<String>? = [""]

@objc var fluid: Bool = false {
didSet {
sendIfPropsSet()
isFluid = fluid
}
}

override init(frame: CGRect) {
super.init(frame: frame)
}
Expand All @@ -69,25 +77,33 @@ class BannerView: UIView, GADAppEventDelegate, GADBannerViewDelegate, GADAdSizeD
}

func createAdView(){
bannerView = DFPBannerView.init()

if(isFluid){
bannerView = DFPBannerView(adSize: kGADAdSizeFluid)
}
else{
bannerView = DFPBannerView.init()
}

let rootViewController = UIApplication.shared.delegate?.window??.rootViewController

var validAdSizes = [NSValue]()

let adSizesArray = adSizes ?? []

for sizes in adSizesArray {

for sizes in adSizes! {
let width = sizes[0]
let height = sizes[1]
let customGADAdSize = GADAdSizeFromCGSize(CGSize(width: width, height: height))
validAdSizes.append(NSValueFromGADAdSize(customGADAdSize))
}

if(isFluid){
validAdSizes.append(NSValueFromGADAdSize(kGADAdSizeFluid))
} else{
bannerView?.translatesAutoresizingMaskIntoConstraints = false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know nothing about Swift, it's cool to see that it also has optional chaining 👌

}

bannerView?.rootViewController = rootViewController
bannerView?.delegate = self
bannerView?.adSizeDelegate = self
bannerView?.translatesAutoresizingMaskIntoConstraints = false
bannerView?.appEventDelegate = self
bannerView?.validAdSizes = validAdSizes
}
Expand Down Expand Up @@ -136,12 +152,18 @@ class BannerView: UIView, GADAppEventDelegate, GADBannerViewDelegate, GADAdSizeD

func adView(_ bannerView: GADBannerView, willChangeAdSizeTo size: GADAdSize) {
print("\(LOG_TAG): Ad size changed")
if let adSize = adSizes?.first(where: {
let customGADAdSize = GADAdSizeFromCGSize(CGSize(width: $0[0], height: $0[1]))
return GADAdSizeEqualToSize(customGADAdSize, size)
}) {
width = adSize[0]
height = adSize[1]
if(isFluid){
width = Int(size.size.width)
height = Int(size.size.height)
}
else{
if let adSize = adSizes?.first(where: {
let customGADAdSize = GADAdSizeFromCGSize(CGSize(width: $0[0], height: $0[1]))
return GADAdSizeEqualToSize(customGADAdSize, size)
}) {
width = adSize[0]
height = adSize[1]
}
}
}

Expand Down
1 change: 1 addition & 0 deletions ios/RNGoogleAdManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ @interface RCT_EXTERN_REMAP_MODULE(RNGAMBannerView, BannerViewManager, RCTViewMa

RCT_EXPORT_VIEW_PROPERTY(adId, NSString)
RCT_EXPORT_VIEW_PROPERTY(adSizes, NSArray<NSArray>)
RCT_EXPORT_VIEW_PROPERTY(fluid, BOOL)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(testDeviceIds, NSArray<NSString>)

Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@callosum/react-native-google-ad-manager",
"name": "@axioscode/react-native-google-ad-manager",
"version": "2.1.0-rc.7",
"description": "Google Ad Manager for react-native",
"main": "index.js",
Expand Down Expand Up @@ -50,10 +50,13 @@
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/cmsw/react-native-google-ad-manager.git"
"url": "https://github.com/axioscode/react-native-google-ad-manager"
},
"bugs": {
"url": "https://gitlab.com/cmsw/react-native-google-ad-manager/issues"
"url": "https://github.com/axioscode/react-native-google-ad-manager/issues"
},
"homepage": "https://gitlab.com/cmsw/react-native-google-ad-manager#readme"
"homepage": "https://github.com/axioscode/react-native-google-ad-manager#readme",
"directories": {
"example": "example"
}
}
3 changes: 3 additions & 0 deletions src/RNGAMBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class RNGAMBanner extends React.PureComponent {
adId={this.props.adId}
adSizes={this.props.adSizes}
adType={this.props.adType}
fluid={this.props.fluid}
onAdClicked={this._onAdClicked}
onAdClosed={this._onAdClosed}
onAdFailedToLoad={this._onAdFailedToLoad}
Expand All @@ -173,6 +174,7 @@ RNGAMBanner.propTypes = {
adId: P.string.isRequired,
adSizes: P.arrayOf(P.arrayOf(P.number)),
adType: P.string,
fluid: P.bool,
onAdClicked: P.func,
onAdClosed: P.func,
onAdFailedToLoad: P.func,
Expand All @@ -189,6 +191,7 @@ RNGAMBanner.propTypes = {
RNGAMBanner.defaultProps = {
adSizes: undefined,
adType: undefined,
fluid: false,
onAdClicked: noop,
onAdClosed: noop,
onAdFailedToLoad: noop,
Expand Down